Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Try collision detection between monsters and bullets (basic stuff)

  • Loading branch information...
commit cb3637400bd6b2c8467b637a59e26c12e1a3f255 1 parent c21aafb
Mario Izquierdo authored
BIN  bin/YouShallNotPass.swf
Binary file not shown
15 src/PlayState.as
@@ -18,6 +18,7 @@ package
18 18 public var playerWizard:PlayerWizard;
19 19 public var cameraFocus:FlxObject;
20 20 public var enemies:FlxGroup;
  21 + public var bullets:FlxGroup;
21 22
22 23 override public function create():void
23 24 {
@@ -38,6 +39,10 @@ package
38 39 enemies.add(new Enemy(350, FLOOR_Y));
39 40 add(enemies);
40 41
  42 + // Bullets (player attacks)
  43 + bullets = new FlxGroup();
  44 + add(bullets);
  45 +
41 46 // Bridge
42 47 add(new FlxSprite(0, FLOOR_Y - 8, ImgBridge));
43 48
@@ -68,9 +73,11 @@ package
68 73 particle.width = 1;
69 74 particle.acceleration.y = 10;
70 75 particle.makeGraphic(1, 1, 0xffffffff, false);
71   - add(particle);
  76 + bullets.add(particle);
72 77 }
73 78
  79 + // Bullet collision with Enemy
  80 + FlxG.overlap(bullets, enemies, hitEnemy);
74 81
75 82 super.update(); // calls update on everything you added to the game loop
76 83
@@ -81,5 +88,11 @@ package
81 88 }
82 89
83 90 } // end function update
  91 +
  92 + //Called whenever a bullet touches a enemy
  93 + public function hitEnemy(bullet:FlxSprite, monster:Enemy):void
  94 + {
  95 + monster.hitBy(bullet);
  96 + }
84 97 }
85 98 }
7 src/PlayerWizard.as
@@ -10,14 +10,9 @@ package
10 10
11 11 public function PlayerWizard(X:int,Y:int)
12 12 {
13   - super(X,Y);
  13 + super(X,Y-HEIGHT); // Initialize having the Y in the bottom
14 14
15   - // load graphic and set align to bottom-center (in the foots)
16 15 loadGraphic(ImgWizard, false, false, WIDTH, HEIGHT);
17   - width = WIDTH;
18   - height = HEIGHT;
19   - origin.x = offset.x = WIDTH/2;
20   - offset.y = origin.y = HEIGHT; // bottom, so the origin is in its foots
21 16
22 17 //basic player physics
23 18 drag.x = 300;
18 src/enemy/Enemy.as
@@ -10,28 +10,28 @@ package enemy
10 10
11 11 public function Enemy(X:int,Y:int)
12 12 {
13   - super(X,Y);
  13 + super(X, Y-HEIGHT); // Initialize having the Y in the bottom
14 14
15   - // load graphic and set align to bottom-center (in the foots)
16 15 loadGraphic(ImgOrc, false, false, WIDTH, HEIGHT);
17   - width = WIDTH;
18   - height = HEIGHT;
19   - origin.x = offset.x = WIDTH/2;
20   - offset.y = origin.y = HEIGHT; // bottom, so the origin is in its foots
21   -
  16 +
22 17 //basic player physics
23 18 maxVelocity.x = 20;
24   -
25 19 }
26 20
27 21 override public function update():void
28 22 {
29 23
30 24 //MOVEMENT
31   - acceleration.x = -100; // just go ahead
  25 + acceleration.x = -15; // just go ahead
32 26
33 27 }
34 28
  29 + public function hitBy(bullet:FlxSprite):void
  30 + {
  31 + velocity.x = +20;
  32 + bullet.kill();
  33 + }
  34 +
35 35 }
36 36
37 37 }

0 comments on commit cb36374

Please sign in to comment.
Something went wrong with that request. Please try again.