开发者

Trouble resetting objects in Actionscript 3 / Flash CS5

开发者 https://www.devze.com 2023-02-17 01:44 出处:网络
I\'m making a simple platform game in Flash CS5 and 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 ag

I'm making a simple platform game in Flash CS5 and 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:

  • Set everything (player, background, etc) to visible = false
  • Set "Game over" movie clip to visible = true (it is invisible during gameplay)
  • Have the button in the "Game over" movie clip hide the movie clip then re-show all the gameplay elements
  • Reset position of player to start, set score to 0, lives to 3开发者_如何学JAVA, etc..

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

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. Inside this function, I do whatever's needed to set the object in it's default state; set position, alpha, visible, custom props etc.

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. These State objects can represent the different states of your game; MainMenu, Play, GameOver, Reset, etc.

Each State would have a begin(), an end() and possibly an 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.

Inside the begin() function, you set up everything you need for that particular state. For example, the begin() function for your Play state can add all the keyboard/mouse event listeners that you need to control your game. Inside the end() function, you clear everything that you set up. In the end() function for your Play state, you would remove all the keyboard/mouse event listeners for example. This would mean that it's impossible for the player to do any play logic unless they're in the Play state. 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.

For the reset logic, in your Reset state, you could call the reset() function on all your objects, or set them manually. The path through your game with states would look like this:

MainMenu (play) -> Reset (or an Init state) -> Play -> GameOver (replay) -> 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. 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.

0

精彩评论

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