Go back to How To Make A Simple Platform Game In Flash
Since we now have the player and the enemy, we need to give the player a weapon to fight the enemy.
1. Draw a simple sprite for Ninja Star. Example below:
Name it as “NinjaStar.png”, and put it in org/flixel/data folder.
2. Next is to create a “spark” sprite when the Ninja Star hits some objects.
Name this file “Spark.png” and place it in the data directory as well.
3. Create a new Class and name it “NinjaStar.as”, then import Flixel as usual and have it extend FlxSprite.
4. Then, embed the Ninja Star and Spark graphics:
package com.Tutorial { /** * ... * @author pisces_eyes */ import org.flixel.*; public class NinjaStar extends FlxSprite { [Embed(source='../../org/flixel/data/NinjaStar.png')] private var ImgStar:Class; [Embed(source='../../org/flixel/data/Spark.png')] private var ImgSpark:Class;
5. Declare a _sparks variable setup to be as FlxEmitter used for special effect to our star.
6. And add the constructor’s arguments to look like this:
private var _sparks:FlxEmitter; public function NinjaStar(X:Number, Y:Number, XVelocity:Number, YVelocity:Number, Sparks:FlxEmitter ):void {
7. Inside the constructor, call super() , and setup various settings:
super(X, Y); loadGraphic(ImgStar, true, true, 16, 16); //Basic movement speeds maxVelocity.x = 200; //How fast left and right it can travel maxVelocity.y = 200; //How fast up and down it can travel angularVelocity = 100; //How many degrees the object rotates //bounding box tweaks width = 5; //Width of the bounding box height = 5; //Height of the bounding box offset.x = 6; //Where in the sprite the bounding box starts on the X axis offset.y = 6; //Where in the sprite the bounding box starts on the Y axis addAnimation("normal", [0]); //Create and name and animation "normal" //This is the particle emitter that spews things off the bottom of the screen. //I'm not going to go over it in too much detail here, but basically we // create the emitter, then we create 50 16x16 sprites and add them to it. _sparks = Sparks; //The object now is removed from the render and update functions. It returns only when reset is called. //We do this so we can precreate several instances of this object to help speed things up a little exists = false;
This should be familiar to you by now which was used from previous FlxSprites. The FlxEmitter is an object used to actually create a group of sprites at once when we call it using the ImgSpark graphic so to look like an explosion of sparks. There are many neat effects you can do with FlxEmitters.
8. Then, let's override some of the FlxSprites functions so that when the Ninja Star hits the wall or some objects, it'll call the “Kill” function, and kill itself.
override public function hitBottom(Contact:FlxObject, Velocity:Number):void { kill(); return;// super.hitBottom(Contact, Velocity); } override public function hitLeft(Contact:FlxObject, Velocity:Number):void { kill(); return;// super.hitLeft(Contact, Velocity); } override public function hitRight(Contact:FlxObject, Velocity:Number):void { kill(); return;// super.hitRight(Contact, Velocity); } override public function hitTop(Contact:FlxObject, Velocity:Number):void { kill(); return;// super.hitTop(Contact, Velocity); }
9. Then, FlxSprite has a kill() function which immediately kills the sprite, regardless of any health they might have. Let's override this function to let the Ninja Star explodes (using the Spark FlxEmitter):
override public function kill():void { _sparks.at(this); _sparks.start(true,0,20); super.kill(); }
10. Lastly, to save resources, let the system ‘reuse’ the dead Ninja Stars and put them back in the game, so let's create a ‘reset’ function :
public function resetStar(X:Number,Y:Number,XVelocity:Number,YVelocity:Number):void { x = X; y = Y; dead = false; exists = true; visible = true; velocity.x = XVelocity; //Set the left and right speed play("normal"); //Play the animation }
This will just ‘reset’ to initial state when you kill a sprite so that the game can use the Ninja Star again.
Here's the complete NinjaStar.as:
package com.Tutorial { /** * ... * @author pisces_eyes */ import org.flixel.*; public class NinjaStar extends FlxSprite { [Embed(source='../../org/flixel/data/NinjaStar.png')] private var ImgStar:Class; [Embed(source='../../org/flixel/data/Spark.png')] private var ImgSpark:Class; private var _sparks:FlxEmitter; private var _gibs:FlxEmitter; public function NinjaStar(X:Number, Y:Number, XVelocity:Number, YVelocity:Number, Sparks:FlxEmitter ):void { super(X, Y); loadGraphic(ImgStar, true, true, 16, 16); //Basic movement speeds maxVelocity.x = 200; //How fast left and right it can travel maxVelocity.y = 200; //How fast up and down it can travel angularVelocity = 100; //How many degrees the object rotates //bounding box tweaks width = 5; //Width of the bounding box height = 5; //Height of the bounding box offset.x = 6; //Where in the sprite the bounding box starts on the X axis offset.y = 6; //Where in the sprite the bounding box starts on the Y axis addAnimation("normal", [0]); //Create and name and animation "normal" //This is the particle emitter that spews things off the bottom of the screen. //I'm not going to go over it in too much detail here, but basically we // create the emitter, then we create 50 16x16 sprites and add them to it. _sparks = Sparks; //The object now is removed from the render and update functions. It returns only when reset is called. //We do this so we can precreate several instances of this object to help speed things up a little exists = false; } override public function hitBottom(Contact:FlxObject, Velocity:Number):void { kill(); return;// super.hitBottom(Contact, Velocity); } override public function hitLeft(Contact:FlxObject, Velocity:Number):void { kill(); return;// super.hitLeft(Contact, Velocity); } override public function hitRight(Contact:FlxObject, Velocity:Number):void { kill(); return;// super.hitRight(Contact, Velocity); } override public function hitTop(Contact:FlxObject, Velocity:Number):void { kill(); return;// super.hitTop(Contact, Velocity); } override public function kill():void { _sparks.at(this); _sparks.start(true,0,20); super.kill(); } public function resetStar(X:Number,Y:Number,XVelocity:Number,YVelocity:Number):void { x = X; y = Y; dead = false; exists = true; visible = true; velocity.x = XVelocity; //Set the left and right speed play("normal"); //Play the animation } } }
Next part we will equip the player with this ninja star.
Go back to How To Make A Simple Platform Game In Flash
No comments:
Post a Comment