website promotion banner
eturnkeys
Your Ad Here
Flash & Swish  Home Flash & Swish Swish Tutorials Shooting An Object
rss

Shooting An Object

Author: Craig Lowe More by this author


Example:

Click on the above example and use the following controls.

image 1

Open swishmax, i have used 350x350 for the movie size for this tutorial.

First of all the purpose of this tutorial is to get one object to shoot another. If you wish to have the stars as your background follow this tutorial first.

Step 1

Create a ship to fire the object, use the tools within swishmax or import a previously made image.

Click on transform and make sure the anchor point is set to top center

Group the shape or shapes as a sprite and name the sprite ship, and add the following code to the sprite.

onLoad () {
_root.yy=this._y;
moveSpeed=5;
_root.speed=15;
}
onEnterFrame() {
if (_root.a==3) {
_root.n=0;
_root.a=0;
}
_root.xx=this._x;
if (Key.isDown(Key.RIGHT)) {
this._x+=moveSpeed;
} else if (Key.isDown(Key.LEFT)) {
this._x-=moveSpeed,0;
}
if (this._x<25) {
this._x=25;
}
if (this._x>330) {
this._x=330;
}
}
on (keyPress("< Space >")) {
_root.n++;
if (_root.n<=3) {
shoot.gotoAndPlay(2);
weapon.duplicateSprite("new"+_root.n,10+_root.n);
xnew=eval("new"+_root.n);
xnew._x=_root.xx;
xnew._y=_root.yy;
xnew.yes=1;
}
}

Now Press Ctrl+t to test your movie in player, your ship should now move left and right.

Step 2

Now create a weapon for the ship to shoot, again use the tools within swishmax or import a ready made one.

Again click on transform and make sure the anchor point is set to top center

Group the shape or shapes together as a sprite and name the sprite weapon and add the following code to the sprite.

onEnterFrame() {
if (yes==1) {
if (_parent.Enemy.isNearThis()) {
_parent.Enemy.explode.gotoAndPlay(3);
}
}
if (yes==1) {
this._y-=_root.speed;
if (this._y<1) {
_root.a+=1;
this.RemoveSprite();
}
}
}

Now move the weapon sprite off stage to the left so it cannot be seen in the movie window.

That's it for this tutorial if you press Ctrl+T your ship should now shoot a weapon.

For the weapon to destroy something you will need to follow the Collision Detection tutorial.




Rate this Material: Bad 1 2 3 4 5 Excellent
print this page tell a friend subscribe to newsletter subscribe to rss

Add comments to "Shooting An Object"