从穿墙移动AS3停止字符穿墙、字符

2023-09-08 14:19:04 作者:不问归期

我想停止影片剪辑的运动,当它击中墙壁(其它影片剪辑)。 以下作品中的例子,但碰撞后的影片剪辑块所有的移动到左侧... 我想问你的是,这是一个好办法,为什么不是它的工作呢?

将有这个code不对劲,但是我学习。 对于现在的例子与LEFTARROW键;

变量来检查按键,如果它击中墙壁,如果它的移动与否:

  VAR LEFTARROW:布尔;
变种速度:INT = 10;
VAR打击:布尔;
VAR ismoving:布尔;
 

事件侦听器键/移动和碰撞检测:

  stage.addEventListener(KeyboardEvent.KEY_DOWN,关键pressed);
stage.addEventListener(KeyboardEvent.KEY_UP,调用keyReleased);
stage.addEventListener(Event.ENTER_FRAME,步行);
stage.addEventListener(Event.ENTER_FRAME,detectHit);
 

碰撞检测功能:

 函数detectHit(五:事件):无效
{
   如果(char.hitTestObject(边界))
   {
       打= TRUE;
   }
}
 
用PHOTOSHOP CS6创建一个3D文字的片头动画

功能左方向键:

 功能键pressed(事件:KeyboardEvent的):无效
{

    如果(event.key code == Keyboard.LEFT)
    {
       LEFTARROW = TRUE;
    }

}

函数调用keyReleased(事件:KeyboardEvent的):无效
{

    如果(event.key code == Keyboard.LEFT)
    {
        LEFTARROW = FALSE;
    }

}
 

和它不工作的原因可能是在这里,但我不明白为什么不:

 函数步行(事件:事件):无效{
    如果(向右键){
        char.x + =速度;
    }

    如果(LEFTARROW&安培;&安培;!击中){
        char.x  -  =速度;
    }
    其他
    {
        ismoving = FALSE
    }
 

解决方案

 如果(LEFTARROW&安培;&安培;!打)
 

字符将移动如果是假的。当 char.hitTestObject(边界)是真的,你要设置为true。您还没有设置 再次击中,以虚假的任何地方。这就是为什么一旦离开墙打它永久地停止向左移动。你需要找出合适的条件设置假了。

添加在 detectHit 的else分支应该解决的问题。

 函数detectHit(五:事件):无效{
   如果(char.hitTestObject(边界))
   {
       打= TRUE;
   } 其他 {
       命中= FALSE; //添加此
   }
}
 

I want to stop the movieclips movement when it hits a wall (another movieclip). The example below works, but after the collision the movieclip 'blocks' all movement to the left... My question to you is, is this a good way and why isn't it working well?

There will be something wrong in this code, but i'm learning. For now the example with the leftArrow key;

variables to check the key, if it's hitting the walls and if it's moving or not:

var leftArrow:Boolean;
var speed:int = 10;
var hitting:Boolean;
var ismoving:Boolean;

event listeners for the keys/movement and detecting collision:

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
stage.addEventListener(Event.ENTER_FRAME, walking);
stage.addEventListener(Event.ENTER_FRAME, detectHit);

detecting collision function:

function detectHit(e:Event) :void
{
   if(char.hitTestObject(bounds))
   {
       hitting = true;
   }
}

function to the left arrow key:

function keyPressed(event:KeyboardEvent):void
{

    if (event.keyCode == Keyboard.LEFT)
    {
       leftArrow = true;
    }

}

function keyReleased(event:KeyboardEvent):void 
{

    if (event.keyCode == Keyboard.LEFT) 
    {
        leftArrow = false;
    }

}

And the reason it's not working is probably here, but I don't understand why not:

function walking(event:Event):void {
    if (rightArrow) {
        char.x += speed;    
    }

    if (leftArrow && ! hitting) {
        char.x -= speed;
    }
    else
    {
        ismoving = false
    }

解决方案

if (leftArrow && ! hitting)

char will move if hitting is false. When char.hitTestObject(bounds) is true you are setting hitting to true. You are not setting hitting again to false anywhere. That's why once left wall is hit it stops left movement permanently. You need to figure out suitable condition to set hitting to false again.

Adding an else branch in detectHit should solve the problem.

function detectHit(e:Event):void {
   if(char.hitTestObject(bounds))
   {
       hitting = true;
   } else { 
       hitting = false;    // add this
   }
}