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.

We call the same function, selectStar, when any of the stars is clicked, then use information about the MouseEvent to determine which star was clicked and assign it to star.

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, selectStar);
greenStar.addEventListener(MouseEvent.CLICK, selectStar);
redStar.addEventListener(MouseEvent.CLICK, selectStar);
purpleStar.addEventListener(MouseEvent.CLICK, selectStar);
yellowStar.addEventListener(MouseEvent.CLICK, selectStar);
function frameHandler(e:Event):void
{
	star.rotation+=speed;
}

function selectStar(e:MouseEvent):void
{
	star=this[e.target.name]; //whichever star was clicked
	speed*=-1;
}
Download this movie.

NEXT: Arithmetic Operations in ActionScript3