Blast Off!

In this movie varaibles dx and dy have initial values of 0. In the frame event the rockets position is incremented by dx and dy. At start, because dx and dy are both 0, the rocket doesn't move.

When we press the button we assign values to dx and dy. In sebsequent frame events, the rocket will move using the new values of dx and dy.


Get Adobe Flash player

The code for this movie is shown below:

var dx:int=0;
var dy:int=0;

this.addEventListener(Event.ENTER_FRAME,frames);
function frames(e:Event):void {
	//the rocket will not move as long as dx and dy are 0
	rocket.x=rocket.x+dx;
	rocket.y=rocket.y-dy;
} //frames

btnBlastOff.addEventListener(MouseEvent.CLICK, blastOff);
function blastOff(e:MouseEvent):void {
   //after dx and dy are not 0, the rocket will move in the frame event
    dx=2;
	dy=4;
} //blastOff

Once the rocket blasts off, the show is over. Download this movie.
In the next part, we will add code to relaunch the rocket.


INDEX, Variables, Blast Off!, Relaunch and fire, Select a star to spin, Select ANY star to spin
Next lesson: Arithmetic Operations in Actionscript

Copyright © Zebra0.com
All rights reserved worldwide.

 
 

Blast Off!