遇到左右撞击碰撞问题,它会检测到它,但是如果你按住右边,你可以穿过一堵墙,没有任何真正的挣扎,无论是粗细的还是薄的。
我知道我的碰撞检测并不是很好,但这是我的第一个项目,我尝试了一些简单的事情。
xFart是x方向的速度,fartKonstant是你可以为更高或更低的速度改变的常数,friksjonsKonstant是游戏中的摩擦力。
非常感谢帮助,这不仅仅是一种绷带:)
package AS
{
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.geom.Point;
public class Bane extends MovieClip
{
//Bevegelse variabler
private var goUp:Boolean;
private var goDown:Boolean;
private var goLeft:Boolean;
private var goRight:Boolean;
//Fart variabler
private var xScrollFart:Number = 10;
private var yScrollFart:Number = 10;
private var xFart:Number = 0;
private var yFart:Number = 0;
//Konstanter som kan endres
private var minFart:Number = 0.5;
private var maksFart:Number = 18;
private var gravitasjonKonstant:Number = 1.8;
private var hoppeKonstant:Number = 35;
private var fartKonstant:uint = 4;
private var friksjonKonstant:Number = 0.95;
//Kollisjon
private var upBumping:Boolean = false;
private var downBumping:Boolean = false;
private var leftBumping:Boolean = false;
private var rightBumping:Boolean = false
//Kollisjonspunkter
private var leftBumpPoint:Point = new Point(300 - 20, 300 - 40);
private var rightBumpPoint:Point = new Point(300 + 20, 300 - 40);
private var upBumpPoint:Point = new Point(300, 300 - 80);
private var downBumpPoint:Point = new Point(300, 300);
public function Bane()
{
goUp = false;
goDown = false;
goLeft = false;
goRight = false;
Constants.stageRef.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
Constants.stageRef.addEventListener(KeyboardEvent.KEY_UP, keyUp);
Constants.stageRef.addEventListener(Event.ENTER_FRAME, frameListener);
}
private function keyDown(evt:KeyboardEvent)
{
if(evt.keyCode==65) {goLeft=true}
if(evt.keyCode==68) {goRight=true}
if(evt.keyCode==87) {goUp=true}
if(evt.keyCode==83) {goDown=true}
if(evt.keyCode==37) {goLeft=true}
if(evt.keyCode==38) {goUp=true}
if(evt.keyCode==39) {goRight=true}
if(evt.keyCode==40) {goDown=true}
}
private function keyUp(evt:KeyboardEvent)
{
if(evt.keyCode==65) {goLeft=false}
if(evt.keyCode==68) {goRight=false}
if(evt.keyCode==87) {goUp=false}
if(evt.keyCode==83) {goDown=false}
if(evt.keyCode==37) {goLeft=false}
if(evt.keyCode==38) {goUp=false}
if(evt.keyCode==39) {goRight=false}
if(evt.keyCode==40) {goDown=false}
}
//Kjører koden 29 ganger i sekundet, hver frame
private function frameListener(e:Event)
{
//Kollisjon
if(this.hitTestPoint(upBumpPoint.x, upBumpPoint.y, true))
{
trace("upBumping");
upBumping = true;
}
else
{
upBumping = false;
}
if(this.hitTestPoint(downBumpPoint.x, downBumpPoint.y, true))
{
trace("downBump");
downBumping = true;
}
else
{
downBumping = false;
}
if(this.hitTestPoint(leftBumpPoint.x, leftBumpPoint.y, true))
{
trace("leftBump");
leftBumping = true;
}
else
{
leftBumping = false;
}
if(this.hitTestPoint(rightBumpPoint.x, rightBumpPoint.y, true))
{
trace("rightBump");
rightBumping = true;
}
else
{
rightBumping = false;
}
if(leftBumping)
{
if(xFart < 0)
{
xFart *= -0.5;
}
}
if(rightBumping)
{
if(xFart > 0)
{
xFart *= -0.5;
}
}
if(upBumping)
{
if(yFart < 0)
{
yFart *= -0.5;
}
}
if(downBumping)
{
if(yFart > 0)
{
yFart *= 0;
}
goDown = false;
}
if(goLeft == true)
{
xFart -= fartKonstant
}
if(goRight == true)
{
xFart += fartKonstant
}
if(goDown == true)
{
yFart += fartKonstant
}
if(Math.abs(xFart) <= minFart)
{
xFart = 0
}
if(xFart >= maksFart)
{
xFart = maksFart;
}
if(xFart <= (maksFart * -1))
{
xFart = maksFart * -1;
}
xFart *= friksjonKonstant;
yFart *= friksjonKonstant;
xScrollFart -= xFart;
yScrollFart -= yFart;
this.x = xScrollFart;
this.y = yScrollFart;
if(downBumping == false)
{
goUp = false;
yFart += gravitasjonKonstant;
}
else if(downBumping == true)
{
if(goUp)
{
yFart = hoppeKonstant * -1;
}
}
}
}
}
答案 0 :(得分:0)
你没有解决冲突,这就是问题所在。除了检测碰撞事实外,您还需要对碰撞物体做一些事情,使它们不会发生碰撞。
模拟的每个刻度线的当前逻辑如下:
问题是,在整个计算完成后,对象可能仍然与边界冲突。您错过了这个步骤,您可以通过在上一步骤之后沿着它碰撞的路径向后移动对象来解决碰撞。
因此,要解决此问题,您需要在检测到对象时首先将对象移出碰撞。这应该是计算的第二步。
以下链接可能有用: