PDA

View Full Version : [AS3] hitTestObject


Ben91
04-04-2010, 10:49 AM
Hi i've been making skeetshoot game as part of an assignment.

However the hitTestObject part is not working exactly how i want it.
When a bullet hits a gameskeet or gameskeet2 i want the score to increase by 10 and it to be displayed in the dynamic text. It does this but then goes back to 0.

I'm not sure if it's because i've put some if statements in to get past the problem with some objects being null as at times they aren't on the stage.

I'd really appreciate some help with this as i'm stuck at what to do.

I've put the section of code below. I apologise if it isn't concise- I'm a flash noob xD.

Thanks, Ben.

function updateGameScore():void {
score+=10;
gameScore.text = String(score);
}

stage.addEventListener(Event.ENTER_FRAME, collisionDetector);

function collisionDetector (event:Event):void {
if (bullet != null)
{
if (gameskeet != null)
{
if (gameskeet.hitTestObject(bullet))
{
if (!collisionOccurred)
{
updateGameScore();
collisionOccurred = true;
removeChild(gameskeet);
}
}
else
{
collisionOccurred = false;
}
}
else if (gameskeet2 != null)
{
if (gameskeet2.hitTestObject(bullet))
{
if (!collisionOccurred)
{
updateGameScore();
collisionOccurred = true;
removeChild(gameskeet2);
}
}
else
{
collisionOccurred = false;
}
}
}
}

Impacatus
04-04-2010, 09:51 PM
Doesnt look like anything in that code should be setting it to zero.

Is it the "score" variable getting reset, or just the gameScore textbox?

Try doing a search (ctrl+f) for every time the variable is mentioned, and see if you can figure out where it's getting reset. If that doesn't work, try commenting out each reference one by one until you find the one that stops this from happening.

Ben91
04-04-2010, 11:41 PM
Just realised i'd missed off the start of that piece of code. Does the first bit of this make any difference?

var score:int;
var collisionOccurred:Boolean;
gameScore.text = "0";

score = 0;
collisionOccurred = false;

function updateGameScore():void {
score+=10;
gameScore.text = String(score);
}

stage.addEventListener(Event.ENTER_FRAME, collisionDetector);

function collisionDetector (event:Event):void {
if (bullet != null)
{
if (gameskeet != null)
{
if (gameskeet.hitTestObject(bullet))
{
if (!collisionOccurred)
{
updateGameScore();
collisionOccurred = true;
removeChild(gameskeet);
}
}
else
{
collisionOccurred = false;
}
}
else if (gameskeet2 != null)
{
if (gameskeet2.hitTestObject(bullet))
{
if (!collisionOccurred)
{
updateGameScore();
collisionOccurred = true;
removeChild(gameskeet2);
}
}
else
{
collisionOccurred = false;
}
}
}
}

Impacatus
04-05-2010, 05:46 PM
Hmm, not that I can see, unless this section:
score = 0;
collisionOccurred = false;
...is being executed more than once for some reason. Maybe you can try putting a trace next to it and see.

When you say it goes back to zero, when do you mean? Unless you mean after you close the swf and restart it, I don't see anything in that code that should cause it.

mecopius
04-05-2010, 09:05 PM
Where is this code? If this code is on a frame and the frame is being called again, then that could be the reason the score is being set back to 0.