Hello,
i have an issue with collision detection in a tower defense game. I have the collision detection in my Enemy class and it should detect when hitting a directional tile to change direction. The think is it is not detecting anything and what is more annoying is that when i put the detection code in the directional tile class it is working. But by doing that i have other issued to deal with so i would prefer it in the Enemy class since it is easier to work with and more logical. This is the directional tile class:
public class directBlock extends Entity {
private var blockImg:Image;
public var directType:String;
public function directBlock(type:String, xVal:int, yVal:int) {
blockImg = new Image(new BitmapData(C.TILE_WIDTH, C.TILE_HEIGHT, false, 0x111111));
x = xVal;
y = yVal;
directType = type;
setHitboxTo(blockImg);
type = "directBlock";
layer = C.LAYER_DIRECT_BLOCK;
graphic = blockImg;
}
}
And this is my Enemy class:
public class Enemy extends Entity {
public var xSpeed:int; //how fast it's going horizontally
public var ySpeed:int; //how fast it's going vertically
public var maxSpeed:int = C.MAX_ENEMY_SPEED; //how fast it can possibly go
private var enemyImg:Image;
public function Enemy(initDir:String, xVal:int, yVal:int) {
var sprite:Sprite = new Sprite;
sprite.graphics.beginFill(0xFF0000);
sprite.graphics.drawCircle(12.5,12.5,5);
sprite.graphics.endFill();
var bd:BitmapData = new BitmapData(C.TILE_WIDTH, C.TILE_HEIGHT, true, 0);
bd.draw(sprite);
enemyImg = new Image(bd);
mask = new Pixelmask(enemyImg.getBuffer());
switch (initDir) {
case C.U: //up
xSpeed = 0;
ySpeed = -maxSpeed;
break;
case C.D: //down
xSpeed = 0;
ySpeed = maxSpeed;
break;
case C.R: //right
xSpeed = maxSpeed;
ySpeed = 0;
break;
case C.L: //left
xSpeed = -maxSpeed;
ySpeed = 0;
break;
default:
xSpeed = 0;
ySpeed = 0;
break;
}
x = xVal;
y = yVal;
layer = C.LAYER_ENEMY;
type = "enemy";
graphic = enemyImg;
}
override public function update():void {
x += xSpeed * FP.elapsed;
y += ySpeed * FP.elapsed;
var d:directBlock = collide("directBlock", x, y) as directBlock;
if (d) {
changeDirection(d.directType);
}
}
private function changeDirection(newDir:String):void {
switch (newDir) {
case C.U: //up
xSpeed = 0;
ySpeed = -maxSpeed;
break;
case C.D: //down
xSpeed = 0;
ySpeed = maxSpeed;
break;
case C.R: //right
xSpeed = maxSpeed;
ySpeed = 0;
break;
case C.L: //left
xSpeed = -maxSpeed;
ySpeed = 0;
break;
default:
break;
}
}
}
Their layers are equal and the C.TILE_WIDTH and C.TILE_HEIGHT are both 25 pixels.