闪光碰撞检测(平台游戏)

时间:2013-04-19 17:41:59

标签: actionscript-3 flash collision-detection game-physics rectangles

我正在尝试使用ActionScript 3在flash中制作一个平台游戏。目前除了一件事外,一切都正常。

问题:

当玩家与平台底部发生碰撞时,如果他的xVel不等于零,则会调用水平碰撞检测循环,并且玩家可以水平移动也可以垂直移动。这意味着他从下面从平台弹回,但也移到了平台的一侧。如果玩家的xVel等于零,一切正常。这是因为没有调用水平碰撞循环。我无法弄清楚为什么会这样。任何帮助将不胜感激。

守则:

import flash.events.Event;
import flash.geom.Rectangle;

var level:Array = new Array();

var xVel = 0;
var yVel = 0;

var xSpeed = 15;

var accel =1.5;
var grav = 2;
var jumpHeight = 15*grav;


for(var i = 0; i<numChildren;i++){
    if(getChildAt(i) is platform){
        level.push(getChildAt(i).getRect(this));
    }   
}

var upKeyDown = false;
var leftKeyDown = false;
var rightKeyDown = false;
var downKeyDown = false;


stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDown);
stage.addEventListener(KeyboardEvent.KEY_UP,keyUp);
stage.addEventListener(Event.ENTER_FRAME,gameLoop);

function keyDown(e:KeyboardEvent){
    if(e.keyCode == Keyboard.UP){
        upKeyDown = true;
    }   
    if(e.keyCode == Keyboard.LEFT){
        leftKeyDown = true;
    }   
    if(e.keyCode == Keyboard.RIGHT){
        rightKeyDown = true;
    }   
    if(e.keyCode == Keyboard.DOWN){
        downKeyDown = true;
    }   
}
function keyUp(e:KeyboardEvent){
    if(e.keyCode == Keyboard.UP){
        upKeyDown = false;
    }   
    if(e.keyCode == Keyboard.LEFT){
        leftKeyDown = false;
    }   
    if(e.keyCode == Keyboard.RIGHT){
        rightKeyDown = false;
    }   
    if(e.keyCode == Keyboard.DOWN){
        downKeyDown = false;
    }   
}

function gameLoop(e:Event){
    if(rightKeyDown){
        if(xVel<xSpeed){
            xVel+=accel;
        }

    }else if(leftKeyDown){
        if(xVel>-xSpeed){
            xVel-=accel;
        }

    }else{
        xVel *=0.6;
    }
    //horizontal
    player.x+=xVel;
    for(i = 0; i<level.length;i++){
        if(player.getRect(this).intersects(level[i])){
            if(xVel>0){
                player.x = level[i].left-player.width/2;
            }
            if(xVel<0){
                player.x = level[i].right+player.width/2;
            }
            xVel = 0;
        }
    }

    yVel+=grav;
    player.y+=yVel;
    var jumpable = false;
    for(i = 0; i<level.length;i++){
        if(player.getRect(this).intersects(level[i])){
            if(yVel>0){
                player.y = level[i].top-player.height/2;
                yVel = 0;
                jumpable = true;
            }
            if(yVel<0){
                player.y = level[i].bottom+player.height/2;
                yVel*=-0.5;
            }
        }
    }
    if(upKeyDown&&jumpable){
        jump();
    }
    this.x = -player.x+(stage.stageWidth/2);
    this.y = -player.y+(stage.stageHeight/2);
}

function jump(){
    yVel-=jumpHeight;   
}

我认为问题出现

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

    yVel+=grav;
    player.y+=yVel;
    var jumpable = false;
    for(i = 0; i<level.length;i++){
        if(player.getRect(this).intersects(level[i])){
            if(yVel>0){
                player.y = level[i].top-player.height/2;
                yVel = 0;
                jumpable = true;
            }
            if(yVel<0){
                player.y = level[i].bottom+player.height/2;
                yVel*=-0.5;
            }
        }
    }

图片!

A visual description

额外信息:

平台是影片剪辑符号,所有这些符号都源自相同的符号。它们被拖到画布上并重新调整大小。平台符号具有称为“平台”的AS链接,这是子代在代码中被识别为平台的方式

播放器是一个没有动画的矩形

平台和播放器都在对象的中心有一个方向。

1 个答案:

答案 0 :(得分:1)

您需要先测试天花板碰撞并在水平检查前进行相应调整。如果不这样做,则在进行水平检查时,您的播放器会与平台底部发生碰撞。