我正在尝试按照教程帮助我学习如何创建平台游戏。但是,每当我测试我的代码时,我的播放器都会经历背景(命名为背景而不是背景)。我试图添加更多的点来阻止它这样做,但它只是继续。任何有hitTestPoint或flash经验的人都可以帮助我!
查看我的代码的Bumping部分
http://as3gametuts.com/2012/01/08/platformer-3/ ---使用此功能。
背景只是玩家所站的平台。 hitTestPoint代码应该阻止玩家通过平台掉落或穿过墙壁。有时候,当玩家进展很慢时,hitTestPoint会起作用。
import flash.events.KeyboardEvent;
import flash.geom.Point;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(Event.ENTER_FRAME, loop);
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
var upPressed:Boolean = false;
var downPressed:Boolean = false;
var leftReleased:Boolean = false;
var rightReleased:Boolean = false;
var upReleased:Boolean = false;
var downReleased:Boolean = false;
var xSpeed:int = 10;
var ySpeed:int = 10;
var scrollX:int = 0;
var scrollY:int = 0;
var speedConstant:int = 5;
var friction:Number = 0.95;
var leftBumping:Boolean = false;
var rightBumping:Boolean = false;
var upBumping:Boolean = false;
var downBumping:Boolean = false;
var upleftBumping:Boolean = false;
var uprightBumping:Boolean = false;
var downrightBumping:Boolean = false;
var downleftBumping:Boolean = false;
var leftBumpPoint:Point = new Point(-30, -55)
var upleftBumpPoint:Point = new Point(-30, -87)
var rightBumpPoint:Point = new Point(30, -55)
var uprightBumpPoint:Point = new Point(30, -87)
var upBumpPoint:Point = new Point(0, -120)
var downBumpPoint:Point = new Point(0, 0)
var downrightBumpPoint:Point = new Point(30, -27)
var downleftBumpPoint:Point = new Point(-30, -27)
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 keyUpHandler(e:KeyboardEvent):void{
if(e.keyCode == Keyboard.LEFT){
leftReleased = true;
} else if(e.keyCode == Keyboard.RIGHT){
rightReleased = true;
} else if(e.keyCode == Keyboard.UP){
upReleased = true;
} else if(e.keyCode == Keyboard.DOWN){
downReleased = true;
}
}*/
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 + upleftBumpPoint.x, player.y + upleftBumpPoint.y, true)){
trace("upleftBumping")
upleftBumping = true;
} else {
upleftBumping = false;
}
if(back.hitTestPoint(player.x + uprightBumpPoint.x, player.y + uprightBumpPoint.y, true)){
trace("uprightBumping")
uprightBumping = true;
} else {
uprightBumping = false;
}
if(back.hitTestPoint(player.x + downrightBumpPoint.x, player.y + downrightBumpPoint.y, true)){
trace("downrightBumping")
downrightBumping = true;
} else {
downrightBumping = false;
}
if(back.hitTestPoint(player.x + downleftBumpPoint.x, player.y + downleftBumpPoint.y, true)){
trace("downleftBumping")
downleftBumping = true;
} else {
downleftBumping = false;
}
if(leftPressed){
xSpeed -= speedConstant ;
} else if(rightPressed){
xSpeed += speedConstant;
} else if(upPressed){
ySpeed += speedConstant;
} else if(downPressed){
ySpeed -= 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.5;
}
}
xSpeed *= friction;
ySpeed *= friction;
scrollX -= xSpeed;
scrollY += ySpeed;
back.x = scrollX;
back.y = scrollY;
}
答案 0 :(得分:0)
我没有分析整个代码,但它看起来像是典型的子弹场景。
如果你只针对宽度小于玩家速度的障碍物测试一个点,你偶尔会错过障碍物,因为在一帧中测试点在它的一侧而在另一帧中在另一帧中 - 点在两者上都没有击中它帧。
解决问题的最简单方法就是确保您的障碍物的宽度大于玩家的速度。
另一种方法是测试测试点位置之间的中间点。您可以通过Point.interplate()
方法轻松完成此操作,此外,您还可以根据玩家的速度和obsticles的大小调整中间测试的数量。
但是自从你开始使用Box2D之后,你可以在对象物理属性中设置一个子弹标记。