-1

I am creating a room escape game, similar to those Adventure games. Three rooms: kitchen, living room, bath. Each room is in one scene. There are buttons connecting them. For instance in the kitchen is a little arrow to the right sight, clicking on it will move you to the living room, thus playing the scene living room.

Each scene consists of a backgroundpicturelayer, objects (inventory) layer, button layer, and a actionscript layer.

In the kitchen there is a key you can grab and put in your inventory. I do it with key1_mc and key2_mc. Key2 is the version of key1 that sits invisible in the inventory until key1 is grabbed. When clicking on key1, key1 becomes invisible and key2 becomes visible, thus giving the impression I grabbed it and put it in the inventory. I can copypaste the inventorylayer from kitchen in every other scene. Would it be possible to define that layer to show up in all other scenes?

In AS I say

key2_mc._visible = false;
key1_mc._visible =  true;
key1_mc.onPress = function() {
  key2_mc._visible = true;
  key1_mc._visible =  false;
  keylocated = "keyfound"
}

The problem: Once I've grabbed the key, I want it to stay in my inventory, no matter how many times I walk around the three rooms, until I use it on the keyhole in the living room. But currently, after grabbing the key, leaving and re-entering the kitchen, it starts from the beginning. But so far, whenever I enter any of those three rooms, they play from the beginning and don't care what I did in the other roomscenes. So I want to say in AS: if in Scene kitchen key2_mc.visible = true, then in [name of scene, or all other scenes] scene key2_mc = visible. Or in livingroom AS: Check if in scene kitchen key2 was enabled visible. If, then here visible too. Or have one meta AS Valid for all scenes?

Cody Guldner
  • 2,888
  • 1
  • 25
  • 36

1 Answers1

0

Firstly, consider learning how to code using class files. Doing stuff like this using timelines is very cumbersome and can lead to much frustration. This however will be a bit of a time investment and not necessarily helpful at this point for you.

What you need to do, is have global variables (vars that are scoped to lifespan of the application). Flash only knows about what is currently loaded - so if you have a display asset on a scene that is NOT currently loaded, there is no way to access that. Variables however, will last the life of the timeline they are defined on, so if you define something on the root/main timeline, it will stay around for the life of your application.

In your main timeline, you could do this:

var hasKey1:Boolean = false;

This makes a var you can access from anywhere in your application.

Then, on the frame or container movie clip where the key appears or can appear, do this:

key2_mc.visible = MovieClip(root).hasKey1;

And for the one you can grab, do the opposite:

Key1_mc.visible = !MovieClip(root).hasKey1;

When you 'grab' the key, do this:

key1_mc.addEventListener(MouseEvent.CLICK, key1Click, false,0,true)

function key1Click(e:Event):void {
  key2_mc.visible = true;
  key1_mc.visible =  false;
  keylocated = "keyfound"

  MovieClip(root).hasKey1 = true;
}

For as2:

//main timeline
var hasKey1 = false;

//where you first see or can see the key(s)
key2_mc._visible = _root.hasKey1;
key1_mc._visible = !_root.hasKey1;

//when you grab the key
key1_mc.onPress = function() {
  key2_mc._visible = true;
  key1_mc._visible =  false;

  _root.hasKey1 = true;
} 
BadFeelingAboutThis
  • 14,445
  • 2
  • 33
  • 40
  • Thanks! I was using Macr. Flash 8 with AS 2. Beginner mistake! So before I do more stuff, I should switch to Actionscript 3, thus Adobe Flash Professional CC 2015. I shouldn't learn AS2 anymore nowadays from scratch, if I can learn AS3, right? Is Adobe Flash Professional CC 2015 best for that? Is that the right assumption? I am really sorry to bother you with those simple questions!2.Do you think having one room in one scene is the best way afterall? Each scene has background layer, object layer, inventory layer and aslayer. I like the layer system, since I know it from Photoshop. – flashmon1 Dec 11 '15 at 08:05
  • 3.In which actionscript should I paste the global variable thing you wrote? The one of the kitchen? All Actionscripts? Or is there in AS3 maybe a global actionscript? 4.I did not mess with the timeline at all, because in the game there no time running. I am willing to learn about it though, sounds interesting. At this point I don't understand, where my "main timeline" is. Don't worry, I am gonna watch some youtube stuff about AS3 and flash and look for tutorials, in the future it is hopefully less and less stupid questions from my side. I will also look into "class files". – flashmon1 Dec 11 '15 at 08:05
  • Main timeline is just your outer most timeline. If you have movie clips on your timeline, those movies clips then have their own timeline, which could have more movie clips on them with their timelines and so forth – BadFeelingAboutThis Dec 11 '15 at 16:13
  • Your script works, but do I have to paste a copy of key2image in EVERY scene, give it an individual instance, like keylivingroom, keybathroom? Also, in each scenes AS I have to paste that text you gave me and replace the key2 instance keylivingroom instance and so on? I would prefare to have ONE inventory layer which is visible on all scenes (not just the illusion). Because I want to make maybe 8 oder 13 rooms, and modifying the inventory layer for every single scene (room) seems a lot of work. Maybe having an AS layer in each scene is not a good idea affter all? – flashmon1 Dec 13 '15 at 09:13
  • Make it easier by nesting movie clips. So on your main timeline, have the whole playable game on just one frame. On that frame, have a movie clip and inside that movie clip have all your stuff. You could make your key a movie clip, and put the code on it's timeline so that no matter when you have the key that one piece of code will run. – BadFeelingAboutThis Dec 14 '15 at 02:34