If you study the FlxTeroids demo project closely, you can learn a lot of things from it. But just in case you’re completely new and would like some interesting points highlighted, do read on.
MenuState.hx
|
override public function create():Void { FlxG.mouse.visible = true; var t:FlxText; t = new FlxText(0, FlxG.height / 2 - 20, FlxG.width, "FlxTeroids"); t.setFormat(null, 32, FlxColor.WHITE, "center", FlxText.BORDER_OUTLINE); add(t); t = new FlxText(0, FlxG.height - 30, FlxG.width, "click to play"); t.setFormat(null, 16, FlxColor.WHITE, "center", FlxText.BORDER_OUTLINE); add(t); } |
Note that the
t variable is a reference rather than the value itself. As such you can reuse variables when adding objects of the same type into the stage.
PlayState.hx
|
// Create a starfield add(new FlxStarField2D()); |
Note how easy it is to create a simple star field in HaxeFlixel! 😀
|
// Spawn 3 asteroids for a start asteroids = new FlxTypedGroup<Asteroid>(); add(asteroids); |
The
FlxTypedGroup is similar to an Array, except its purpose can be similar to an Object Pool — you can use
recycle() to reuse inactive objects in the group. Check out the API for more info.
|
FlxG.overlap(bullets, asteroids, bulletHitsAsteroid); FlxG.overlap(asteroids, _playerShip, asteroidHitsShip); FlxG.collide(asteroids); |
There are two ways to check for collisions in HaxeFlixel —
overlap() and
collide() .
overlap() will trigger when the two objects intersect, but you will have to handle collision the logic yourself.
collide() will trigger when two objects intersect as well, but it includes physics; The collided objects will push away from each other automatically. You can use this for situations where you know two objects must not stay intersected with one another, such as platformers (between the player and floor).
Also note that even though the API accepts two arguments, you can just provide one instead, as shown in the code
FlxG.collide(asteroids); . This is because asteroids is a
FlxTypedGroup , and
collide() will check for collision among all the objects within the group. Very convenient!
Also note that you can overlap or collide two
FlxTypedGroups , and it’ll automatically trigger collision between objects from the two groups.
|
for (bullet in bullets.members) { if (bullet.exists) { FlxSpriteUtil.screenWrap(bullet); } } |
The
screenWrap() function is very convenient. Check the API for more info.
|
private function increaseScore(Amount:Int = 10):Void { _score += Amount; _scoreText.text = "Score: " + _score; _scoreText.alpha = 0; FlxTween.tween(_scoreText, { alpha: 1 }, 0.5); } |
The FlxTween library is convenient for various animations. You might end up using it on many occasions 😀
|
private function bulletHitsAsteroid(Object1:FlxObject, Object2:FlxObject):Void { Object1.kill(); Object2.kill(); increaseScore(); } private function spawnAsteroid():Void { var asteroid:Asteroid = asteroids.recycle(Asteroid); asteroid.init(); } |
The
kill() function is part of FlxBasic that can be conveniently used to disable a game object. You can later then use
reset() or
revive() to reenable the game object.
In the case of FlxTeroids — when asteroids are killed, you can instead use
recycle() to revive them. Note that we pass the Asteroid class as parameter into the
recycle() function. This is so that if there are no available inactive asteroids in the group, it will create and add a new asteroid into the group.
|
private function resetTimer(Timer:FlxTimer):Void { Timer.start(5, resetTimer); spawnAsteroid(); } |
You may need
FlxTimer on several occasions, such as “spawn enemy every X seconds”, so take note of its existence 🙂 Check out the API for more info.
We shall continue studying the code in the next post.