Skip to main content
Inspiring
January 6, 2013
解決済み

code error

  • January 6, 2013
  • 返信数 1.
  • 946 ビュー

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

このトピックへの返信は締め切られました。
解決に役立った回答 Ned Murphy

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?


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....

返信数 1

Ned Murphy
Legend
January 6, 2013

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.

cams_zalzos作成者
Inspiring
January 6, 2013

yes they remain there. is it possible to register the hit as false after hitting the target?

Ned Murphy
Legend
January 6, 2013

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.