Adding Lives to Breakout Game
Praenor

Hello, I am relatively new to Wade and Clockwork Chilli and started with the Breakout tutorial. I am trying to add features to it as part of my learning curve but seem to be stuck with adding lives. I have managed to get the ball to delete instead of going to the start screen and reduce the lives count by 1 but I cannot wrap my head around the next step which involves getting the ball to respawn and start moving again.I feel I am close but each thing I try is not working or breaks the code. I think that making the ball a template would help but the again I am having trouble with where I should go from there. I haven't seen any tutorials on adding lives to a game yet so I am reaching out for some direction. Right now the below code snipets from the app.js file is all and from the ballObject onUpdate are I have that works. The game coding is the same as the 3 tutorials minus the addition of the score coding i found elsewhere in the forums. I am hoping someone can point me in the right direction. 

***app.js snippet***

    this.updateLives = function(die)
    {
        this.lives += die;
        wade.getSceneObject("livesObject").getSprite().setText(this.lives);
        wade.removeSceneObject("ballObject");

    };

***onUpdate - ballObject snippet***

if(pos.y-size.y < -halfHeight || pos.y+size.x > halfHeight)
{
     this.setVelocity({x:this.getVelocity().x, y:this.getVelocity().y*-1});
     if(pos.y+size.y > halfHeight)
     {
        //Game over
        wade.app.updateLives(-1);
        //wade.clearScene();
        //wade.loadScene('scene1.wsc', true, function()
        {
            // the scene has been loaded, do something here
        }//);
     }
}

All 2 Comments
foxcode

Hi Praenor,
It's a long time since I made that tutorial but I think I can help you :)

Let's use your code, starting with this.updateLives = function(die)

Deleting the ball is one approach, though I feel it is uncessary. The ball is a very simple object, I believe the only properties it has that change are position and velocity. This means that instead of destroying and recreating the ball, we can just move it back to it's initial position and restore it's original velocity.

So we could instead try this

this.updateLives = function(die)
{
    this.lives += die;
    wade.getSceneObject("livesObject").getSprite().setText(this.lives);
    _.ballObject.init(); // shorthand for wade.getSceneObject("ballObject").init();
};

Notice the _.ballObject.init function. This function does not exist yet but we can create it. The tutorial already contains code to initialise the ball, but it's pretty poor, my bad. Before we create this init function, click on the ball and select the onAddToScene function. Unless you have modified it, you should see this
 

var velocity = {x:Math.random() < 0.5 ? this.speed : -this.speed,
                y:Math.random() < 0.5 ? this.speed : -this.speed};
                
this.setVelocity(velocity);


This is what happens when the ball is first added to the scene. We need this to happen every time we lose a life, so click the add function button, and create a function called init. Cut and paste everything from the onAddToScene function, into our new init function. We still need onAddToScene to run its code, so lets call our init function we just made. onAddToScene should now look like this

this.init();

Recall that we are also calling this init function in the modified updateLives function. Every time a life is decremented, we call the balls initialise function, however there is still a problem. You may have noticed that the initialise function does not set the balls position. Currently the balls position can be found in the properties tab for the ball. In the tutorial this is set to x=0, y=100. You can set whaterver values you want, but we need to preseve this so lets modify our init function again. It should now look like this.

var velocity = {x:Math.random() < 0.5 ? this.speed : -this.speed,
                y:Math.random() < 0.5 ? this.speed : -this.speed};
                
this.setVelocity(velocity);

this.setPosition(0, 100); // You can use your own numbers here

We have added a call to setPosition.

You should now have a ball that correctly repositions when you call your update lives function, which is your key issue if I read your post correct.


One thing I notice that's missing from your question is what happens when the lives get to zero. The easiest change is to proabably do this, though it could be structured more neatly.
 

***onUpdate - ballObject snippet***

if(pos.y-size.y < -halfHeight || pos.y+size.x > halfHeight)
{
    this.setVelocity({x:this.getVelocity().x, y:this.getVelocity().y*-1});
    if(pos.y+size.y > halfHeight)
    {
        wade.app.updateLives(-1); // Update lives counter
        if(wade.app.lives <= 0) //Game over
        {
            wade.clearScene();
            wade.loadScene('scene1.wsc', true, function(){});
        }
    }
}



I hope this helps. If you have any problems with this or need more help, just reply below. Good luck

Praenor

Thank you! That worked and is exactly what I was looking for it to do. Also thank you for the last bit as that was my next task lol. I was closer than I though but I am glad I asked so now others my view this if they need help with life counters.

Post a reply
Add Attachment
Submit Reply
Login to Reply