使用“R”重置游戏钥匙

发布于 2024-11-09 15:48:43 字数 619 浏览 0 评论 0原文

我正在尝试使用 Action Script 3 在 Flash 中制作游戏,

我拥有游戏中的所有内容并且游戏可以运行,但我正在尝试添加重新启动功能,以便当您按下“R”键。

我的游戏从第 1 帧上的标题屏幕开始,有 2 个关卡,1 个位于第 2 帧中,另一个位于第 3 帧中。

当您在标题屏幕上单击所需的关卡时,它会将您带到具有以下内容的帧:通过使用 gotoAndStop 达到所需的级别

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

但我希望它做的是当我按“R”时我希望它重新加载第 2 帧中的所有内容,我尝试过的是

stage.addEventListener(KeyboardEvent.KEY_DOWN, resetGame);

function resetGame(e:KeyboardEvent):void
{
        if (e.keyCode == 82)
    {
        gotoAndStop(2);
    }
}

但这似乎不起作用。

如果有人能告诉我这样做的正确方法,我将非常感激。

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.keyCode == 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

巴黎盛开的樱花 2024-11-16 15:48:43

我尝试了代码的键盘处理部分,效果很好。尝试跟踪某些内容以查看该部分在您的代码中是否有效。如果这部分没问题,你可能无法到达正确的时间线,或者类似的事情。这部分代码还不足以说明。

无论如何,使用 Keyboard.R 代替 82.

R 更清晰,

Tamas Gronas

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

萌化 2024-11-16 15:48:43

如果对游戏一无所知,就很难判断自己真正需要做什么;但重置游戏可能是最难完成的部分之一。

一旦您开始添加对象等,它们在更改框架后不会全部被删除和清理。您必须删除已创建的所有对象、对这些对象的所有引用以及将它们与内存联系在一起的任何其他内容(例如事件监听器)。

这是您从项目一开始就真正需要牢记的事情,并在整个开发阶段进行测试。

一个好的策略:

  1. 为所有游戏对象建立一个基类,该基类将通过 remove() 方法处理删除。
  2. 有一个核心类来存储所有游戏对象的列表,以便您可以循环遍历并在最后remove()所有内容。

因此,您的基类的片段如下所示:

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);
        }
    }
}

和一个基本管理器:

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();
            }
        }
    }
}

然后在扩展 GameObject 的类中,您只需向 remove() 方法添加逻辑即可删除 eventListener 等。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文