开发者

Reseting a game using the 'R' Key

I am trying to make a game in Flash using Action Script 3,

I have everything in the game and the game works, but I am trying to add a restart function so the game will go back to the start of the level when you pres the 'R' key.

My game starts off with a title screen on Frame 1, and there are 2 levels, 1 is in frame 2 and the other is in frame 3.

When you click the level you want on the title screen it takes you to the frame which has the required level in it by using a gotoAndStop

function click_handler(event:MouseEvent) :void
{
    gotoAndStop(2);
}

But what I want it to do is when I press 'R' i want it to reload everything in frame 2, what I have tried is

stage.addEventListener(KeyboardEvent.KEY_DOWN, resetGame);

function resetGame(e:KeyboardEvent):void
{
        if (e.keyCo开发者_运维问答de == 82)
    {
        gotoAndStop(2);
    }
}

But this does not seem to work.

If anyone can tell me the correct way of doing this I would be very greatful.


I tried the keyboard handling part of your code, and it works fine. Try trace something to see this part works or not in your code. If this part is fine, you maybe can't reach the correct timeline, or something like this. These part of codes not enough to tell.

Anyway, more clear using Keyboard.R instead 82.

R,

Tamas Gronas


It's hard to tell what you really need to do without knowing anything about your game; but resetting a game can be one of the harder parts to accomplish.

Once you start adding objects and such, they aren't all removed and cleaned up once you change frame. You must remove all objects that you've created, all reference to these objects and anything else that ties them to memory like eventListeners.

This is something you really need to have in mind right from the beginning of your project and test throughout its development stage.

A good strategy:

  1. Have a base class for all of your game objects which will handle removal via a remove() method.
  2. Have a core class that stores a list of all your game objects so that you can loop through and remove() everything at the end.

So a snippet of your base class would look like:

package
{
    import flash.display.DisplayObject;

    public class GameObject extends Object
    {
        // vars
        public var skin:DisplayObject;

        /**
         * Constructor
         */
        public function GameObject()
        {
            Manager.entire.push(this);
        }

        /**
         * Removes this
         */
        public function remove():void
        {
            if(skin.parent) skin.parent.removeChild(skin);

            var ix:uint = Manager.entire.indexOf(this);
            Manager.entire.splice(ix, 1);
        }
    }
}

And a basic manager:

package
{
    public class Manager
    {
        public static var entire:Array = [];

        /**
         * Removes all instances of GameObject
         */
        public static function removeAll():void
        {
            var i:GameObject;
            for each(i in entire)
            {
                i.remove();
            }
        }
    }
}

Then in your classes that extend GameObject you'll just have to add logic to the remove() method for removing eventListeners and so on.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜