Go back to How To Make A Simple Platform Game In Flash
1. We now require to update the Player.as file to be able the player to really throw these stars.
2. We require to include 2 more variables. First, a FlxArray which will hold all of the stars the player throws. We also need to track when the player can throw another star – we want to create a pause.
private var _stars:Array; private var _attack_counter:Number = 0;
3. Next, we’re going to need to pass the Array of stars to the Player object when it initializes. So, change the top of the constructor class to this:
private var _sparks:FlxEmitter; public function Player(X:Number,Y:Number,Stars:Array, Sparks:FlxEmitter):void {
4. Somewhere within the constructor, after "**super** ", we need to actually connect the Stars array we pass into the constructor to the _stars array we created.
_stars = Stars; _sparks = Sparks;
5. Next, in the update function, we want to see if our attack counter is > 0,and if so, subtract from it to make it closer to 0. The player will only be able to throw a Star if the counter is at 0. You can place this code right around the hurt counter code:
if (_attack_counter > 0)
{
_attack_counter -= FlxG.elapsed*3;
}
6. Now we want to respond to the player pressing the attack key . Still in update , and below the other keypress checks, add this:
if(FlxG.keys.justPressed("C") && _attack_counter <= 0) { _attack_counter = 1; play("attack"); throwStar(facing); }
So what this says is: If the player presses the B Key ( C ), and the attack counter is 0 or less, then set the counter to 1, play the “attack” animation , and then call a function called "**throwStar** ", passing data based on which direction the player is facing . We’ll write this function later.
7. Next, we’re going to make sure the attack animation plays while the attack counter is going down. This helps show the player that they can’t attack again just yet. In the update function, right beneath the _hurt_counter check to play(“hurt”), we’re going to add this clause to the if statement :
else if (_attack_counter > 0) { play("attack"); }
8. For the throwStar function , we’re going to do a couple of things. First, we need it to receive the direction we want to face to first determine which way to throw the star . “facing” is a variable built into FlxSprite . If an object is ‘facing ’ to the right, then it’s true , otherwise, it’s false . We pass facing to this function, and use it to determine which direction the star will move.
Then we’re going to see if we have any stars that we can reuse: stars that are in our array of stars , but aren’t currently on the screen . If we do, we’re going to use one of those stars , or we’ll make a new star if we have to.
Then we set the star’s velocity , and add our star to the lyrSprites layer , so it starts moving in the direction specified.
private function throwStar( dir:uint ):void { var XVelocity:Number; if (dir == RIGHT) XVelocity = 200; else XVelocity = -200; for(var i:uint = 0; i < _stars.length; i++) if(!_stars[i].exists) { _stars[i].resetStar(x, y + 2,XVelocity, 0); return; } var star:NinjaStar = new NinjaStar(x, y + 2, XVelocity, 0, _sparks); star.resetStar(x, y,XVelocity, 0) _stars.push(PlayState.lyrSprites.add(star) ); }
That’s it for the Player.as file for now! It should looks like this:
package com.Tutorial { /** * ... * @author pisces_eyes */ import org.flixel.*; public class Player extends FlxSprite { [Embed(source = '../../org/flixel/data/Player.png')] private var ImgPlayer:Class; private var _move_speed:int = 400; private var _jump_power:int = 800; public var _max_health:int = 10; public var _hurt_counter:Number = 0; private var _stars:Array; private var _attack_counter:Number = 0; private var _sparks:FlxEmitter; public function Player(X:Number,Y:Number,Stars:Array, Sparks:FlxEmitter):void { super(X, Y); loadGraphic(ImgPlayer, true, true, 16, 16); _stars = Stars; _sparks = Sparks; //Max speeds maxVelocity.x = 70; maxVelocity.y = 200; //Set the player health health = 10; //Gravity acceleration.y = 420; //Friction drag.x = 300; //bounding box tweaks 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); facing = RIGHT; } 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(FlxG.keys.LEFT) { facing = LEFT; velocity.x -= _move_speed * FlxG.elapsed; } else if (FlxG.keys.RIGHT) { facing = RIGHT; velocity.x += _move_speed * FlxG.elapsed; } if (FlxG.keys.justPressed("X") && velocity.y == 0) { velocity.y = -_jump_power; } if(FlxG.keys.justPressed("C") && _attack_counter <= 0) { _attack_counter = 1; play("attack"); throwStar(facing); } 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 hurt(Damage:Number):void { _hurt_counter = 1; return super.hurt(Damage); } private function throwStar( dir:uint ):void { var XVelocity:Number; if (dir == RIGHT) XVelocity = 200; else XVelocity = -200; for(var i:uint = 0; i < _stars.length; i++) if(!_stars[i].exists) { _stars[i].resetStar(x, y + 2,XVelocity, 0); return; } var star:NinjaStar = new NinjaStar(x, y + 2, XVelocity, 0, _sparks); star.resetStar(x, y,XVelocity, 0) _stars.push(PlayState.lyrSprites.add(star) ); } } }
Go back to How To Make A Simple Platform Game In Flash
No comments:
Post a Comment