游戏角色出现在舞台之后

时间:2014-07-16 19:46:56

标签: actionscript-3 flash game-physics flash-cs6

我正在使用ActionScript 3在Adobe Flash中制作一个基本的Flash平台游戏。我似乎无法让我的角色在我制作的舞台上开始。我能够将我的角色移动到舞台上,但是在增加重力和跳跃力学后,它不能再移动到那么高,所以我现在被卡住了。我正在学习本教程,但我创建了自己的背景和播放器。任何帮助是极大的赞赏。 这是代码:

   import flash.events.KeyboardEvent;

    //Variables
    var leftPressed:Boolean = false;
    var rightPressed:Boolean = false;
  var upPressed:Boolean = false;
var downPressed:Boolean = false;

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

//variables to keep track of background x  and y locations
var scrollX:Number = 0;
var scrollY:Number = 0;
//Constant Variables
var speedConstant:int = 5;
var friction:Number = 0.95;
var jumpConstant:Number = -25;
//Gravity constant
var gravityConstant:Number = 1.5;

//Variables for collission detection
var leftBumping:Boolean = false;
var rightBumping:Boolean = false;
var upBumping:Boolean = false;
var downBumping:Boolean = false;

var leftBumpPoint:Point = new Point(-30, -55);
var rightBumpPoint:Point = new Point(30, -55);
var upBumpPoint:Point = new Point(0, -120);
var downBumpPoint:Point = new Point(0, 0);

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

//Functions for listners
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;
}
}

//Main Game Loop

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(leftPressed){
    xSpeed -= speedConstant;

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

}

/*    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 we are touching the floor
    if(ySpeed > 0){
      ySpeed *= -0.5;
 }
 if(upPressed){ //and if the up arrow is pressed
      ySpeed = jumpConstant; //set the y speed to the jump constant
 }
} 
else { //if we are not touching the floor
     ySpeed += gravityConstant; //accelerate downwards
}

xSpeed *= friction;
ySpeed *= friction;

scrollX -= xSpeed;
scrollY -= ySpeed;

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

0 个答案:

没有答案