Skip to main content
Inspiring
August 28, 2015
Answered

hitTestObject score increases but it keeps going on..... it should increase only one point

  • August 28, 2015
  • 3 replies
  • 545 views

hitTestObject score increases but it keeps going on.....

it should increase only one point when i drag crescent on start but it runs increasing point untill i remove it . any way to fix it ?

var score:Number=0;

addEventListener(Event.ENTER_FRAME, checkCollision);

function checkCollision(event:Event) {

  // test star versus crescent

  if (star.hitTestObject(crescent)) {

  messageText2.text = "hitTestObject:Yes";

  score=score+1;

  text1.text=String(score);

  } else {

  messageText2.text = "hitTestObject: NO";

  }

}

This topic has been closed for replies.
Correct answer kglad

my error.  use:

var hitBool:Boolean

var score:Number=0;

addEventListener(Event.ENTER_FRAME, checkCollision);

function checkCollision(event:Event) {

  // test star versus crescent

  if (star.hitTestObject(crescent)) {

if(!hitBool){

hitBool=true;

  messageText2.text = "hitTestObject:Yes";

  score=score+1;

  text1.text=String(score);

}

  } else {

hitBool=false;  // <- if you want to allow another hit after a no-hit.

  messageText2.text = "hitTestObject: NO";

  }

}

3 replies

Inspiring
August 28, 2015

working fine. thanks a lot

kglad
Community Expert
Community Expert
August 29, 2015

you're welcome.

Inspiring
August 28, 2015

kglad

thanks for your reply . i applied your code . but its same like previous.

here is the link of animation : http://canvas.byethost11.com/forum/CollisionDetection.swf

kglad
Community Expert
Community Expert
August 28, 2015

if the collision continues, score will increase with each loop.

if you only want it to increase once (until there's no hit), use a boolean

var hitBool:Boolean

var score:Number=0;

addEventListener(Event.ENTER_FRAME, checkCollision);

function checkCollision(event:Event) {

  // test star versus crescent

  if (star.hitTestObject(crescent)&&!hitBool) {

hitBool=true;

  messageText2.text = "hitTestObject:Yes";

  score=score+1;

  text1.text=String(score);

  } else {

hitBool=false;  // <- if you want to allow another hit after a no-hit.

  messageText2.text = "hitTestObject: NO";

  }

}

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
August 28, 2015

my error.  use:

var hitBool:Boolean

var score:Number=0;

addEventListener(Event.ENTER_FRAME, checkCollision);

function checkCollision(event:Event) {

  // test star versus crescent

  if (star.hitTestObject(crescent)) {

if(!hitBool){

hitBool=true;

  messageText2.text = "hitTestObject:Yes";

  score=score+1;

  text1.text=String(score);

}

  } else {

hitBool=false;  // <- if you want to allow another hit after a no-hit.

  messageText2.text = "hitTestObject: NO";

  }

}