0

我正在尝试使用 actionscript 为我的 nexus 7 制作一个交互式场景。我正在尝试在按下按钮时让颈背身体连续移动。跟踪(event.target.name);在点击处理程序中,按我的预期返回正在按下的按钮的实例名称,但是 enter_frame 按下函数中的相同跟踪语句正在返回按钮父名称,因此下面的移动没有​​发生是我的代码..

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;
    }
}

如果您能帮我解决这个问题或提出更好的方法来实现这一点,我们将不胜感激。

4

1 回答 1

0

您的问题基本上是您将侦听器添加到 Enter_frame 事件的父级,因此该事件调度时的目标不是您想要的按钮。我认为与其移动侦听器,不如使用变量来保存对按钮的引用,如下所示:

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;
    }
}

好的,请参阅我上面的编辑,希望能帮助解决。此外,看起来您可能已经弄清楚了在定义变量时为其提供的实例名称,与 .name 属性或 .id 属性不同,实例名称在运行时不再可用(如果你真的想要,你可以使用 name 属性,但你必须自己填充它,通常最好只在代码中使用实例名称,因为这是在编译时检查的,并且不会产生额外的运行时高架)。

于 2013-03-25T03:26:55.883 回答