Would you like to react to this message? Create an account in a few clicks or log in to continue.

Actionscript [Firing Projectiles]

Go down

Actionscript [Firing Projectiles] Empty Actionscript [Firing Projectiles]

Post by Wingedge Tue Oct 14, 2008 3:38 am

Projectiles are small objects moving at a destined course. You can create projectiles by creating a simple movie clip that will continue to move unless conditions are met.

first we must create a movie clip. Lets name it as missile (instance name)
now we must put a code on it so that it'll move on its own with out interfering in our other objects

[action script for missile movieclip]
Code:

//when the object loads
onClipEvent(load){
  //set it's location same as your mouse location
  this._x = _root._xmouse;
  this._y = _root._ymouse;
}
// move object towards its path
onClipEvent(enterFrame){
  //lets just do a horizontal movement by adding its x location
  this._x +=1;
  // remove this object once it is out of the stage
  if (this._x >= Stage.width){this.removeMovieClip();}
}

Now let us put the script in the mouseDown event.
when the user clicks the mouse, we will create a duplicate of the movie clip missile, and the script inside that movie clip will do the rest. To duplicate, we will use duplicateMovieClip() function

duplicateMovieClip(target,newtarget,depth);

[frame]
Code:

var X:Number = 0; // counter for the movieclip
onMouseDown = function(){
  X++;
  //duplicate missile and name it as missile_(number), in a depth of (number)
  duplicateMovieClip(missile,"_root.missile_"+X,X); 
}

When the mouse is clicked, a duplicate copy of missile is loaded into the stage. then it's (load & enterframe event) triggers therefore it'll appear where your mouse is located and moves towards the east until it is out of the stage.

You can put the hitTest inside your missile object to determine if it hits something.
Wingedge
Wingedge
Admin
Admin

Male
Number of posts : 77
Age : 40
Name : Sir Francis
Year Level : Instructor
Location : UC
Registration date : 2008-06-10

Credits
GP: 9999

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum