Search This Blog

12/27/09

Tutorial 3:Moving an Object in a Rectanguler Path

After knowing the basic flash co-ordinate system, now we are going to move an object in a rectangular path, the easiest one among the closed paths.
Let's assume the co-ordinates of the end point of the rectangle are (x1,y1),(x2,y1),(x2,y2) and (x1,y2) respectively.I hope that u can do a little arithmetic to find out the width and height of the rectangle and place it properly on the flash stage.If u can't ask me.
Draw an arrowhead so that u can see the rotation at each point and convert it to a movieclip. No need to give an instance name as we are going to place the action on the clip itself. Select the arrowhead and press F9 to bring in the script panel and attach the following script...


onClipEvent (load) {
// initialize the coordinate variables
var x1:Number = 200;
var y1:Number = 100;
var x2:Number = 500;
var y2:Number = 300;
//u can always change the numbers to create rectangles of
//different widh and height
// initialize two other variables
var i:Number = 0;
var j:Number = 0;
//set the speed
var speed:Number = 10;
//set the initial position of the arrow
this._x = x1;
this._y = y1;
}
onClipEvent (enterFrame) {
// set the conditin to move
if (this._x>=x1 && this._y<=y1) { this._x = this._x+speed; this._y = y1; this._rotation = 360; } if (this._x>=x2 && this._y>=y1) {
this._x = x2;
this._y = this._y+speed;
this._rotation = 90;
}
if (this._x<=x2 && this._y>=y2) {
this._y = y2;
this._x = this._x-speed;
this._rotation = 180;
}
if (this._x == x1 && this._y<=y2) { this._x = x1; this._y = this._y-speed; this._rotation = 270; } }

here is the movie


Test your movie.
Try to understand the conditional logic that is all important here.Questions and comments are welcome.

No comments:

Post a Comment