AS3平台斜率检测

时间:2017-07-20 10:55:48

标签: actionscript-3 actionscript collision

我正在AS3制作平台游戏。然而,我正在努力的一件事是如何管理斜率检测。喜欢像喜欢花式裤子冒险这样的游戏,但是我真的不喜欢使用像Box2D或CDK这样的外部东西,因为这两者都让我很困惑,而且id通常更喜欢没有一个。我所有的尝试都有问题,我可以让它与一些斜坡一起工作但不是全部(例如角色管理一个斜坡,但是通过另一个斜坡)然后当我改变它以适应另一个时,它不适合第一个(例如,玩家管理第二个斜坡,但在第二个斜坡上疯狂地反弹...... 所以,任何帮助将不胜感激...... 谢谢。 代码如下。

//var setup
var leftPressed: Boolean = false;
var rightPressed: Boolean = false;
var upPressed: Boolean = false;
var downPressed: Boolean = false;
var leftBumping: Boolean = false;
var rightBumping: Boolean = false;
var upBumping: Boolean = false;
var downBumping: Boolean = false;
var lowerleftBumping: Boolean = false;
var lowerrightBumping: Boolean = false;
var leftBumpPoint: Point = new Point(-30, -87);
var rightBumpPoint: Point = new Point(30, -87);
var lowerleftBumpPoint: Point = new Point(-32, -2);
var lowerrightBumpPoint: Point = new Point(32, -2);
var upBumpPoint: Point = new Point(0, -174);
var downBumpPoint: Point = new Point(0, 0);
var scrollX: Number = -4;
var scrollY: Number = -68;
var ySpeed: Number = 0;
var xSpeed: Number = 0;
var speedConstant: int = 5;
var maxSpeedConstant: Number = 30;
var frictionConstant: Number = 0.9;
var gravityConstant: Number = 2;
var jumpConstant: Number = -45;
//game
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(Event.ENTER_FRAME, loop);
function keyDownHandler(e: KeyboardEvent): void {
    if (e.keyCode == Keyboard.LEFT) {
        leftPressed = true;

    } else if (e.keyCode == Keyboard.RIGHT) {
        rightPressed = true;

    } else if (e.keyCode == Keyboard.UP) {
        upPressed = true;

    } else if (e.keyCode == Keyboard.DOWN) {
        downPressed = true;
    }
}

function keyUpHandler(e: KeyboardEvent): void {
    if (e.keyCode == Keyboard.LEFT) {
        leftPressed = false;

    } else if (e.keyCode == Keyboard.RIGHT) {
        rightPressed = false;

    } else if (e.keyCode == Keyboard.UP) {
        upPressed = false;

    } else if (e.keyCode == Keyboard.DOWN) {
        downPressed = false;
    }
}

function loop(e: Event): void {
    if (back.hitTestPoint(player.x + leftBumpPoint.x, player.y + leftBumpPoint.y, true)) {
        trace("leftBumping");
        leftBumping = true;
    } else {
        leftBumping = false;
    }

    if (back.hitTestPoint(player.x + rightBumpPoint.x, player.y + rightBumpPoint.y, true)) {
        trace("rightBumping");
        rightBumping = true;
    } else {
        rightBumping = false;
    }

    if (back.hitTestPoint(player.x + upBumpPoint.x, player.y + upBumpPoint.y, true)) {
        trace("upBumping");
        upBumping = true;
    } else {
        upBumping = false;
    }

    if (back.hitTestPoint(player.x + downBumpPoint.x, player.y + downBumpPoint.y, true)) {
        trace("downBumping");
        downBumping = true;
    } else {
        downBumping = false;
    }
            if (back.hitTestPoint(player.x + lowerleftBumpPoint.x, player.y + lowerleftBumpPoint.y, true)) {
            trace("lowerrightBumping");
            lowerleftBumping = true;
        } else {
            lowerleftBumping = false;
        }
        if (back.hitTestPoint(player.x + lowerrightBumpPoint.x, player.y + lowerrightBumpPoint.y, true)) {
            trace("lowerrightBumping");
            lowerrightBumping = true;
        } else {
            lowerrightBumping = false;
        }
    if (leftPressed) {
        xSpeed -= speedConstant;

    } else if (rightPressed) {
        xSpeed += speedConstant;

    }
    if (leftBumping) {
        if (xSpeed < 0) {
            xSpeed *= -0.5;
        }
    }

    if (rightBumping) {
        if (xSpeed > 0) {
            xSpeed *= -0.5;
        }
    }

    if (upBumping) {
        if (ySpeed < 0) {
            ySpeed *= -0.5;
        }
    }

    if (downBumping) {
        if (ySpeed > 0) {
            ySpeed = 0; //set the y speed to zero
        }
        if (upPressed) {
            ySpeed = jumpConstant;
        }
    } else {
        ySpeed += gravityConstant;
    }
    if (xSpeed > maxSpeedConstant) { //moving right
        xSpeed = maxSpeedConstant;
    } else if (xSpeed < (maxSpeedConstant * -1)) { //moving left
        xSpeed = (maxSpeedConstant * -1);
    }
    xSpeed *= frictionConstant;
    ySpeed *= frictionConstant;
    if (Math.abs(xSpeed) < 0.5) {
        xSpeed = 0;
    }

    scrollX -= xSpeed;
    scrollY -= ySpeed;

    back.x = scrollX;
    back.y = scrollY;
    if (scrollY < -770) {
        scrollX = -4;
        scrollY = -68;
        ySpeed=0
        xSpeed=0
    }
}

1 个答案:

答案 0 :(得分:1)

如果我被迫不使用我生命中的爱(Box2D),那么我想我会和hitTestObject一起玩,并对我的任何一个斜坡的角度进行一些数学计算以产生它们的速率会开始。这个问题有点宽泛,我认为这里的社区更多是针对特定问题而不是架构头脑风暴。