0

我正在尝试在动作脚本 3 中做一个碰撞示例。character当它碰到platform. 当我仅向右、向左、向上或向下移动时效果很好,但如果我尝试沿对角线移动,如果 与character碰撞platform,则对象会转到屏幕的不同区域。

这是编译的示例:http ://dl.dropbox.com/u/5282142/GameDemo.html

下面是我的代码。

现在,有没有人知道更好的方法来做我正在做的事情,或者当我尝试沿对角线移动角色时,我怎样才能让角色不去一个奇怪的位置?

var level:Array = new Array();
for (var i = 0; i < numChildren; i++) {
    if (getChildAt(i) is Platform) {
        level.push(getChildAt(i).getBounds(this));
    }
}

var speedX:int = 0;
var speedY:int = 0;

var kLeft:Boolean = false;
var kRight:Boolean = false;
var kDown:Boolean = false;
var kUp:Boolean = false;

stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUpHandler);

function onKeyDownHandler(event:KeyboardEvent):void {
    if (event.keyCode == 37) kLeft = true;
    if (event.keyCode == 38) kUp = true;
    if (event.keyCode == 39) kRight = true;
    if (event.keyCode == 40) kDown = true;
}

function onKeyUpHandler(event:KeyboardEvent):void {
    if (event.keyCode == 37) kLeft = false;
    if (event.keyCode == 38) kUp = false;
    if (event.keyCode == 39) kRight = false;
    if (event.keyCode == 40) kDown = false;
}

addEventListener(Event.ENTER_FRAME, loop);

function loop(event:Event):void {
    moveChar();
    bound();
}

function moveChar():void {
    if (kLeft) {
        speedX = -10;
    } else if (kRight) {
        speedX = 10;
    } else {
        speedX *= 0.5;
    }

    if (kUp) {
        speedY = -10;
    } else if (kDown) {
        speedY = 10;
    } else {
        speedY *= 0.5;
    }

    character.x += speedX;
    character.y += speedY;
}

function bound():void {
    if (character.x > (800 - character.width/2)){
        character.x = 800 - character.width/2;
    }
    if (character.x < (character.width/2)){
        character.x = character.width/2;
    }
    if (character.y > (480 - character.height/2)){
        character.y = 480 - character.height/2;
    }
    if (character.y < (character.height/2)){
        character.y = character.height/2;
    }
    for (i = 0; i < level.length; i++) {
        if (character.getBounds(this).intersects(level[i])) {
            if (speedX > 0) {
                character.x = level[i].left - character.width/2;
            }
            if (speedX < 0) {
                character.x = level[i].right + character.width/2;
            }
        }
    }
    for (i = 0; i < level.length; i++) {
        if (character.getBounds(this).intersects(level[i])) {
            if (speedY > 0) {
                character.y = level[i].top - character.height/2;
            }
            if (speedY < 0) {
                character.y = level[i].bottom + character.height/2;
            }
        }
    }
}
4

1 回答 1

0

您的问题在于代码的这一部分:

for (i = 0; i < level.length; i++) {
        if (character.getBounds(this).intersects(level[i])) {
            if (speedX > 0) {
                character.x = level[i].left - character.width/2;
            }
            if (speedX < 0) {
                character.x = level[i].right + character.width/2;
            }
        }
    }

for (i = 0; i < level.length; i++) {
            if (character.getBounds(this).intersects(level[i])) {
                if (speedY > 0) {
                    character.y = level[i].top - character.height/2;
                }
                if (speedY < 0) {
                    character.y = level[i].bottom + character.height/2;
                }
            }
        }

角色实际上并没有移动到随机位置。上面是你设置的。

您必须创建一个更好的逻辑(根据场景),当两个键同时按下并发生冲突时,您的代码不应混淆。

于 2012-04-12T09:01:43.483 回答