Skip to content

Instantly share code, notes, and snippets.

@ortonomy
Created March 5, 2014 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ortonomy/9370186 to your computer and use it in GitHub Desktop.
Save ortonomy/9370186 to your computer and use it in GitHub Desktop.
FLMobiGame Week 2 Complete
@Override
protected void actionOnTouch(float x, float y) {
mLastX = x;
mLastY = y;
//Increase/decrease the speed of the ball making the ball move towards the touch
mBallSpeedX = ( x - mBallX ) * 2; // will double the speed of the ball
mBallSpeedY = ( y - mBallY ) * 2;
}
//This is run just before the game "scenario" is printed on the screen
@Override
protected void updateGame(float secondsElapsed) {
// will make the ball stop under a tap
/*if ( ( mBallSpeedX < 0 && ( mBallX + secondsElapsed * mBallSpeedX ) < mLastX ) || ( mBallSpeedX > 0 && ( mBallX + secondsElapsed * mBallSpeedX ) > mLastX ) )
{
mBallSpeedX = 0;
mBallSpeedY = 0;
}*/
// will make the ball "bounce" of the "walls" , or in other words, the screen edges!
if ( mBallSpeedX < 0 && ( ( mBallX + secondsElapsed * mBallSpeedX ) < 0 ) ) {
mBallSpeedX = -mBallSpeedX;
}
else if ( mBallSpeedY < 0 && ( ( mBallY + secondsElapsed * mBallSpeedY ) < 0 ) ) {
mBallSpeedY = -mBallSpeedY;
}
else if ( mBallSpeedY > 0 && ( ( mBallY + secondsElapsed * mBallSpeedY ) > mCanvasHeight ) ) {
mBallSpeedY = -mBallSpeedY;
}
else if ( mBallSpeedX > 0 && ( ( mBallX + secondsElapsed * mBallSpeedX ) > mCanvasWidth ) ) {
mBallSpeedX = -mBallSpeedX;
}
//Move the ball's X and Y using the speed (pixel/sec)
mBallX = mBallX + secondsElapsed * mBallSpeedX;
mBallY = mBallY + secondsElapsed * mBallSpeedY;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment