AS3碰撞检测 - 跳跃将角色推向地板

时间:2014-02-19 06:48:54

标签: actionscript-3 actionscript collision-detection collision side-scroller

Wellp,我到处都是,我就像他们来到这里一样绿色。

我最近开始玩AS3,因为我想学习它,所以我开始构建一个简单的平台游戏。

我想我知道我的问题是什么,但找到解决方案已经过了一半。

http://sta.sh/0ucxs1xjaua

根据我的研究,这是玩家位置计算/帧率的问题。在程序计算出与地板相关的位置之前,角色已经走得太远了,当它最终识别出他在哪里时,它开始重新校准,将他带回零。

我的特殊问题是每个制作教程和内容的人都使用不同的标签系统,而且我找不到任何适合这种特殊问题的东西。

请原谅我提问,但有没有人有机会查看我的代码并帮我找出问题所在。我觉得它与

有关

if(downBumping)位,但我正在努力弄清楚要改变什么以及如何改变。 :\

以下是代码:

import flash.geom.Point;
import flash.events.KeyboardEvent;
import flash.events.Event;

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(Event.ENTER_FRAME, loop);

var scrollX:Number = 0;
var scrollY:Number = 0;

var xSpeed:Number = 0;
var ySpeed:Number = 0;

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 leftBumpPoint:Point = new Point(-62, 40);
var rightBumpPoint:Point = new Point(62, 40);
var upBumpPoint:Point = new Point(0, -83);
var downBumpPoint:Point = new Point(0, 80);

var animationState:String = "idle";

class Player extends player {
    var speedConstant:int = 4;
    var friction:Number = .75;
    var maxSpeedConstant:Number = 18;
    var gravityConstant:Number = 16;
    var jumpConstant:Number = -180;
}

function loop(e:Event):void{

    if(back.collisions.hitTestPoint(player.x + leftBumpPoint.x, player.y +     leftBumpPoint.y, true)){
        trace("my butt!!");
    leftBumping = true;
    } else {
        leftBumping = false;
    }

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

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

    if(back.collisions.hitTestPoint(player.x + downBumpPoint.x, player.y +     downBumpPoint.y, true)){
        trace("my feet!");
        downBumping = true;
    } else {
        downBumping = false;
    }

    //speed mechanics
    if(leftPressed){
        xSpeed -= speedConstant;
        player.scaleX = -1;
    } else if(rightPressed){
        xSpeed += speedConstant;
        player.scaleX = 1;
    }   

    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 *= -.19;
        }
        if(upPressed){ //and if we're pressing UP
            ySpeed = jumpConstant; //set y speed to jump constant
        }
    } else {//if we are not touching the floor
        ySpeed += gravityConstant; //accelerate downwards       
    }   

    if(xSpeed > maxSpeedConstant){
        xSpeed = maxSpeedConstant;
    } else if(xSpeed < (maxSpeedConstant * -1)){
        xSpeed = (maxSpeedConstant * -1);
    }

    xSpeed *= friction;
    ySpeed *= friction;

    if(Math.abs(xSpeed) < 0.5){
        xSpeed = 0;
    }

    scrollX -= xSpeed;
    scrollY -= ySpeed;

    back.x = scrollX;
    back.y = scrollY;

    if( ( leftPressed || rightPressed || xSpeed > speedConstant || xSpeed <     speedConstant *-1) && downBumping){
        animationState = "walk";
    }else if(downBumping){
        animationState = "idle";
    }

    if(player.currentLabel !=animationState){
        player.gotoAndStop(animationState)
    }
}


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;
    }
}

任何帮助都会非常感激。我想学习,但是当我甚至不知道到底要找什么时很难...非常感谢!

1 个答案:

答案 0 :(得分:0)

我打算给你写一些你需要改变的解决方案。但这不是最好的努力。我只是不想改变你的整个逻辑,但你可以在你的逻辑中使用它。

在跳跃后不要使用基于时间的计算来固定y位置, 让这个计算是即时的

如果您的玩家的y位置低于零点,则将其y位置设置为零并停止加速

另外,重要的一点是,不要先使用你的hittest函数,在加速函数之后使用它们,在每个循环中使用它们,这样可以做得更好。

此外,提高电影的帧速率和降低加速度值是让事情变得更好的好方法。