Syed
Syed

Reputation: 79

AS3 How to communicate between frames

I have been writing a game in timeline code. I want the different frames (rooms) in the game to be able to share information between each other. Of course, timeline code is limited to the frame it is written in.

After doing quite a bit of reading ("Foundation Game Design with Flash" and a number of articles, tutorials, forums etc) I decided to employ a document class. This did not work either. It seems it only works for frame one but not the rest of the frames (I have four).

How can I have frame four respond to something that happpened in frame one? For example, if the player achieves something in frame one, I want a movie clip in frame four to be visible.

Upvotes: 0

Views: 3062

Answers (3)

adamfsk
adamfsk

Reputation: 377

You may find the simplest way to link everything with the document class is to move your four frames into a movieclip together and have that on the first frame, then interact with that movieclip.

E.g. in the document class, where the movieclip instance on the timeline is called 'game'.

game.gotoAndStop(4);
game.objectToDisplay.visible = true;

If you encounter reference errors in the IDE then you can avoid these by using [] notation to refer to the properties of game, e.g. game["objectToDisplay"].visible = true;

Note that it's not really best practice to do this, but it will at least help you to finish that first game which is really more important at this stage in your learning. Afterwards, if you want to learn more then I'd recommend "The Essential Guide to Flash Games" by Jeff Fulton from 8bitrocket.com - it will teach you how to use the document class effectively.

Upvotes: 1

dexterous
dexterous

Reputation: 111

If You are writing your code on the timeline, My suggestion would be to create two layers in the timeline, one for 'frame-actions' - in this layer you insert the code specific to a single frame (will work when the movieclip is stopped on that particular frame).. And also create one more layer called global-actions (for the entire timeline). Only the first frame will be a key frame and there should be empty frames till the end of the timeline. In this layer actions write the code that you want to access from any keyframe in the same timeline.

If you define a variable in the actions which are written for the whole timeline (global-actions) then that will be available on all the frames.

Now if you want to go to a different frame based on some action, just write some functions in the layer which contains global actions and call that particular function through the frame actions. To go to a different frame use the 'gotoAndStop(frameNumber)' function of flash.

I want to tell you that while it will work, I would not recommend using it in this way.

HTH.

Upvotes: 2

MickMalone1983
MickMalone1983

Reputation: 1054

You can use static variables - these are variables which are linked to a class, rather than an instance of it.

Suppose your document class was called Document.as, and you wanted a variable, playerLives, to be visible from any part of the program.

Declare it inside Document.as:

public static var playerLives:int = 3;

You can then reference this directly from anywhere else in your code with:

Document.playerLives

(note that the variable is a member of the class itself, not an instance of it).

You could use a dedicated Statics class to hold these variables if you want to keep your document neat, or attach them to the relevant classes (eg Player.lives)

I've not used timeline/frames for some years but I believe this is how I used to do it!

NB Statics will be fine for your purposes but they are, in some ways, an equivalent to the _global variable in AS2 (at least, they can be used in the same manner) - many would not approve of their use, or over-use, as they are freely accessible from anywhere in your program (thus anathema to the OO concept of encapsulation), but personally I try not to worry about it in small cases - the most important thing to know about the rules of any design pattern is when they can be broken!

They are also slightly slower to access than instance members, but you won't notice this unless you are constantly accessing/changing them (making things like player velocity, which will need to be referenced/changed every frame, static, is not a good idea).

Hope this helps.

Upvotes: 1

Related Questions