TouchEvent.TOUCH_BEGIN上的AS3循环移动

时间:2014-03-05 03:40:20

标签: actionscript-3 touch

My Code

编辑:抱歉让我的代码在外部链接中,但它太大了。

我一直在使用此代码尝试让我的角色向某个方向移动,并在屏幕仍然被打孔时继续移动。我可以正确地计算出我需要进入的方向,但问题是这段代码冻结了。

有人有任何提示吗?

1 个答案:

答案 0 :(得分:0)

'while'循环锁定你,因为它的功能范围内没有出路。循环繁忙,编译器无法对MOUSE_UP起作用。 如果你在那里放一条跟踪而不是拨打moveCharacter(),你就会明白我的意思。等待调试编译器放弃,大约15秒。并打印出一千条痕迹然后你的'屏幕被释放'。

这将使用ENTER_FRAME循环而不是'while':

package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;

public class Main extends Sprite
        {
                public var MySprite:Sprite;
                public var touchDown:Boolean;
                var MySpriteImage:Sprite = new redSprite;

                public function Main():void
                {
                        MySprite = new Sprite();
                        MySprite.x = 200;
                        MySprite.y = 200;

                        var world:Sprite = new Sprite();

                        //world.width = 320;
                        //world.height = 480;
                        addChild (world);

                        MySprite.addChild(MySpriteImage);

                        world.addChild(MySprite);

                        stage.addEventListener (MouseEvent.MOUSE_DOWN, touchHandler);
                        stage.addEventListener(MouseEvent.MOUSE_UP, removeTouch);
                }

                private function touchHandler(e:MouseEvent):void
                {
                        //stage.removeEventListener(MouseEvent.MOUSE_DOWN, touchHandler);
                        trace("Screen Touched");
                        this.touchDown = true;
                        trace (this.touchDown);

                        if (touchDown == true && e.stageX > 160)
                          moveCharacterRight ();
                        if (touchDown == true && e.stageX < 160)
                          moveCharacterLeft ();  
                  }


                private function removeTouch(e:MouseEvent):void
                {
                        this.touchDown = false;
                        trace("Screen Released");
                        trace (this.touchDown);
                }

                private function moveCharacterRight():void
                {
                    addEventListener (Event.ENTER_FRAME, movitRight)
                    function movitRight(e)
                        {
                                MySprite.x += 10;
                                if (touchDown == false){removeEventListener(Event.ENTER_FRAME, movitRight)}
                        }
                }

                private function moveCharacterLeft():void
                {
                    addEventListener (Event.ENTER_FRAME, movitLeft)
                    function movitLeft(e)
                        {
                                MySprite.x -= 10;
                                if (touchDown == false){removeEventListener(Event.ENTER_FRAME, movitLeft)}
                        }
                }

        }
}
为了清楚起见,我让它变得有点过于冗长。你能弄清楚如何简化吗?