我正在开发一款游戏,针对Android和ios的AS3 Adobe air,其中你有一个中心的影片剪辑,以及两个应该移动该影片剪辑的按钮(左和右)。我的目标是让左/右之间的转换尽可能顺畅:
- 如果播放器正在触摸左按钮,则影片剪辑向左移动。如果他从左按钮移开他的手指,并立即触摸右按钮,则影片剪辑将不会移动,直到他重新触摸右按钮,影片剪辑向右移动。我试图实现多点触控事件,但我似乎有些不对劲,因为这是我得到的行为
- 如果播放器正在触摸左侧按钮,则影片剪辑会按预期向左移动,如果他触摸右侧按钮,则影片剪辑会按预期停止,但如果他将手指从左侧按钮移开,同时将其保持在右侧按钮上,影片剪辑仍然会冻结而不会移动,它应该移动到右边
这是我正在使用的代码:
leftButtonCreated.addEventListener(TouchEvent.TOUCH_BEGIN,mouseDown);
rightButtonCreated.addEventListener(TouchEvent.TOUCH_BEGIN,mouseDown2);
stage.addChild(leftButtonCreated);
stage.addChild(rightButtonCreated);
function mouseDown(e:TouchEvent):void
{
stage.addEventListener(TouchEvent.TOUCH_END,mouseUp1);
//listen for mouse up on the stage, in case the finger/mouse moved off of the button accidentally when they release.;
addEventListener(Event.ENTER_FRAME,myButtonClick);//while the mouse is down, run the tick function once every frame as per the project frame rate
}
function mouseUp1(e:TouchEvent):void
{
removeEventListener(Event.ENTER_FRAME,myButtonClick);//stop running the tick function every frame now that the mouse is up
stage.removeEventListener(TouchEvent.TOUCH_END,mouseUp1);
}
function mouseDown2(e:TouchEvent):void
{
stage.addEventListener(TouchEvent.TOUCH_END,mouseUp2);
//listen for mouse up on the stage, in case the finger/mouse moved off of the button accidentally when they release.;
addEventListener(Event.ENTER_FRAME,stopDragging);//while the mouse is down, run the tick function once every frame as per the project frame rate
}
function mouseUp2(e:TouchEvent):void
{
removeEventListener(Event.ENTER_FRAME,stopDragging);//stop running the tick function every frame now that the mouse is up
stage.removeEventListener(TouchEvent.TOUCH_END,mouseUp2);
}
function stopDragging(ev2:Event):void
{
if (MC.x <= rightButtonCreated.x)
{
MC.x = MC.x + 10;
}
}
function myButtonClick(ev:Event):void
{
if (MC.x > leftButtonCreated.x)
{
MC.x = MC.x - 10;
}
else
{
}
}
Code最初设置为mouseEvents,所以我试图转移到触摸事件,所以我可以解决这个问题,上面的代码是我得到的。谢谢你的时间。
编辑:
并且,我使用以下代码:
var leftButtonCreated:leftB= new leftB();
答案 0 :(得分:1)
您当前的问题是,释放左键后会触发mouseUp1
和mouseUp2
,因为它们都附加到舞台上。但你的实际问题更深刻。如果按下相应的按钮,则应首先左右移动对象,并使用TouchEvent.touchPointID
跟踪已释放的触摸以了解已释放的按钮。
另外一个潜在的警告:如果您同时触摸左右按钮,然后在保留两个触摸的同时交换手指,然后松开右手按钮上的手指 - 您的对象应该向左或向右移动?我说正确的答案是在右边,因为释放的手指对应于左按钮。
leftButtonCreated.addEventListener(TouchEvent.TOUCH_BEGIN,touchDown);
rightButtonCreated.addEventListener(TouchEvent.TOUCH_BEGIN,touchDown);
leftButtonCreated.addEventListener(TouchEvent.TOUCH_END,touchUp);
rightButtonCreated.addEventListener(TouchEvent.TOUCH_END,touchUp);
addEventListener(Event.ENTER_FRAME,moveObject);
function touchDown(e:TouchEvent):void {
var button:DisplayObject=e.currentTarget as DisplayObject;
if (!button) return; // can't fail typecast to DisplayObject in this context, but leave for good measure
if (button==leftButtonCreated) {
leftButtonPressed=true;
leftButtonTouchID=e.touchPointID;
return;
}
if (button==rightButtonCreated) {
rightButtonPressed=true;
rightButtonTouchID=e.touchPointID;
return;
}
}
function touchUp(e:TouchEvent):void {
var button:DisplayObject=e.currentTarget as DisplayObject;
if (!button) return;
var ti:int;
if (button==leftButtonCreated) {
ti=leftButtonTouchID;
if (ti==e.touchPointID) {
leftButtonPressed=false;
}
}
if (button==rightButtonCreated) {
ti=rightButtonTouchID;
if (ti==e.touchPointID) {
rightButtonPressed=false;
}
}
}
function moveObject(e:Event):void {
if (leftButtonPressed) MC.x-=10;
if (rightButtonPressed) MC.x+=10;
if (MC.x<leftButtonCreated.x) MC.x=leftButtonCreated.x;
if (MC.x>rightButtonCreated.x) MC.x=rightButtonCreated.x;
}
编辑:显然SimpleButton
不允许事件向外传播给父母。好的,这仍然可以解决,但您必须在Main
课程中存储所需的属性。
var leftButtonPressed:Boolean=false;
var rightButtonPressed:Boolean=false;
var leftButtonTouchID:int=0;
var rightButtonTouchID:int=0;
上述代码已更新。请与SimpleButton
一起使用时直接使用var leftButtonCreated:leftB= new leftB();
。