TouchEvent.TOUCH_BEGIN上的AS3循环移动



我的代码

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

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

有人有什么线索吗?

while循环将您锁定,因为在它的函数范围内没有出路。它忙于循环,以至于编译器无法对MOUSE_UP执行操作。如果你在那里放一个跟踪,而不是调用moveCharacter(),你就会明白我的意思。只需等待调试编译器在大约15秒内放弃,并打印出1000个跟踪,然后您的"屏幕释放">

这将起作用,使用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)}
}
}
}
}

为了清楚起见,我把它写得有点太冗长了。你能想出如何简化它吗?

最新更新