1
\$\begingroup\$

I have a simple problem. I have a button on the screen. I want isActionDown to push the button down, and isActionUp to push the button back up again and make my character jump. But the problem is if I push the button down and drag away from it, the button stays pushed down. I would like the button to push back up again if the user drags away from it.

How can I create the desired affect? isActionCancel doesn't seem to do the trick.

    private void addButton(){
goB = new AnimatedSprite(240, 364, ResourceManager.getInstance().go_region, vbom){
    @Override
    public boolean onAreaTouched(final TouchEvent event, final float x, final float y){


        if (event.isActionDown()){
            goB.animate(new long[]{ 250 }, new int[] { 1 }, true);
            /*if(event.isActionCancel()){
                goB.animate(new long[]{ 250 }, new int[] { 0 }, true);
            }*/
        }
        if (event.isActionUp()){
            goB.animate(new long[]{ 250 }, new int[] { 0 }, true);
            fresh = true;
            playerBody.applyLinearImpulse(new Vector2(0, 45), playerBody.getPosition());
        }


        if (event.isActionCancel() || event.isActionOutside()){
            goB.animate(new long[]{ 250 }, new int[] { 0 }, true);
        }
        return true;
    }
};
goB.setScale(3);

registerTouchArea(goB);
attachChild(goB);

goB.animate(250);
}

I'm having the hardest time with this, and I've tried all sorts of variations of the code above. Please let me know if you have the solution.

Thanks for your help!

\$\endgroup\$
1
\$\begingroup\$

onAreaTouched Is triggered when the button is pressed, and that press is inside its bounds. This means the event will never be isActionOutside() or isActionCancel(), because a button simply wouldn't receive events like this.

You need to have a global listener that listens for events. All your components talk to your global listener and the global listener watches for events that are outside the bounds of your components. For example, you'd have your button notify the global listener that it's being pressed, then if the global listener hears about another event that's not inside the button's bounds (like an ActionUp event), the global listener can tell the button that it's no longer being pressed.

This is a common problem with event systems. It's more complex to remember who received what event and if they should be notified about future events based on the initial event type. That's why it's up to you to write this functionality if you want it.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.