Question

i am attempting to make an interactive scene fot my nexus 7 using actionscript. i am attempting to make a nape body to move continuously while a button is being pressed. the trace(event.target.name); in the tap handler is returning the instance name of the button being pressed as i expected it to but the same trace statement in the enter_frame pressed function is returning the buttons parent name and thus the movement is not happening below is my code..

resetBtn.addEventListener(TouchEvent.TOUCH_BEGIN , tapHandler);
ballBtn.addEventListener(TouchEvent.TOUCH_BEGIN , tapHandler );
leftArrow.addEventListener(TouchEvent.TOUCH_BEGIN , tapHandler );
rightArrow.addEventListener(TouchEvent.TOUCH_BEGIN , tapHandler );

private function tapHandler(event:TouchEvent):void
{
    //listen for mouse up on the stage, in case the finger moved off of the button accidentally when they release.
    rightArrow.addEventListener(TouchEvent.TOUCH_END, endTouch);
    //while the mouse is down, run the tick function once every frame as per the project frame rate
        addEventListener(Event.ENTER_FRAME, pressed); 

    trace(event.target.name);           
    //ball.applyImpulse(new Vec2(25,0));
}

function endTouch(e:Event):void 
{
    removeEventListener(Event.ENTER_FRAME, pressed);  
    //stop running the tick function every frame now that the mouse is up
    this.removeEventListener(TouchEvent.TOUCH_END,endTouch); 
    //remove the listener for endTouch
}
function pressed(e:Event):void 
{
    space.step(1 / stage.frameRate);

    trace(e.target.name);           
    if(e.target.name == "leftArrow")
    {
        trace("going left");
        crane.position.x -=  5;
        boom.position.x -=  5;
        c.position.x -=  5;
    }
    if(e.target.name == "rightArrow")
    {
        trace("going Right");
        crane.position.x +=  5;
        boom.position.x +=  5;
        c.position.x +=  5;
    }
}

if you can help me fix this or suggest a better way of achieving this wour help would be greatly appreciated.

Was it helpful?

Solution

Your issue is basically that you're adding the listener to the parent for the Enter_frame events so the target when that event dispatches isn't your buttons as you desire. I think instead of moving the listener though you're better off using a variable to hold a reference to your buttons like this:

private var curButton:Sprite;

resetBtn.addEventListener(TouchEvent.TOUCH_BEGIN , tapHandler);
ballBtn.addEventListener(TouchEvent.TOUCH_BEGIN , tapHandler );
leftArrow.addEventListener(TouchEvent.TOUCH_BEGIN , tapHandler );
rightArrow.addEventListener(TouchEvent.TOUCH_BEGIN , tapHandler );

private function tapHandler(event:TouchEvent):void
{
    curButton = event.target;
    curButton.addEventListener(TouchEvent.TOUCH_END, endTouch);

    addEventListener(Event.ENTER_FRAME, pressed); 
}

function endTouch(e:Event):void 
{
    trace("touch end received");
    removeEventListener(Event.ENTER_FRAME, pressed);  
    curButton.removeEventListener(TouchEvent.TOUCH_END, endTouch);
    curButton = null;
}
function pressed(e:Event):void 
{
    trace("currently pressed");
    space.step(1 / stage.frameRate);

    if(curButton == leftArrow)
    {
        trace("going left");
        crane.position.x -=  5;
        boom.position.x -=  5;
        c.position.x -=  5;
    }
    if(curButton == rightArrow)
    {
        trace("going Right");
        crane.position.x +=  5;
        boom.position.x +=  5;
        c.position.x +=  5;
    }
}

Okay see my edits above, hopefully should help resolve. Also as it seems you've probably already figured out the instance name that you give a variable when you define it, isn't the same as the .name property or the .id property, the instance name is no longer available at run time (if you really want to you can use the name property but you have to populate it yourself, and generally it's best to just use the instance names in the code instead, as this is checked at compile time, and incurs no extra run-time overhead).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top