[英]Trouble resetting objects in Actionscript 3 / Flash CS5
I'm making a simple platform game in Flash CS5 and Actionscript 3.0. 我正在Flash CS5和Actionscript 3.0中制作一个简单的平台游戏。 When the player loses all their lives, I want it to redirect the player to a "Game over" screen where they can select a "Try again" button to restart the game.
当玩家丧命时,我希望它将其重定向到“游戏结束”屏幕,他们可以在其中选择“重试”按钮来重新启动游戏。
I'm having trouble finding a way to do this. 我很难找到一种方法来做到这一点。 I've been trying to accomplish my goal by doing the following:
我一直在尝试通过执行以下操作来实现自己的目标:
It's probably not a very good way of doing this but if it works, then I'm happy. 这样做可能不是一个很好的方法,但是如果可行,那么我很高兴。 I just want the "Game over" screen to show briefly and if the player clicks the "try again" button, they can play from the start.
我只希望短暂显示“游戏结束”屏幕,如果玩家单击“重试”按钮,他们可以从头开始玩。
Now, the problem with my implementation of the above is that when I set all the gameplay elements to visible = true after having them set to false, the game has stopped.. keyboard input doesn't reactivate and the game elements are shown but aren't active. 现在,上述实现的问题在于,当我将所有游戏元素设置为false后,将所有游戏元素设置为visible = true时,游戏已停止..键盘输入不会重新激活,并且游戏元素会显示但不活跃。 Is there something about the visible attribute I don't know?
关于我不知道的可见属性有什么问题吗? Does it mess with the "state" of an object?
它会干扰对象的“状态”吗?
Here's some snippets of code from the Actionscript file... 这是Actionscript文件中的一些代码片段...
if(lives >= 0) {
//print number of lives
}
else {
gameOverFlag= true;
//hide game objects, show game over menu
Coins.visible = false;
Platforms.visible = false;
Background.visible = false;
StartPosition.visible = false;
thePlayer.visible = false;
GameOver.visible = true; //this is the movie clip with the "Game over" text and "Try again" button in
GameOver.TryAgainButton.addEventListener(MouseEvent.CLICK, playagain);
}
function playagain(event:MouseEvent):void
{
//start game again
Coins.visible = true;
Platforms.visible = true;
Background.visible = true;
StartingPosition.visible = true;
thePlayer.visible = true;
}
This is more of an architecture problem than a code problem. 这更多是架构问题,而不是代码问题。 Personally, I create a
reset()
function on all custom classes. 就个人而言,我在所有自定义类上创建一个
reset()
函数。 Inside this function, I do whatever's needed to set the object in it's default state; 在此函数中,我需要执行任何操作以将对象设置为默认状态。 set position, alpha, visible, custom props etc.
设置位置,alpha,可见,自定义道具等
As for when to call it, it's really up to you, but a good design pattern for you would be the State design pattern. 至于何时调用它,实际上取决于您,但是对于您来说,一种好的设计模式就是状态设计模式。
Basically, you have a StateManager
in your game that holds and controls different State
objects. 基本上,您的游戏中有一个
StateManager
来保存和控制不同的State
对象。 These State
objects can represent the different states of your game; 这些
State
对象可以代表游戏的不同状态。 MainMenu
, Play
, GameOver
, Reset
, etc. MainMenu
, Play
, GameOver
, Reset
等
Each State
would have a begin()
, an end()
and possibly an update()
. 每个
State
都有一个begin()
,一个end()
以及一个update()
。 When your StateManager
switches states, it would call end()
on the State
leaving, and start()
on the State
coming in. You can only be in one State
at a time, so it lets you easily encapsulate your logic depending on where you are in your game. 当你的
StateManager
切换状态,它会调用end()
在State
离开,并start()
对State
的未来,你只能在一个State
的时间,所以它可以让您根据您轻松自己的逻辑封装在你的游戏中。
Inside the begin()
function, you set up everything you need for that particular state. 在
begin()
函数内部,设置了该特定状态所需的一切。 For example, the begin()
function for your Play
state can add all the keyboard/mouse event listeners that you need to control your game. 例如,您的
Play
状态的begin()
函数可以添加控制游戏所需的所有键盘/鼠标事件监听器。 Inside the end()
function, you clear everything that you set up. 在
end()
函数内部,您清除了所有已设置的内容。 In the end()
function for your Play
state, you would remove all the keyboard/mouse event listeners for example. 在
Play
状态的end()
函数中,您将删除所有键盘/鼠标事件监听器。 This would mean that it's impossible for the player to do any play logic unless they're in the Play
state. 这意味着除非玩家处于“
Play
状态,否则玩家无法执行任何播放逻辑。 If you had an update()
function (that's called every frame) in your State
, then you could, in the Play
example, check if the player has no more lives yet, or has reached the score for the next level. 如果您在
State
有一个update()
函数(称为每帧),则可以在Play
示例中检查播放器是否还没有生命,或者是否达到下一个等级。
For the reset logic, in your Reset
state, you could call the reset()
function on all your objects, or set them manually. 对于复位逻辑,在
Reset
状态下,您可以在所有对象上调用reset()
函数,或手动设置它们。 The path through your game with states would look like this: 通过状态进行游戏的路径如下所示:
MainMenu
(play) -> Reset
(or an Init
state) -> Play
-> GameOver
(replay) -> Reset
-> Play
MainMenu
(播放) - > Reset
(或Init
状态) - > Play
- > GameOver
(重播) - > Reset
- > Play
There's no built-in logic to reset object, you'll need to take care of it yourself. 没有内置的逻辑可以重置对象,您需要自己照顾一下。 Adopting a pattern such as this can help with that.
采用这样的模式可以帮助您。
I have had experiences with browsers and platforms giving me slightly different implementations of my show/hide code. 我曾经在浏览器和平台上有过丰富的经验,这些经历使我的显示/隐藏代码的实现略有不同。 Sometimes it's been necessary to move the elements off the stage
(object.x = -3000)
to completely disable them. 有时有必要将元素移出舞台
(object.x = -3000)
以完全禁用它们。 This is not a best practice but it would avoid any conflicts from enabling and disabling objects if you just need to move on with your life! 这不是最佳实践,但是如果您只需要继续前进,它将避免启用和禁用对象时发生任何冲突!
Definitely use a reset() function to wrap up everything that toggles object properties. 绝对使用reset()函数来包装所有切换对象属性的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.