Saturday, September 15, 2012

Scrolling background

Today's idea is to make a sprite appear to move in a never ending background, where in fact the background is moving and two background sprites alternate.

I used a picture of Nemo and modified it so that the fins can move.

  The code for Nemo simply makes it face in the direction it is moving and changes the costume to make it look like the fins are moving

The code for the stage makes use of a variable scrollx.  The value of scrollx changes as you press the right or left arrow keys.  Scrollx can get very big or very small so you work out how many cycles you have completed to subtract that from scrollx to set the x position of the two background sprites.
The if statement to set repeat determines which of the two background sprites will be on the left and which will be on the right.

 You need two background images that can be joined on either side 

The  code for the one background image is
and the code for the other background image is 
This ensures that the distance of the x position of the two background sprites is always 480 apart.(the width of the stage.  You can make it a little less to prevent a opening between the two background images

You can download the program from

No comments:

Post a Comment