Smooth move or animation (with frame + swap)

Hi there !

Here is a sample game that shows how to smoothly move a sprite or an item across the screen.
It's a star field, where each star tile smoothly goes to the left.

The idea is to use the frame command to tell each star to show its next animation image (the animation mimics a right to left movement within 8x8 pixels), and when the animation is over, display the star tile to the left and go on with the animation.

All is commented in the source.
Star (2.6 KB)

star anim

In the Game script:

on load do
	starInterval = 2 // frames between each animation (0 = fast // 10+ = slow)
	starMaxFrame = 8 // number of animation frames for each star tile
	starCurFrame = 0 // current frame of each star tile
	frameTimer = 0 // current frame number, reset each "starInterval" frames
	starCurFrame = 0 // id of the current animation frame of each star tile

on loop do // automatically called 20x per second
	if frameTimer>=starInterval then // wait is over, lets do animate or move star
		frameTimer = 0 // reset timer
		starCurFrame++ // add 1 to the current animation frame of star tiles
		if starCurFrame>=starMaxFrame then // 1 anim cycle is over 'move star to the left
			starCurFrame = 0
			emit "starMoveLeft"
		else // animate star (= small left movement illusion)
			emit "starAnimate"

In the Star (item) script:

on starMoveLeft do // move the current star tile 1 square to the left
	newX = event.x
	if newX>0 then // still displayed ?
		newY = event.y
		swap "black"
		tell newX,newY to
			swap "star"
	else // the star touched the left side, let's display it to the right
		call "starRestart"

on starAnimate do // change the animation frame
	frame starCurFrame

on starRestart do
	swap "black" // hide the star that touched the left side
	tell 23,event.y to // respawn it to the right of the same line
		swap "star"

The global speed of the animation is set with starInterval. It's the number of frames during which no animation is played. (Pulp calls loop 20x per second, to give an idea)
So, each time we have spent starInterval frames in the loop event, we activate the next animation step: 1) display the next frame of each star tile or, if we are at the last frame, 2) move the star tile to the left, using swap command.


do you have to rename star to whatever your sprite is?