26.02.2015

Begin Programming: Solution to week 2’s challenges

posted by Karsten

Comments Off on Begin Programming: Solution to week 2’s challenges

SPOILER ALERT: This post will show you how to move the ball at double speed, and how to move the ball directly to where you clicked on the screen.

 

There are several ways to move the ball at double speed. I think it is best to do this in the updateGame method:

26-02-2015 13-44-09 updateGame

Here the position of the ball is changed depending on the speed of the ball. If that is multiplied by two, then it will move faster:

26-02-2015 13-44-52 updateGame after

This would do the trick! Notice that I don’t need brackets because the precedence of the *’s mean that they will be performed before +.

However there is a small problem. Now the mBallSpeedX and mBallSpeedY actually aren’t reflecting pixel/sec any more. If you want to keep that then you need to multiply the evaluation of mBallSpeedX and mBallSpeedY, for instance in actionOnTouch:

26-02-2015 13-49-28 actionOnTouch

You could change that to:

26-02-2015 13-48-46 actionOnTouch after

 

Notice I use brackets to enforce that the – operation happens before the *.

You would also need to change in actionWhenPhoneMoved to:

26-02-2015 14-12-22 actionWhenPhoneMoved

 

To change the ball’s behaviour to move instantly is actually simpler. In actionOnTouch you would need to move the ball directly to the x and y position of the touch. You do that this way:

26-02-2015 13-50-29 actionOnTouch move

 

 

We prefer discussions about the code in FutureLearn, so I will not publish comments here. I hope you understand 😉

Share

Comments are closed.