Eric Wooley
Eric Wooley

Reputation: 643

As3 Touch Events affecting eachother

I have a direction and a sprint button. What i want to happen is: while the sprint button is held down the player moves twice as fast. Then when the sprint button is released the player should continue walking at regular speed. What happens though is the player stops when the sprint button is released even though the directional button is still held down.

public function addControllerListeners(){
        controller.leftButton.addEventListener(TouchEvent.TOUCH_ROLL_OVER, moveCharLeft);
        controller.leftButton.addEventListener(TouchEvent.TOUCH_BEGIN, moveCharLeft);
        controller.leftButton.addEventListener(TouchEvent.TOUCH_END, playerStop);
        controller.leftButton.addEventListener(TouchEvent.TOUCH_OUT, playerStop);
        controller.leftButton.addEventListener(TouchEvent.TOUCH_ROLL_OUT, playerStop);

        sprintButton.addEventListener(TouchEvent.TOUCH_ROLL_OVER, playerSprint);
        sprintButton.addEventListener(TouchEvent.TOUCH_BEGIN, playerSprint);
        sprintButton.addEventListener(TouchEvent.TOUCH_END, playerStopSprint);
        sprintButton.addEventListener(TouchEvent.TOUCH_OUT, playerStopSprint);
        sprintButton.addEventListener(TouchEvent.TOUCH_ROLL_OUT, playerStopSprint);
}

public function playerSprint(e:Event = null) {
        playerChar.sprint = true;
        trace("Player Sprint");
}
public function playerStopSprint(e:Event = null) {
        playerChar.sprint = false;
        trace("stopSprinting");
}
public function moveCharLeft(event:TouchEvent):void{
        playerChar.setWalk("left");
}
public function playerStop(event:TouchEvent):void {
        var target:String = event.target.name;
        if(target == "upButton" || target == "rightButton" || target == "downButton" || target == "leftButton"){
            trace("Player Stopped from: " + event.target.name + " - " + event.currentTarget.name);
            playerChar.direction = "idle";
        }
    }

The trace in the stop player function says the target name is whichever controller button was being held down. Even though the button being released is the sprint button.

If it helps the buttons were added on the stage. I don't think that should make a difference, but you never know.

Upvotes: 0

Views: 1440

Answers (1)

iND
iND

Reputation: 2649

Events (touches) are not persistent. When an event fires, it's done, and you have to poll for the next event firing. The auto-repeat is a feature of the OS, and that (usually) gets interrupted by another similar event (the second touch will stop the first, I think).

Use a flag indicating whether a certain condition is met. You will have to turn this boolean on and off manually, but it will be able to keep track of what you need to know. (A similar example of this is found in this SO comment: https://stackoverflow.com/a/7976038/516537. This happens to be for keyboard events which can both repeat and co-exist with other keyboard events, but you can see the use of the flag.) So, listen for your events, but also:

  1. Flag on when an condition becomes active.
  2. Flag off when an condition becomes inactive.
  3. Check for multiple flags being on to activate complex conditions.

What you gain is persistence of an event. If a TOUCH_BEGIN (or other starting) event fires, but no TOUCH_END (or other ending) event is fired, the flag will be your way of knowing that the touch event still exists.

The caveat to all this is that you will have to poll for events in a slightly different way. You will have to add a Event.ENTER_FRAME event listener to keep checking which events are still active.

HTH.

Upvotes: 1

Related Questions