0

我目前正在为 Android 制作 2d 游戏。

我现在在角色的移动方面遇到了麻烦。

这是我想要做的:

  1. 当我触摸左/右按钮时,英雄应该向左/右走。

  2. 如果 HERO 不在 STAGE 的中间,他应该继续步行直到到达 STAGE 的中间。

  3. 当他到达舞台中间时。我希望背景移动而不是英雄。

您能否告诉我应该如何验证代码,以便(HERO.x = 舞台中间)背景开始移动?

谢谢 :) 这是右侧按钮的代码片段,它不能按预期工作。

btnright.addEventListener(TouchEvent.TOUCH_BEGIN,onright);
addEventListener(Event.ENTER_FRAME,goright);


function onright(e:TouchEvent):void{
    istouching = true;
}

function goright(e:Event):void{

    if(istouching){ //if the button is touched
        hero.x+=10;
        hero.gotoAndStop("walking");
        hero.scaleX=1;

        if(hero.x == stage.stageWidth*.5){ //if HERO reaches the middle of the stage
            bg.x-=5;
            hero.gotoAndStop("walking");
            hero.scaleX=1;
        }
    }
}
4

1 回答 1

1

你总是在触摸英雄!并且您的“如果等于中心”语句仅在 1 个像素上触发,您需要“大于”

if(istouching){ //if the button is touched

    hero.gotoAndStop("walking");
    hero.scaleX=1;

    if(hero.x > stage.stageWidth*.5){ //if HERO reaches the middle of the stage
        bg.x-=5;

    }else{
        hero.x+=10;
     }
}
于 2013-03-09T17:36:02.650 回答