flmobimooc – week 5

Posted on March 24, 2014 | Category :MOOC | 10 Comments on flmobimooc – week 5

I have added sound to the game. You can see the method used here.

I have also added lives, and the ball speeds up when ever you score.

To add lives I first declare a variable mLives. I then set mLives to 3 in setupBeginning()

To show the remaining lives, I use a for loop in doDraw and place a red ball in the top left corner of the screen in a row, 20 pixels from the top and spaced 30 pixels apart (with an offset of 5 pixels from the left)

In game update, instead of losing the game, I remove one life every time you miss the ball. I then check if the number of lives is 0. If it is, I use the same old setState(GameThread.STATE_PAUSE); code.If we still have some lives left, I set the state of the game to Paused. I then place the ball in the centre of the screen and reset the speed to the same as the initial speed when the game starts.

To increase the ball speed is very easy.

When you check to see if the ball has hit the smiley face in the updateGame method, add the following code to increase the ball speed 5%.

//using the fraction between the original velocity and present velocity to calculate the needed
//speeds in X and Y to get the original velocity but with the new angle.
mBallSpeedX = mBallSpeedX * velocityOfBall / newVelocity;
mBallSpeedY = mBallSpeedY * velocityOfBall / newVelocity;

// Code added to speed up the ball.
// These are the only 2 lines required. Everything else is standard
// Speed up ball by 5% on every score
mBallSpeedX = mBallSpeedX * 1.05f;
mBallSpeedY = mBallSpeedY * 1.05f;

//Increase score
updateScore(1);

Take a look at my current TheGame.java file with all the updates so far. I have tried to add comments to explain what everything is doing.

Remember, if you want to add sound, follow the instructions on this page and download the file with the sounds from there, or add your own.

Files for week 5

These are the only files that differ from the originals supplied by Karsten. I have changed various parts for different features. Everthing should be commented. Right click to save the files or take a look at the Github Gists.

Please comment below if I have missed anything, or get it wrong.

Thanks.

Comments 10

  1. Philip Saunders Reply
    14/03/24

    Excellent stuff.

    Any chance of having a copy of your:-

    TheGame (game view) where you’ve added the sound.
    TheGame.java where you’ve added the sound.

  2. Philip Saunders Reply
    14/03/24

    Sorry I have the TheGame.java just need the game view bit

  3. Rob Rob Reply
    14/03/24

    I have now link all the files I have changed at the bottom of the page. There is a gist link and a direct download link.
    I think it should be all you need but if you hit any problems let me know.

  4. Philip Saunders Reply
    14/03/24

    I get a error:-

    The field GameThread.mcontent is not visible

    Thanks for the links.

  5. Rob Rob Reply
    14/03/24

    I think it should read mContext.
    Which file is that in ?

  6. Philip Saunders Reply
    14/03/25

    TheGame.Java

    TheGame(gameview)

  7. Rob Rob Reply
    14/03/25

    Hi Philip.
    I appologise. I managed to post a link to the wrong file for TheGame.java. I have now updated that.
    At the end of the post you should now see a link to my “TheGame.java” file.
    The GameView.java I use is unchanged from the origional supplied with the course.
    I can link it, but there are no differences.
    I have changed one line in the “GameView.java” so that file is also posted.
    Those two are the only source files I have edited to use the SoundPool Library in the game.

    The method I usd to get the ound going is documented here http://www.robsbots.org.uk/flmobigame/

    Let me know if you get it going, or run into any problems.

    Thanks

    Rob

  8. Philip Saunders Reply
    14/03/25

    tired the links again and same problem..

    can you email me and I send you a screenshot (unable to add files to comments)

    • Rob Rob Reply
      14/03/25

      Email on its way. I’ll use the address you posted here with.

      Email me a link to a pastebin with the code and a screenshot on postimage of the errors if you like.

  9. Philip Saunders Reply
    14/03/25

    All resolved thanks mate.

Leave a Reply

Your email address will not be published. Required fields are marked *

*