Skip to main content
Known Participant
August 24, 2009
Question

hitTestObject doesn't detect intended object

  • August 24, 2009
  • 2 replies
  • 938 views

I am trying to quickly publish a simple game in which the user has to cross through an intersection without being hit by another car. Both roads curve, so the movie clips each consist of a child movie clip symbol moving along a path on the timeline. The User's movie clip plays when the user hits the space bar, and the other movie clip is initiated everytime the main timeline loops. The user has to carefully time his spacebar so that the child movie clips do not occupy the same space on the stage at the same time.

The target path to the user's car is: parkingQue.nextCar.playerCar (actually a child of a child)

The target path to the crossing car is: northBoundCar.northCar

I am attempting the following code, but naturally it doesn't work:


parkingQue.nextCar.playerCar.addEventListener(Event.ENTER_FRAME, crash);

function crash(event:Event):void {
    if (parkingQue.nextCar.playerCar.hitTestObject(northBoundCar.northCar)) {
        trace ("ouch");
    } else {
        trace ("whew");
    }
}

This topic has been closed for replies.

2 replies

Known Participant
August 26, 2009

I have tried to simplify this file, but I still can't figure out what is wrong. I have checked and rechecked my keyframes, and all instances are named as they should. Additionally, I have moved the cars directly in front of each other so that there is no possible way that they are not contacting each other.

Below is my code, and the simplified file is attached. I am tracing the x and y of both children, but discovered that they are returning a constant value. Am I trying the hitTest on the wrong objects? I'm lost. I just don't get this.


var northBoundCar:outBoundThruCar = new outBoundThruCar;
var nextCar:roundNRound = new roundNRound;
var thisOtherKey:uint = new uint;

northBoundContainer.addChild(northBoundCar);
playerCarContainer.addChild(nextCar);

function goNow (event:KeyboardEvent): void {
    thisOtherKey = event.keyCode;
    if (thisOtherKey == 32) {
        play();
        nextCar.play()
    }
}         

function crash(event:Event):void {
    if (northBoundCar.northCar.hitTestObject(nextCar.playerCar)) {
        trace ("ouch");
    } else {
        trace ("whew");
        trace (nextCar.playerCar.x + ", " + nextCar.playerCar.y);
        trace (northBoundCar.northCar.x + ", " + northBoundCar.northCar.y);
    }
}

stage.addEventListener(KeyboardEvent.KEY_DOWN, goNow);
nextCar.playerCar.addEventListener(Event.ENTER_FRAME, crash);

Known Participant
August 26, 2009

For some reason, I couldn't upload the .fla. I'll try again with a .zip ...

kglad
Community Expert
Community Expert
August 26, 2009

i generally don't download and correct files unless i'm hired to do so.

each object should be in one keyframe.  if one of the objects is in more than one keyframe you should clear all the keyframes but the first, then add the keyframes you need.  if you do this any other way objects that look the same to you and that have the same instance name will be considered different objects by flash.

kglad
Community Expert
Community Expert
August 24, 2009

if "ouch" not traced when you think it should you have a path/name problem with, at least, one of the two cars.

the most common problem is caused by using an object that appears the same (and has the same instance name) in more than 1 keyframe but which flash considers two different instances.  this is always a problem unless you place the object in the first keyframe in which it appears give it an instance name, clear all other keyframes on its layer, insert the other keyframes, transform the object in the those keyframes, add tweens and retest.

Known Participant
August 25, 2009

That's what I thought too, but I've gone through both movie clips and double checked all of the keyframes. I have checked the parent clips as well, and simply cannot find an instance that is either un-named or mis-named. I have used trace() to double check both the target path and the names and can't see a problem that way either. I was wondering if maybe there was a problem with the child of a child thing,  but I guess not.

the file's attached, if you had either the time or the inclination to take a look.