Copy link to clipboard
Copied
i have 4 buttons (pink_harm, pink_not, blue_non, and blue_flam) in which you need to drag and hit the corresponding targets. and here is the code.
if (pink_harm.hitTestObject(pink_target))
{
score += 1;
right.play();
pink_not.mouseEnabled = false;
}
else if (pink_not.hitTestObject(pink_target))
{
score -= 1;
wrong.play();
pink_harm.mouseEnabled = false;
}
else if (blue_non.hitTestObject(blue_target))
{
score += 1;
right.play();
blue_flam.mouseEnabled = false;
}
else if (blue_flam.hitTestObject(blue_target))
{
score -= 1;
wrong.play();
blue_non.mouseEnabled = false;
}
if (score < 0) score = 0;
ScoreDisplay.text = String(score);
}
the problem is that everytime i click and drag the pink buttons first then followed by the blue ones they either add to the score even though the blue one is incorrect or do not add anymore even though the next one is correct. but when i drag the blue buttons first they work fine.
Message was edited by: cams_zalzos
You should know better than anyone what you need to be using, I can only suggest based on thinking I know what you are trying to do.
In the code you show you are using the currentClip for part of it and not for the rest. I think what you want to do in to determine if the currentClip is the pink or the blue, and for whichever it is, test the rest of the conditions. That way you are not testing an object that has not just been dropped.
if(currentClip == pink_harm && pink_harm.hitTestObject(pink_ta
...Copy link to clipboard
Copied
What happens to the objects after they pass a hitTest as true. If they remain where you left them, they will still register the hit as true.
Copy link to clipboard
Copied
yes they remain there. is it possible to register the hit as false after hitting the target?
Copy link to clipboard
Copied
If the hit exists, it cannot be false.
What you probably need to do is to use information from the object being dragged to have your code only focus on that object and no others. I don't think you are showing all of the code related to this hitTest checking. If this is part of a function that results from a MOUSE_UP (stopDrag) event, then you should be able to make use of the event.currentTarget to determine which object needs to be checked for the hit.
Copy link to clipboard
Copied
here is the full code for the hitTest checking
function item_onMouseDown(event:MouseEvent):void {
currentClip = MovieClip(event.currentTarget);
startX = currentClip.x;
startY = currentClip.y;
addChild(currentClip); //bring to the front
currentClip.startDrag();
stage.addEventListener(MouseEvent.MOUSE_UP, stage_onMouseUp);
}
function stage_onMouseUp(event:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_UP, stage_onMouseUp);
currentClip.stopDrag();
var index:int = dragArray.indexOf(currentClip);
var matchClip:MovieClip = MovieClip(matchArray[index]);
if(currentClip.hitTestObject(matchClip)) {
currentClip.x = matchClip.x;
currentClip.y = matchClip.y;
currentClip.removeEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDown);
currentClip.buttonMode = false;
if (currentClip.hitTestObject(pink_target))
{
score += 1;
right.play();
pink_not.mouseEnabled = false;
}
else if (currentClip.hitTestObject(pink_target))
{
score -= 1;
wrong.play();
pink_harm.mouseEnabled = false;
}
else if (blue_non.hitTestObject(blue_target))
{
score += 1;
right.play();
blue_flam.mouseEnabled = false;
}
else if (blue_flam.hitTestObject(blue_target))
{
score -= 1;
wrong.play();
blue_non.mouseEnabled = false;
}
if (score < 0) score = 0;
ScoreDisplay.text = String(score);
}
else {
currentClip.x = startX;
currentClip.y = startY;
}
}
so i should use the currentClip instead of the button itself?
Copy link to clipboard
Copied
You should know better than anyone what you need to be using, I can only suggest based on thinking I know what you are trying to do.
In the code you show you are using the currentClip for part of it and not for the rest. I think what you want to do in to determine if the currentClip is the pink or the blue, and for whichever it is, test the rest of the conditions. That way you are not testing an object that has not just been dropped.
if(currentClip == pink_harm && pink_harm.hitTestObject(pink_target)) {
score += 1;
right.play();
pink_not.mouseEnabled = false;
} else if (currentClip == pink_not && pink_not.hitTestObject(pink_target)) { score -= 1;
wrong.play();
pink_harm.mouseEnabled = false;
} else if(currentClip == blue...etc....
Copy link to clipboard
Copied
Thank you! It is working now!
Sorry about that. I'm still a newbie in AS3 coding and I can only understand a little of the codes. I only rely on tutorials available in the net. I really appreaciate your time for helping me.
Copy link to clipboard
Copied
You're welcome
Find more inspiration, events, and resources on the new Adobe Community
Explore Now