Variables in ActionScript3

In this movie a variable star is declared as a MovieClip with an initial value of redStar. In the frame event star is rotated. If the value of star is redStar, then the redStar rotates, but if the value of star is blueStar, then the blueStar rotates. Whenever we click on one of the stars, it becomes the value of star. Also, whenever one of the stars is clicked, we multiple the value of speed by -1 so that the speed changes from +5 to -5 and back to +5 again. This makes the star spin in the opposite direction.

Get Adobe Flash player

The code for this movie is shown below:

var star:MovieClip=redStar;
var speed:int=5;
addEventListener(Event.ENTER_FRAME, frameHandler);
blueStar.addEventListener(MouseEvent.CLICK, selectBlueStar);
redStar.addEventListener(MouseEvent.CLICK, selectRedStar);
function frameHandler(e:Event):void
{
	star.rotation+=speed;
}

function selectBlueStar(e:MouseEvent):void
{
	star=blueStar;
	speed*=-1;
}
function selectRedStar(e:MouseEvent):void
{
	star=redStar;
	speed*=-1;
}

Download this movie.

NEXT: Select ANY star to spin