开发者

how to handle multiple game modes in actionscript 3 - flash

I'm new to actionscript flash 3.0. Anyways, i have written code for one whole game level including the character. Is it now possible to create a class, and when i press 开发者_如何学Cfor e.g. 'Level 1' the first level starts?(create an object of the class) When i press quit, to unload the whole level. How is this possible in flash? are there any tutorials? thanks.


The joys of OOP. ANY time you code in an object orientated language you need to develop your classes for re-usability. After all that is one of the main goals for OOP design. You did not post any code so I am going to assume you wrote your game in a procedural way and did not use and classes besides the base classes supplied to you by flash packages.

As an example Since we are talking about games think of a bullet. You can make a bullet class that will control all aspects of any single bullet( from hitting a target to its trajectory to flying off into space. so when the user fires a round you would create a new instance of the bullet and assign an event listener to it for something like "HIT" of course you would have to dispatch the event in the bullet class, but at this point the only think you would need to worry about is if a hit event happened, the bullet class takes care of everything else.
Basically, as you can see doing it this way encapsulates any action with bullet aside from the HIT event.

An other example would be a Button. This class comes with the flash libraries and I am sure you have used it before. Drag a button onto stage add an eventlistener make an onclick function and you are done. None of the internal methods of the Button class need to be played with, it's all encapsulated nicely for you.
Kirupa is always a good place to learn LINK
Be prepared to rewriting your entire game for this change. Porting procedural code into classes can be a hair pulled experience


Short answer: yes.

A simple way (not necessarily the best way) is to add mousedown event handlers to your button objects that trigger a function which constructs your level (by creating a new object instance of your class). You can then access this object through its reference (i.e. variable name).

After you're done with the level, you can set the object reference to null, which will let the garbage collector clean up your memory.

There are much more elegant ways to do this, but try this as a basic starting point. Good luck.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜