Skip to content

Instantly share code, notes, and snippets.

@spotco
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save spotco/80a8be85dbbfbfa8dc9b to your computer and use it in GitHub Desktop.

Select an option

Save spotco/80a8be85dbbfbfa8dc9b to your computer and use it in GitHub Desktop.
working hit detection (3 files)
package {
import flash.geom.Vector3D;
import org.flixel.FlxGroup;
import org.flixel.FlxSprite;
import org.flixel.FlxState;
import org.flixel.FlxG;
import org.flixel.FlxCamera;
import org.flixel.FlxRect;
import org.flixel.system.input.Keyboard;
public class TestState extends FlxState {
var _player:FlxSprite = new FlxSprite();
var _objs:FlxGroup = new FlxGroup();
var _bullets:FlxGroup = new FlxGroup();
public function TestState() {
super();
}
public override function create():void {
super.create();
FlxG.worldBounds = new FlxRect(0, 0, 10000, 10000);
_player.makeGraphic(10, 10, 0xFFFFFFFF);
this.add(_player);
this.add(_objs);
this.add(_bullets);
_objs.add(new Box(800, 800).rotate(15));
_objs.add(new Box(700, 700));
_objs.add(new Box(600, 600).rotate(25));
_objs.add(new Box(500, 500));
_objs.add(new Box(400, 400));
_objs.add(new Box(300, 300).rotate(75));
_objs.add(new Box(200, 200));
_objs.add(new Box(100, 100).rotate(45));
_objs.add(new Box(50, 50));
FlxG.camera.follow(_player);
}
public override function update():void {
super.update();
if (FlxG.keys.pressed("W")) {
_player.y -= 2;
}
if (FlxG.keys.pressed("A")) {
_player.x -= 2;
}
if (FlxG.keys.pressed("S")) {
_player.y += 2;
}
if (FlxG.keys.pressed("D")) {
_player.x += 2;
}
if (true) {
_bullets.add(new Bullet(_player.x,_player.y,0,2,3000));
}
for (var i:int = 0; i < _bullets.length; i++) {
var b:Bullet = _bullets.members[i];
b.game_update();
if (b._ct <= 0) {
_bullets.remove(b, true);
}
}
FlxG.overlap(_bullets, _objs, function(bullet:Bullet, box:Box) {
if (bullet.overlaps(box,false,FlxG.camera)) {
_bullets.remove(bullet, true);
}
});
}
}
}
package {
import org.flixel.FlxSprite;
import flash.geom.*;
public class Bullet extends FlxSprite {
public var _ct:Number, _vel:Vector3D = new Vector3D();
public function Bullet(x:Number, y:Number, vx:Number, vy:Number, ct:Number) {
this.x = x;
this.y = y;
_vel.x = vx;
_vel.y = vy;
_ct = ct;
this.makeGraphic(5, 5, 0xFFFFFFFF);
}
public function game_update():void {
this.x += _vel.x;
this.y += _vel.y;
_ct--;
}
}
}
package {
import org.flixel.FlxSprite;
import flash.geom.*;
public class Box extends FlxSprite {
public function Box(x:Number, y:Number) {
this.x = x;
this.y = y;
this.makeGraphic(30, 30, 0xFFFFFFFF);
}
public function rotate(f:Number):Box {
this.angle = f;
return this;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment