1 Replies - 1757 Views - Last Post: 20 July 2012 - 11:16 AM

#1 jay1459  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 17-November 10

hitTestObject always returning true

Posted 20 July 2012 - 04:14 AM

Hello, my Flash assignment is going to kill me, Im brand new to flash and have very little experience in any language, so please excuse the lack of OOP and good use of coding conventions, My problem is in eBCollision it always returns true, i have attached the code for you to see yourself, I really need help with this, I've tried hitTestPoint and Object, collision just isn't working for me and it's probably just something stupid but any help would be greatly appreciated, thank you.

stop();

stage.addEventListener(Event.ENTER_FRAME, main);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownListener)
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpListener)

var stageWidth = stage.stageWidth;
var stageHeight = stage.stageHeight;

var gameState:String = "title";

var playOn:Boolean = true;
var noSelect:Boolean = true;
var returnFrom:Boolean;

var timer:int = 0;
var moveTime:int = 0;

var keyPressedLeft:Boolean, keyPressedRight:Boolean, keyPressedUp:Boolean, keyPressedDown:Boolean;
var keyPressedW:Boolean, keyPressedA:Boolean, keyPressedS:Boolean, keyPressedD:Boolean;
var keyPressedSpace:Boolean;

var enemySpawn:Array = [59.45, 126.3, 193.15, 260, 326.85, 393.7, 460.55];
var globalSpawn = -40;
var enemyMoveSpeed:int = 5;
var enemyName:int = 100;

var playerX:int;
var playerY:int;

Code Separator||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

		case "gScreen":
		{
			var level:MovieClip = new Level1();
			
			level.x = stageWidth/2;
			level.y = stageHeight/2;
			
			addChild(level)
			
			playerSpawn();
			
			gameState = "game";
			
			break;
		}
		
		case "game":
		{		
			enemySpawner();
			shoot();
			break;
		}

Code Separator||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

function enemySpawner()
{
	var cannon:MovieClip = new Cannon1();
	var sideSpawn:int = randRange(0, 0);
	
	cannon.addEventListener(Event.ENTER_FRAME, eBCollide);
	
	if(sideSpawn == 1)
	{
		cannon.x = globalSpawn;
		cannon.y = enemySpawn[randRange(0,6)]
		cannon.dir = "left"
		cannon.scaleX = -0.6;
		cannon.scaleY = 0.6;
		cannon.addEventListener(Event.ENTER_FRAME,enemyMover)
		addChild(cannon);
	}
	
	if (sideSpawn == 2)
	{
		cannon.x = enemySpawn[randRange(0,6)]
		cannon.y = globalSpawn
		cannon.dir = "up"
		cannon.scaleX = -0.6;
		cannon.scaleY = 0.6;
		cannon.rotation = 90;
		cannon.addEventListener(Event.ENTER_FRAME,enemyMover)
		addChild(cannon);
	}
	
		if (sideSpawn == 3)
	{
		cannon.x = stageWidth + globalSpawn;
		cannon.y = enemySpawn[randRange(0,6)]
		cannon.dir = "right"
		cannon.scaleX = 0.6;
		cannon.scaleY = 0.6;
		cannon.addEventListener(Event.ENTER_FRAME,enemyMover)
		addChild(cannon);
	}
	
	if (sideSpawn == 4)
	{
		cannon.x = enemySpawn[randRange(0,6)]
		cannon.y = stageHeight + globalSpawn
		cannon.dir = "down"
		cannon.scaleX = -0.6;
		cannon.scaleY = 0.6;
		cannon.rotation = 270;
		cannon.addEventListener(Event.ENTER_FRAME,enemyMover)
		addChild(cannon);
	}
}

function enemyMover(event:Event)
{
	var cannon:MovieClip = event.currentTarget as MovieClip;
	
	if(cannon.dir == "left")
	{
		cannon.x += enemyMoveSpeed
		
		if(cannon.x > (stageWidth + 40))
		{
			removeChild(cannon)
			cannon.removeEventListener(Event.ENTER_FRAME,enemyMover)
		}
	}
	
	if(cannon.dir == "up")
	{
		cannon.y += enemyMoveSpeed
		
		if(cannon.y > (stageHeight + 40))
		{
			removeChild(cannon)
			cannon.removeEventListener(Event.ENTER_FRAME,enemyMover)
		}
	}
	
	if(cannon.dir == "right")
	{
		cannon.x -= enemyMoveSpeed
		
		if(cannon.x < -40)
		{
			removeChild(cannon)
			cannon.removeEventListener(Event.ENTER_FRAME,enemyMover)
		}
	}
	
	if(cannon.dir == "down")
	{
		cannon.y -= enemyMoveSpeed
		
		if(cannon.y < -40)
		{
			removeChild(cannon)
			cannon.removeEventListener(Event.ENTER_FRAME,enemyMover)
		}
	}
	
	
}

var shootTime:int = 0;
var bSpeed:int = 6;

function shoot():void
{
	var bullet:MovieClip = new Bullet();
	
	bullet.x = playerX;
	bullet.y = playerY;
	
	bullet.scaleX = 0.75;
	bullet.scaleY = 0.75;
	
	bullet.addEventListener(Event.ENTER_FRAME, eBCollide);	
	
	if(shootTime >= 6)
	{
		
		if(keyPressedLeft)
		{
			bullet.dir = "left";
			bullet.addEventListener(Event.ENTER_FRAME, bHandler)
			addChild(bullet)
			shootTime = 0;
		}
		
		if(keyPressedUp)
		{
			bullet.dir = "up";
			bullet.addEventListener(Event.ENTER_FRAME, bHandler)
			addChild(bullet)
			shootTime = 0;
		}
		
		if(keyPressedRight)
		{
			bullet.dir = "right";
			bullet.addEventListener(Event.ENTER_FRAME, bHandler)
			addChild(bullet)
			shootTime = 0;
		}
		
		if(keyPressedDown)
		{
			bullet.dir = "down";
			bullet.addEventListener(Event.ENTER_FRAME, bHandler)
			addChild(bullet)
			shootTime = 0;
		}
	}
	
	shootTime++;
}

function bHandler(event:Event):void
{
	var bullet:MovieClip = event.currentTarget as MovieClip;
	
	if(bullet.dir == "left")
	{
		bullet.x -= bSpeed;
	}
	
	if(bullet.dir == "up")
	{
		bullet.y -= bSpeed;
	}
	
	if(bullet.dir == "right")
	{
		bullet.x += bSpeed;
	}
	
	if(bullet.dir == "down")
	{
		bullet.y += bSpeed;
	}
	
}

function eBCollide(event:Event):void
{
	var bullet:MovieClip = event.currentTarget as MovieClip;
	var cannon:MovieClip = event.currentTarget as MovieClip;
	
	trace(bullet.hitTestObject(cannon))
	
	if(bullet.hitTestObject(cannon))
	{
		//removeChild(bullet)
		//removeChild(cannon)
		/* bullet.removeEventListener(Event.ENTER_FRAME, bHandler)
		cannon.removeEventListener(Event.ENTER_FRAME, enemyMover)
		bullet.removeEventListener(Event.ENTER_FRAME, eBCollide)
		cannon.removeEventListener(Event.ENTER_FRAME, eBCollide) */
	}
}




Is This A Good Question/Topic? 0
  • +

Replies To: hitTestObject always returning true

#2 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: hitTestObject always returning true

Posted 20 July 2012 - 11:16 AM

You need to check your eBCollide function again.

You have 2 statements there:
var bullet:MovieClip = event.currentTarget as MovieClip;
var cannon:MovieClip = event.currentTarget as MovieClip;



What do you think will variables bullet and cannon will contain and why?

Remember eBCollide is a listener to ENTER_FRAME event. Read about the event here.

Also you can try putting trace(bullet) and trace(cannon) inside the function to see what they are.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1