Go back to How To Make A Simple Platform Game In Flash
1. We need to make a few changes to PlayState.as , so open that file. Somewhere in your variable declarations we want to add a new FlxArray to hold all the stars our player is going to throw.
private var _pStars:Array;
2. Somewhere within the constructor, before you initialize the Player object, we want to setup this FlxArray:
_pStars = new Array; _grp_pStars = new FlxGroup(); for (var n:int = 0; n < 40; n += 1) { _pStars.push(lyrSprites.add(new NinjaStar(0, 0, 0, 0, _sparks))); _grp_pStars.add(_pStars[n]); }
We’re going to set _pStars to be a new (empty) FlxArray , and then we’re going to add 40 new stars to this array so that the game won’t have to create them on the fly. This is the array which we checked in the Player class to see if we can reuse a star or if we need to make a new one.
3. Next, we need to make the stars collide with our Tilemap so that they don’t fly through walls. Inside the update function, after super.update() , add:
FlxU.collide(_map, _grp_pStars);
This works the same as the collision with the enemies.
4. Now, we also want our stars to damage the enemies when they get hit. So we need to add an overlapArray check.
FlxU.overlap(_grp_pStars, _grpEnemies, StarHitsEnemy);
This works the same way that collision between the player and the enemy works – we’re just going to call a different function if any star overlaps any enemy.
5. The StarHitsEnemy function is going to be pretty straightforward – if a star collides with an enemy we want to Kill that star , and hurt that enemy . Since our enemies (right now) only have 1 health each, this will kill them, too. In the future, we could add stronger enemies that take more hits, and/or stronger weapons that deal more damage.
private function StarHitsEnemy(colStar:FlxSprite, colEnemy:FlxSprite):void { colStar.kill(); colEnemy.hurt(1); }
That’s it! Test out your code and you should be able to press C to kill the enemy!
Your PlayState.as should look like this:
package com.Tutorial { /** * ... * @author pisces_eyes */ import org.flixel.*; public class PlayState extends FlxState { [Embed(source = '../../org/flixel/data/Tiles.png')] private var ImgTiles:Class; [Embed(source = '../../org/flixel/data/map.txt', mimeType = "application/octet-stream")] private var DataMap:Class; [Embed(source = '../../org/flixel/data/Spark.png')] private var ImgSparks:Class; [Embed(source = '../../org/flixel/data/hearts.png')] private var ImgHearts:Class; private var _player:Player; private var _map:FlxTilemap; private var _e:Array; public static var lyrStage:FlxGroup; public static var lyrSprites:FlxGroup; public static var lyrHUD:FlxGroup; private var _pStars:Array; private var _eStars:Array; protected var _sparks:FlxEmitter; //meta groups, to help speed up collisions protected var _objects:FlxGroup; protected var _grp_pStars:FlxGroup; protected var _grp_eStars:FlxGroup; protected var _grpEnemies:FlxGroup; private var _scoreDisplay:FlxText; private var _hearts:Array; override public function create():void { lyrStage = new FlxGroup; lyrSprites = new FlxGroup; lyrHUD = new FlxGroup; _scoreDisplay = new FlxText(FlxG.width - 50, 2, 48, FlxG.score.toString()); _scoreDisplay.setFormat(null, 16, 0x00ff00, "right"); _scoreDisplay.scrollFactor.x = _scoreDisplay.scrollFactor.y = 0; lyrHUD.add(_scoreDisplay); _map = new FlxTilemap; _map.loadMap(new DataMap, ImgTiles, 16) _map.drawIndex = 1; _map.collideIndex = 1; lyrStage.add(_map); _sparks = new FlxEmitter(); _sparks.delay = 0.2;//1.5; _sparks.gravity = 0; _sparks.setXSpeed(-100,100); _sparks.setYSpeed(-100,100); _sparks.setRotation(-720,-720); _sparks.createSprites(ImgSparks, 10, 10, true, 0.5); lyrSprites.add(_sparks); _objects = new FlxGroup(); _objects.add(_sparks); _pStars = new Array; _grp_pStars = new FlxGroup(); for (var n:int = 0; n < 40; n += 1) { _pStars.push(lyrSprites.add(new NinjaStar(0, 0, 0, 0, _sparks))); _grp_pStars.add(_pStars[n]); } _player = new Player(48, 48, _pStars, _sparks); lyrSprites.add(_player); _eStars = new Array; _grp_eStars = new FlxGroup(); for (n = 0; n < 40; n += 1) { _eStars.push(lyrSprites.add(new NinjaStar(0, 0, 0, 0, _sparks))); _grp_eStars.add(_eStars[n]); } _hearts = new Array(); var tmpH:FlxSprite; for (var hCount:Number = 0; hCount < _player._max_health; hCount++) { tmpH = new FlxSprite(2 +(hCount * 10), 2) tmpH.loadGraphic(ImgHearts, true, false, 8, 8); tmpH.scrollFactor.x = tmpH.scrollFactor.y = 0; tmpH.addAnimation("on", [0]); tmpH.addAnimation("off", [1]); tmpH.play("on"); _hearts.push(lyrHUD.add(tmpH)); } _e = new Array; _grpEnemies = new FlxGroup(); _e.push(lyrSprites.add(new Enemy(100, 100, _player, _eStars, _sparks))); _grpEnemies.add(_e[0]); add(lyrStage); add(lyrSprites); add(lyrHUD); } override public function update():void { var _old_score:uint = FlxG.score; var _old_health:uint = _player.health; super.update(); FlxU.collide(_map, _player); FlxU.collide(_map, _grpEnemies); FlxU.collide(_map, _grp_pStars); FlxU.collide(_map,_objects); FlxU.overlap(_grp_pStars, _grpEnemies, StarHitsEnemy); FlxU.overlap(_grp_eStars, _player, StarHitsPlayer); FlxG.follow(_player,2.5); FlxG.followAdjust(0.5, 0.5); FlxG.followBounds(1, 1, 640 - 1, 480 - 1); if(_player.dead) FlxG.score = 0; if(_old_score != FlxG.score) { _scoreDisplay.text = FlxG.score.toString(); } if(_player.health != _old_health) { for(var i:Number = 0; i < _player._max_health; i++) { if (i >= _player.health) { _hearts[i].play("off"); } else { _hearts[i].play("on"); } } } } private function StarHitsEnemy(colStar:FlxSprite, colEnemy:FlxSprite):void { colStar.kill(); colEnemy.hurt(1); } private function StarHitsPlayer(colStar:FlxSprite, P:Player):void { if (P._hurt_counter <= 0) { if (colStar.x > P.x) { P.velocity.x = -100; } else { P.velocity.x = 100; } P.hurt(1); colStar.kill(); } } } }
Go back to How To Make A Simple Platform Game In Flash
2 comments:
Wow
my PlayState.as looks nothing like this
are you sure you did not skip a few steps?
Great Tutorial! Thank you for explaining each step and making it easier for beginners.
I am working to add multiple enemies and I don't quite understand the FlxGroup code posted to add the enemy in the PlayState.
You have:
_e = new Array;
_grpEnemies = new FlxGroup();
_e.push(lyrSprites.add(new Enemy(100, 100, _player, _eStars, _sparks)));
_e.push(lyrSprites.add(new Enemy(100, 100, _player, _eStars, _sparks)));
_grpEnemies.add(_e[0]);
I tried adding another add line, but the second enemy just falls through the floor. Suggestions?
Post a Comment