使用“R”重置游戏钥匙
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我尝试了代码的键盘处理部分,效果很好。尝试跟踪某些内容以查看该部分在您的代码中是否有效。如果这部分没问题,你可能无法到达正确的时间线,或者类似的事情。这部分代码还不足以说明。
无论如何,使用 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
如果对游戏一无所知,就很难判断自己真正需要做什么;但重置游戏可能是最难完成的部分之一。
一旦您开始添加对象等,它们在更改框架后不会全部被删除和清理。您必须删除已创建的所有对象、对这些对象的所有引用以及将它们与内存联系在一起的任何其他内容(例如事件监听器)。
这是您从项目一开始就真正需要牢记的事情,并在整个开发阶段进行测试。
一个好的策略:
remove()
方法处理删除。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:
remove()
method.remove()
everything at the end.So a snippet of your base class would look like:
And a basic manager:
Then in your classes that extend GameObject you'll just have to add logic to the
remove()
method for removing eventListeners and so on.