Skip to content

Instantly share code, notes, and snippets.

@zachwlewis
Created April 4, 2012 16:24
Show Gist options
  • Select an option

  • Save zachwlewis/2303508 to your computer and use it in GitHub Desktop.

Select an option

Save zachwlewis/2303508 to your computer and use it in GitHub Desktop.
Emitter Tutorial Game
package
{
import flash.display.BitmapData;
import flash.geom.Rectangle;
import net.flashpunk.FP;
import net.flashpunk.graphics.Emitter;
import net.flashpunk.graphics.Graphiclist;
import net.flashpunk.graphics.Image;
import net.flashpunk.graphics.Text;
import net.flashpunk.utils.Input;
import net.flashpunk.World;
/**
* A massive black hole spitting out enemies! Where are they coming from!?
* Handles emitters, enemy spawning, and player input.
* Has functions to create explosions, trails, and all sorts of cool stuff.
* @author Zachary Weston Lewis (thegamestudio.net)
*/
public class EmitterWorld extends World
{
// Static variables
public static const EXPLOSION:String = "explosion";
public static const TRAIL:String = "trail";
public static const MISS:String = "miss";
// Class variables
protected var _emitter:Emitter;
protected var _score:uint;
protected var _scoreText:Text;
protected var _timer:Number;
protected var _blackHole0:Image;
protected var _blackHole1:Image;
protected var _blackHoleA:Image;
protected var _blackHoleB:Image;
protected var _blackHoleC:Image;
protected var _blackHoleD:Image;
public function EmitterWorld()
{
super();
_score = 0;
_timer = 0;
}
override public function begin():void
{
// Create our emitter's graphic.
var bd:BitmapData = new BitmapData(6, 3, true, 0);
bd.setPixel32(1, 1, 0xffffffff);
bd.fillRect(new Rectangle(3, 0, 3, 3), 0xffffffff);
// Create our emitter.
_emitter = new Emitter(bd, 3, 3);
_emitter.relative = false;
// Set the properties for the explosion.
_emitter.newType(EXPLOSION, [0]);
_emitter.setMotion(EXPLOSION, 0, 10, 0.5, 360, 25, 0.5);
_emitter.setColor(EXPLOSION, 0xfff200, 0xf26522);
// Set the properties for a miss.
_emitter.newType(MISS, [0]);
_emitter.setMotion(MISS, 0, 5, 0.2, 360, 2, 0.1);
// Set the properties for the smoke trail.
_emitter.newType(TRAIL, [1]);
_emitter.setMotion(TRAIL, 0, 5, 1, 360, 5, 0.25);
_emitter.setColor(TRAIL, 0xebebeb, 0xa1a1a1);
_emitter.setAlpha(TRAIL, 0.8, 0);
// Add our emitter to the world.
// Notice that we don't need a reference to the containing entity.
addGraphic(_emitter);
// Add our score.
_scoreText = new Text(String(_score));
addGraphic(_scoreText);
// Add our black hole
// This is goofy graphics.
addBlackHole();
super.begin();
}
/**
* Updates the visible score and the score variable for the player.
* @param value The amount by which to increase the current score.
*/
public function updateScore(value:uint):void
{
_score += value;
_scoreText.text = String(_score);
}
/**
* Emits a given number of a specified particle at a desired location.
* @param type The type of particle to emit
* @param x The x location to emit the particle (in world space)
* @param y The y location to emit the particle (in world space)
* @param size The number of particles to emit
*/
public function emit(type:String, x:int, y:int, size:uint):void
{
var i:uint;
for (i = 0; i < size; i++)
{
_emitter.emit(type, x, y);
}
}
override public function update():void
{
var e:Enemy;
if (_timer <= 0)
{
_timer += Math.random() * 2;
e = Enemy(add(new Enemy()));
}
else
{
_timer -= FP.elapsed;
}
if (Input.mousePressed)
{
// Check to see if our mouse is on an enemy.
// If it is, destroy it.
// If it isn't, show a miss.
e = Enemy(collidePoint(Enemy.TYPE, mouseX, mouseY));
if (e != null) e.destroy();
else emit(EmitterWorld.MISS, mouseX, mouseY, 10);
}
// Update our black hole.
_blackHole0.angle -= 0.5;
_blackHole1.angle += 0.25;
_blackHoleA.angle += 2;
_blackHoleB.angle -= 2;
_blackHoleC.angle -= 4;
_blackHoleD.angle += 5;
super.update();
}
protected function addBlackHole():void
{
_blackHole0 = new Image(new BitmapData(170, 170));
_blackHole1 = new Image(new BitmapData(150, 150));
_blackHoleA = new Image(new BitmapData(50, 50));
_blackHoleB = new Image(new BitmapData(42, 42));
_blackHoleC = new Image(new BitmapData(38, 38));
_blackHoleD = new Image(new BitmapData(36, 36));
_blackHole0.centerOrigin();
_blackHole1.centerOrigin();
_blackHoleA.centerOrigin();
_blackHoleB.centerOrigin();
_blackHoleC.centerOrigin();
_blackHoleD.centerOrigin();
_blackHole0.color = 0x1f1f2e;
_blackHole1.color = 0x1f1f2e;
_blackHoleA.color = 0x1f1f2e;
_blackHoleB.color = 0x000000;
_blackHoleC.color = 0x1f1f2e;
_blackHoleD.color = 0x000000;
_blackHole0.alpha = 0.2;
_blackHole1.alpha = 0.2;
_blackHoleA.alpha = 0.2;
_blackHoleB.alpha = 0.2;
_blackHoleC.alpha = 0.2;
addGraphic(new Graphiclist(_blackHole0, _blackHole1, _blackHoleA, _blackHoleB, _blackHoleC, _blackHoleD), 1, FP.screen.width / 2, FP.screen.height / 2);
}
}
}
package
{
import flash.display.BitmapData;
import net.flashpunk.Entity;
import net.flashpunk.FP;
import net.flashpunk.Graphic;
import net.flashpunk.graphics.Image;
import net.flashpunk.Mask;
import net.flashpunk.masks.Hitbox;
/**
* A spinning enemy that emerges from a black hole.
* Destroying him awards the player 250 points and explosions.
* @author Zachary Weston Lewis (thegamestudio.net)
*/
public class Enemy extends Entity
{
protected var _sprite:Image;
protected var _vx:Number;
protected var _vy:Number;
public static const SPEED_MAX:Number = 80;
public static const SPEED_MIN:Number = 30;
public static const TYPE:String = "enemy";
public function Enemy()
{
var angle:Number = Math.random() * Math.PI * 2;
var speed:Number = Math.random() * (SPEED_MAX - SPEED_MIN) + SPEED_MIN;
_vx = speed * Math.cos(angle);
_vy = speed * Math.sin(angle);
layer = -1;
x = FP.screen.width / 2;
y = FP.screen.height / 2;
_sprite = new Image(new BitmapData(6, 6));
_sprite.color = 0xfe2bf2;
_sprite.centerOrigin();
graphic = _sprite;
type = Enemy.TYPE;
mask = new Hitbox(8, 8, -4, -4);
super(x, y, graphic, mask);
}
override public function update():void
{
// Offscreen? Remove.
if (x < -10 || x > FP.screen.width + 10 || y < -10 || y > FP.screen.height + 10) world.remove(this);
else
{
// Onscreen? Let's move him!
_sprite.angle += 5;
x += _vx * FP.elapsed;
y += _vy * FP.elapsed;
// Also, make our trail.
EmitterWorld(world).emit(EmitterWorld.TRAIL, x, y, 1);
}
super.update();
}
/** Creates an explosion, updates the score, and removes the enemy from the world. */
public function destroy():void
{
// Get a handle to our world.
var w:EmitterWorld;
w = EmitterWorld(world);
// Now, perform the steps.
w.emit(EmitterWorld.EXPLOSION, x, y, 100);
w.updateScore(250);
w.remove(this);
}
}
}
package
{
import net.flashpunk.Engine;
import net.flashpunk.FP;
/**
* Set up our Flash movie and start the FlashPunk rendering framework.
* @author Zachary Weston Lewis (thegamestudio.net)
*/
[SWF(width=512, height=448)]
public class Main extends Engine
{
public function Main():void
{
super(256, 224);
}
override public function init():void
{
FP.screen.scale = 2;
FP.screen.color = 0;
FP.world = new EmitterWorld();
super.init();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment