Learn Create Your Own Split Screen

Next Page

Hopefully you changed all the "3"s to something a little larger than "3". You would have needed to change four "3"s.

That certainly does the job, but as we've mentioned before, it's good coding style not to scatter the same number (when it means the same thing) all throughout your code. It makes it laborious and prone to error if you ever need to change it.

In the code below, I've made a variable which I've chosen to name quickness. It's set to 3, and then used for the velocity setting.

Now, to make the hero move more quickly, you only have to change the "3" in one place, and it's impossible to miss any.


An even better idea is to make the quickness a property of the sprite, so when we create the sprite we might, for example, load an image of a wombat and set the quickness to 1. For another sprite we load an image of a jaguar and set the quickness to 6.

It's good style for objects to know all about themselves. We'll do that in a later session.