-
Disable click event when object is hidden
Hi all,
I have a very confusing conundrum and am not even sure how to word it in the subject line. I have a game where "targets" hide behind trees and when they pop out from behind, you click them and they are destroyed. For some reason though, when you click the tree they are hiding behind, they still get destroyed. I want them to be safe from the user clicks when hiding. Here is a sample of the code I am using:
Code:
stage.addEventListener(MouseEvent.CLICK, onClickHandler, false, 0, true);
function onClickHandler(event:MouseEvent):void
{
mousePositionX = stage.mouseX;
mousePositionY = stage.mouseY;
var bullet:MovieClip = createBullet();
TweenMax.to(bullet, .5, {x:mousePositionX, y:mousePositionY, onComplete:removeSnowball});
function removeSnowball():void {
var poof:Poof = new Poof();
if(bullet.hitTestObject(target1)) {
poof.x = bullet.x;
poof.y = bullet.y;
addChild(poof);
target1.gotoAndPlay(21);
removeChild(bullet);
playerScore++; //increase playerScore by 1
updateTextFields();
} else {
removeChild(bullet);
bullet = null;
}
}
}
I tried adding an else if statement like so:
Code:
else if(bullet.hitTestObject(backtrees)) {
poof.x = bullet.x;
poof.y = bullet.y;
addChild(poof);
removeChild(bullet);
}
but that did nothing as well. As you can see, it is not the click of the user that causes the target to disappear, but the hittestobject that causes it to disappear. Would any one have an idea on what is causing this? Thank you so so much in advance!!!
-
Senior Member
Try this:
var _boundsm2:int = m2.x + m2.width;
var _boundsm1:int = m1.x + m1.width;
if (m3.hitTestObject(m1))
{
if((_boundsm1 > _boundsm2) || m1.x < m2.x)
{
trace ("HIT");
}
}
m2 is the tree in this example, m3 the bullet and m1 the target.
- The right of the People to create Flash movies shall not be infringed. -
-
Thank you for your response! I just got around to trying out the code today. It's a bit glitchy though and I've been messing with it all morning. The best I seem to be able to do is get it so that the Target doesn't click when behind the Tree...but it also doesn't click at all. This is what I tried that worked closest:
Code:
var boundsTree1:int=Tree1.x + Tree1.width;
var boundstarget1:int=target1.x + target1.width;
if (bullet.hitTestObject(target1)) {
if (boundsTree1 < boundstarget1) {
poof.x = bullet.x;
poof.y = bullet.y;
addChild(poof);
target1.gotoAndPlay(21);
removeChild(bullet);
playerScore++; //increase playerScore by 1
updateTextFields();
}else{
removeChild(bullet);
bullet=null;
}
Any thoughts???
Thank you!!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|