PDA

View Full Version : hitTest issue



outlaw_xXx
September 10th, 2008, 03:04 AM
Ok here is the deal:

I made 3 objects and assigned a tween for each one, like so


var redTween:Tween = new Tween(newRed, "y", Strong.easeOut, 500, 10, GetRandomRange(10,18), true);
var greenTween:Tween = new Tween(newGreen, "y", Strong.easeOut, 500, 10, GetRandomRange(10,15), true);
var badTween:Tween = new Tween(newBad, "y", Strong.easeOut, 500, 10, GetRandomRange(10,17), true);


and then after assigning the x value to 100 for all 3 objects i added this code


if (newBad.hitTestObject(newRed)) {
removeChild(newRed);
} else if (newBad.hitTestObject(newGreen)) {
removeChild(newGreen);
}


and for some reason it is not removing the objects if they hit eachother.

any help is appreciated:beam:

pensamente
September 10th, 2008, 04:03 AM
hi outlaw, do you have the "if" and "else if" inside any function or method that keeps checking for the condition? (setInterval / onEnterFrame)

outlaw_xXx
September 10th, 2008, 12:09 PM
i have it in different function.....this one will check for hit like this:


function checkForHit(red:Object, green:Object, bad:Object):void {
if (bad.hitTestObject(red)) {
removeChild(DisplayObject(red));
} else if (bad.hitTestObject(green)) {
removeChild(DisplayObject(green));
}
}



but where do i put this function so its checked every frame?

dail
September 11th, 2008, 04:52 AM
Either use an enterFrame or a Timer to check your hit test every time. Maybe something like the below..



addEventListener(Event.ENTER_FRAME, checkForHit)

function checkForHit(event:Event):void{
checkForHit(red, green, bad)
}

function checkForHit(red:Object, green:Object, bad:Object):void {
if (bad.hitTestObject(red)) {
removeChild(DisplayObject(red));
removeEventListener(Event.ENTER_FRAME, checkForHit)
} else if (bad.hitTestObject(green)) {
removeChild(DisplayObject(green));
removeEventListener(Event.ENTER_FRAME, checkForHit)
}
}

outlaw_xXx
September 11th, 2008, 11:40 AM
but on your line 4 how is "checkForHit(red, green, bad)" gona know what the 3 objects are if the objects are not global and are in another function?

outlaw_xXx
September 13th, 2008, 12:29 AM
ok since the 3 objects are not global and are created in another function and when i try what dali suggested i get an error b/c compailer doesnt know what the objects on line 4 are so i tried this:


function checkForHit(name1:String, name2:String, name3:String):void {
if (this.getChildByName(name1).hitTestObject(this.get ChildByName(name3))) {
trace("HIT1");
} else if (this.getChildByName(name2).hitTestObject(this.get ChildByName(name3))) {
trace("HIT2");
}
}


but i get an error: "Parameter hitTestObject must be non-null."
anyone knows the correct way to access 3 object by name and try hittesting them?

thanks for any input