Tuesday, March 30, 2010

Problems with hitTestPoint

Hey Guys,

I am pretty new AS3 and I am having two problems with hitTestPoint. I am making a game and I am having problems checking for hits. My first problem is when using if (sam.hitTestPoint(_bullets[i].x, _bullets[i].y, true))it only works on the first object spawned from the array. The second problem that I am having is when using if (player.hitTestPoint(robot2.x, robot2.y, true)); nothing happens it is supposed to take off a certain percent of my characters health bar. I am getting no errors with these they are just not working.

Here is my code:

package
{
import flash.display.MovieClip;
import flash.events.Event;
import Robot_Follow;


public class Main_SpaceShooter extends MovieClip
{
?//Arrays to store the player's and enemy's bullets
?private var _bullets:Array;
?private var _playerScore:uint;
?private var _robotScore:uint;
?public var bill:Robot_Follow;
?public var sam:Robot_Follow;
?//public var robot3:Robot_Follow;

?public function Main_SpaceShooter()
?{
//Initialize _bullets arrays
_bullets = new Array();
_bullets = [];

//Initialize player and enemy scores
_playerScore = 0;
_robotScore = 0;

//Add an onEnterFrame event listener
addEventListener(Event.ENTER_FRAME,onEnterFrame);

//Add listeners to listen for the bullets' evbents
stage.addEventListener(''bulletCreated'', onBulletCreated);

for (var i:Number = 0; i %26lt; 2; i++)
{
sam=new Robot_Follow()
sam.x =(Math.random()*400);
sam.y =(Math.random()*800);
addChild(sam)

?}
}


?private function playerHit(event:Event)
?{
if (player.hitTestPoint(robot2.x, robot2.y, true));
{
?meter.width -= 10;
}
?}

?private function onBulletCreated(event:Event)
?{
//Add new bullet to the _bullets array
_bullets.push(MovieClip(event.target));
trace(event.target.name);
?}
?private function onEnterFrame(event:Event):void
?{
bulletDisplay.text = ''Bullets on the stage: '' + String(_bullets.length);
//Bullet collisions with objects
for (var i:int = 0; i %26lt; _bullets.length; i++)
{
?switch (_bullets[i].bulletType)
?{
case ''circle'' :

?//Check for a collision with the player
?if (player.hitTestPoint(_bullets[i].x,_bullets[i].y,true))
?{
//Remove the bullet from the stage
removeChild(_bullets[i]);

//Remove bullet from array
_bullets.splice(i,1);

//meter.width -=10;

//Subtract 1 from the counter to compensate
//for the removed element
i--;

//Update the robot's score
_robotScore++;

//Update the robot's score display on the stage
robotScoreDisplay.text = String(_robotScore);
?}
?break;
?

case ''star'' :

//Check for a collision with the robot
if (sam.hitTestPoint(_bullets[i].x, _bullets[i].y, true))
?{
//Remove the bullet from the stage
removeChild(_bullets[i]);

//////?//Remove bullet from array
_bullets.splice(i, 1);
//////
//////?//Subtract 1 from the counter to compensate
//////?//for the removed element
i--;
//////
//////?//Update the enemy's score
_playerScore++;
//////
trace(sam.x);
//////
sam.removeEventListener(Event.ENTER_FRAME, sam.onEnterFrame);
//////
removeChild(sam);
//////
//////
////// //Update the player's score display on the stage
playerScoreDisplay.text = String(_playerScore);
}
break;
}
?}
?
?//Bullet stage Boundaries:
?for (var j:int = 0; j %26lt; _bullets.length; j++)
?{
//Top
if (_bullets[j].y + _bullets[j].height / 2 %26lt; 0)
{
?removeChild(_bullets[j]);
?_bullets.splice(j, 1);
?j--;
}
//Bottom
else if (_bullets[j].y - _bullets[j].height / 2 %26gt; stage.stageHeight)
?{
?removeChild(_bullets[j]);
?_bullets.splice(j, 1);
?j--;
}
//Left
else if (_bullets[j].x + _bullets[j].width / 2 %26lt; 0)
{
?removeChild(_bullets[j]);
?_bullets.splice(j, 1);
?j--;
}
//Right
else if (_bullets[j].x - _bullets[j].width / 2 %26gt; stage.stageWidth)
{
?removeChild(_bullets[j]);
?_bullets.splice(j, 1);
?j--;
}
?}
?}
}
}


Thanks Guys!

Problems with hitTestPoint

I did not think it through but, at a first glance, I think one of the issues could be that you splice array inside the loop (_bullets.splice(i,1);) so the length of the array becomes different and increments i are confused. I think you need to splice the array at the end of the loop.

Problems with hitTestPoint

Hey Andre1,

Thanks for offering some help. But I made the changes you suggested but I am still having the same problem. Is there any other help you can provide it would be much appreciated.

Thanks again!

No comments:

Post a Comment