Go back to How To Make A Simple Platform Game In Flash
In this part we will create a simple enemy using a new Enemy class that extends FlxSprite. This Enemy class is a near copy of the Player class that we already created so some parts of the class will not be described in details.
First is to create an image sprite file for new Enemy object with the similar frame as of the Player. We do this because we are using the same animation of the Player.
Notice that this sprite is exactly the same as the player with just a new color.
Let's get started:
1. Create a new Class file in the com\Tutorial directory called “Enemy.as”.
2. Define the package structure, import Fixel , and extend this class as Flxsprite.
package com.Tutorial { /** * ... * @author pisces_eyes */ import org.flixel.*; public class Enemy extends FlxSprite {
3. Next, embed the new enemy image for this Enemy class:
[Embed(source = '../../org/flixel/data/Enemy.png')] private var ImgEnemy:Class;
4. Then, declare the variables and define the constructor:
private var _move_speed:int = 400; private var _jump_power:int = 800; private var _max_health:int = 10; private var _hurt_counter:Number = 0; private var _can_jump:Boolean = true; private var _last_jump:Number = 0; private var _player:FlxSprite; public function Enemy(X:Number, Y:Number, ThePlayer:FlxSprite):void { super(X, Y); loadGraphic(ImgEnemy, true, true, 16, 16); maxVelocity.x = 25; maxVelocity.y = 200;
There are 4 differences here compare to the Player Class :
* The _can_jump and _last_jump variables will be used to inform the Enemy if it is ok to jump or not.
* The _player variable is set to be of type FlxSprite. This variable will hold the id, index or value of a FlxSprite Class. This one will hold the value of the Player class that is added in PlayState.
* In the constructor, the variable ThePlayer defined as a FlxSprite that will be used to pass the id of the Player object to this Class from the PlayState class.
* In the FlxSprite’s constructor, we assign ThePlayer to private variable _player.
We can use it all the way through the class to do checks against the player.
The maxVelocity will limit the Enemy's movement speed along x or y axis.
5. Then, to finalize the constructor. This section will be exactly like the Player Class. We setup health, gravity, friction, bounding boxes for collisions, and the animations.
health = 5; acceleration.y = 420; drag.x = 300; width = 8; height = 14; offset.x = 4; offset.y = 2; addAnimation("normal", [0, 1, 2, 3], 10); addAnimation("jump", [2]); addAnimation("attack", [4,5,6],10); addAnimation("stopped", [0]); addAnimation("hurt", [2,7],10); addAnimation("dead", [7, 7, 7], 5); }
6. Again the first section of the update function is just copy from the Player Class.
override public function update():void { if(dead) { if (finished) exists = false; else super.update(); return; } if (_hurt_counter > 0) { _hurt_counter -= FlxG.elapsed*3; }
7. Then, add the horizontal movement of the Enemy. Notice the use of _player variable that holds the Player’s id that was added to PlayState.
This simply says if the Player’s X is less than the Enemy’s X then move the Enemy to the left and update the facing for animations.
if(_player.x < x)
{
facing = LEFT;
velocity.x -= _move_speed * FlxG.elapsed;
}
Else, is to move the Enemy to the right and change the facing for animations.
else
{
facing = RIGHT;
velocity.x += _move_speed * FlxG.elapsed;
}
8. For jumping the enemy, we need a delay between jumps to prevent the Enemy jumping all the time.
if (_last_jump > 0)
{
_last_jump -= FlxG.elapsed;
}
Then, also want that the enemy is only jumping when he is on the ground. So we need another test if he can jump:
if(velocity.y != 0 || _last_jump > 0) { _can_jump = false; }
Now, this is the actual jump:
if(_player.y < y && _can_jump) { velocity.y = -_jump_power; _can_jump = false; _last_jump = 2; }
9. This section will set up the animations like in the Player Class:
if (_hurt_counter > 0) { play("hurt"); } else { if (_attack_counter > 0) { play("attack"); } else { if (velocity.y != 0) { play("jump"); } else { if (velocity.x == 0) { play("stopped"); } else { play("normal"); } } } } super.update(); }
10. Then, override the hitBottom. This function is called whenever a collision occurs against the bottom of the object. We override so that we can set the _can_jump variable to true and make the Enemy able to jump again:
override public function hitBottom(Contact:FlxObject, Velocity:Number):void { _can_jump = true; return super.hitBottom(Contact,Velocity); }
11. Lastly, override the hurt function like in the Player Class:
override public function hurt(Damage:Number):void { _hurt_counter = 1; return super.hurt(Damage); }
Here's the complete code for Enemy.as class:
package com.Tutorial { /** * ... * @author pisces_eyes */ import org.flixel.*; public class Enemy extends FlxSprite { [Embed(source = '../../org/flixel/data/Enemy.png')] private var ImgEnemy:Class; private var _move_speed:int = 400; private var _jump_power:int = 800; private var _max_health:int = 10; private var _hurt_counter:Number = 0; private var _can_jump:Boolean = true; private var _last_jump:Number = 0; private var _player:FlxSprite; private var _eStars:Array; private var _attack_counter:Number = 0; private var _sparks:FlxEmitter; public function Enemy(X:Number, Y:Number, ThePlayer:FlxSprite, EnemyStars:Array, Sparks:FlxEmitter):void { super(X, Y); loadGraphic(ImgEnemy, true, true, 16, 16); _player = ThePlayer; _eStars = EnemyStars; _sparks = Sparks; maxVelocity.x = 25; maxVelocity.y = 200; health = 5; acceleration.y = 420; drag.x = 300; width = 8; height = 14; offset.x = 4; offset.y = 2; addAnimation("normal", [0, 1, 2, 3], 10); addAnimation("jump", [2]); addAnimation("attack", [4,5,6],10); addAnimation("stopped", [0]); addAnimation("hurt", [2,7],10); addAnimation("dead", [7, 7, 7], 5); } override public function update():void { if(dead) { if (finished) exists = false; else super.update(); return; } if (_hurt_counter > 0) { _hurt_counter -= FlxG.elapsed*3; } if (_attack_counter > 0) { _attack_counter -= FlxG.elapsed*3; } if(_attack_counter <= 0 && _player.y > y - 1 && _player.y < y + 1) { _attack_counter = 2; play("attack"); throwStar(facing); } if(_player.x < x) { facing = LEFT; velocity.x -= _move_speed * FlxG.elapsed; } else { facing = RIGHT; velocity.x += _move_speed * FlxG.elapsed; } if (_last_jump > 0) { _last_jump -= FlxG.elapsed; } if(velocity.y != 0 || _last_jump > 0) { _can_jump = false; } if(_player.y < y && _can_jump) { velocity.y = -_jump_power; _can_jump = false; _last_jump = 2; } if (_hurt_counter > 0) { play("hurt"); } else { if (_attack_counter > 0) { play("attack"); } else { if (velocity.y != 0) { play("jump"); } else { if (velocity.x == 0) { play("stopped"); } else { play("normal"); } } } } super.update(); } override public function hitBottom(Contact:FlxObject, Velocity:Number):void { _can_jump = true; return super.hitBottom(Contact,Velocity); } override public function hurt(Damage:Number):void { _hurt_counter = 1; return super.hurt(Damage); } } }
On the next chapter we will create the weapon.
Go back to How To Make A Simple Platform Game In Flash
No comments:
Post a Comment