2017-07-18 103 views
1

我正在製作一個平臺遊戲,主要角色左右移動並跳躍,但是我的角色跳躍並沒有回到地面,而是停留在舞臺的頂部。我的角色電影剪輯符號被稱爲「火影忍者」,我的地面符號被稱爲「地面」。跳躍但不返回地面平臺遊戲的角色AS3

這裏是我的代碼:

import flash.events.KeyboardEvent; 
import flash.ui.Keyboard; 
import flash.display.MovieClip; 
import flash.events.Event; 
import flash.display.Stage; 

naruto.gotoAndStop("stance"); 
var rightPressed:Boolean = new Boolean(false); 
var leftPressed:Boolean = new Boolean(false); 
var upPressed:Boolean = new Boolean(false); 
var downPressed:Boolean = new Boolean(false); 
var narutoSpeed:Number = 10; 
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler); 
stage.addEventListener(KeyboardEvent.KEY_UP,keyUpHandler); 
stage.addEventListener(Event.ENTER_FRAME,gameLoop); 


function keyDownHandler(keyEvent:KeyboardEvent):void 
{ 
    if (keyEvent.keyCode == Keyboard.RIGHT) 
    { 
    rightPressed = true;  
    } 
    else if(keyEvent.keyCode == Keyboard.LEFT) 
    { 
    leftPressed = true; 
    } 
    else if(keyEvent.keyCode == Keyboard.UP) 
    { 
    upPressed = true; 
    }else if(keyEvent.keyCode == Keyboard.DOWN) 
    { 
    downPressed = true; 
    } 

} 
function keyUpHandler(keyEvent:KeyboardEvent):void 
{ 
    if (keyEvent.keyCode == Keyboard.RIGHT) 
    { 
    rightPressed = false; 
     naruto.gotoAndStop("standright") 
    } 
    else if(keyEvent.keyCode == Keyboard.LEFT) 
    { 
    leftPressed = false; 
    naruto.gotoAndStop("standleft") 
    } 
    else if(keyEvent.keyCode == Keyboard.UP) 
    { 
    upPressed = false; 
    naruto.gotoAndStop("stance")  
    }else if(keyEvent.keyCode == Keyboard.DOWN) 
    { 
    downPressed = false; 
    naruto.gotoAndStop("stance")  
    } 

} 

function gameLoop(loopEvent: Event): void { 
    //If the right key is pressed, and the left key is NOT pressed 
    if (rightPressed && !leftPressed) { 
     naruto.x += narutoSpeed; 
     naruto.gotoAndStop("right"); 
    } 

    if(leftPressed && !rightPressed) { 
     naruto.x -= narutoSpeed; 
     naruto.gotoAndStop("left"); 

    } 
var jumpHeight =0; 
var defaultJumpSpeed = 20; 
var jumpSpeed = 20; 




if(upPressed && naruto.hitTestObject(ground)) 
{ 
    trace("HELLO!"); 
naruto.y -= jumpSpeed; 
jumpSpeed-= 4; 
} 


if(upPressed) 
{ 
    trace("HELLO!"); 
jumpHeight++; 
naruto.y -= jumpSpeed; 
if(jumpHeight>10) 
jumpSpeed -= 4; 
} 


if(naruto.hitTestObject(ground)) 
{ 
    trace("HELLO!"); 
jumpHeight =0; 
jumpSpeed = defaultJumpSpeed; 
} 
    } 

這裏是鏈接對我的工作:https://www.mediafire.com/?8d5opy49fuqmup5

現在的問題是:

enter image description here

+0

你能對問題稍微具體一點嗎?你的角色是否會停止墜落(即使它看起來仍然像地面一樣)?如果是這樣,這可能與你角色的錨點/註冊點有關。 – BadFeelingAboutThis

+0

我的角色只是漂浮在屏幕的頂部。 – Hamza

+0

那麼,它會不斷下降? – BadFeelingAboutThis

回答

0

的主要問題,是你的目前的代碼,玩家只能有3個位置:

  1. 無論地面位置是
  2. 16(向上被按壓和字符不接觸地面
  3. 12(向上被按壓並且所述字在接觸地面)

請參閱旁邊的原代碼來解釋發生了什麼代碼註釋:

//you have this var inside the game loop, so every loop it resets to 20 
var jumpSpeed = 20; 

//so here, if up is pressed and the character is touching the ground 
//you initiate a jump 
if(upPressed && naruto.hitTestObject(ground)) 
{ 
    trace("HELLO!"); 
    naruto.y -= jumpSpeed; 
    jumpSpeed-= 4; //since you reset jumpSpeed to 20 every loop, this value will always just be 16 
} 


if(upPressed) 
{ 
    trace("HELLO!"); 
    jumpHeight++; 

    //if naruto is touching the ground 
    //you are subtracting jumpSpeed above and right here again. 
    //so now the y position is 12 if touching the ground (or 16 if not touching the ground) 
    //since you reset jumpSpeed to 20 every loop, it always be one of those two values 
    naruto.y -= jumpSpeed; 


    if(jumpHeight>10) 
    jumpSpeed -= 4; //this serves no purpose, as your not using the value again and it gets reset to 20 next time the game loop runs 
} 

//this hit test will succeed in addition to the first one above when your jump starts. 
if(naruto.hitTestObject(ground)) 
{ 
    trace("HELLO!"); 
    jumpHeight =0; 
    jumpSpeed = defaultJumpSpeed; 
} 

爲了彌補您的跳躍,你需要沿着這些線路做一些事情:

//initialize these vars outside of the game-loop for efficient and proper scoping (so their values don't reset every frame) 
var isJumping:Boolean = false; //a flag to know if a jump is in progress 
var jumpSpeed:Number = 0; //the current velocity of the jump 
var defaultJumpSpeed:Number = 20; //the initial force (speed) of a jump 
var jumpGravity:Number = 2; //subtract this from the speed every frame to slow the jump over time and fall 
var onGround:Boolean = false; //a helper var for efficiency 

function gameLoop(loopEvent: Event): void { 
    //If the right key is pressed, and the left key is NOT pressed 
    if (rightPressed && !leftPressed) { 
     naruto.x += narutoSpeed; 
     naruto.gotoAndStop("right"); 
    } 

    if (leftPressed && !rightPressed) { 
     naruto.x -= narutoSpeed; 
     naruto.gotoAndStop("left"); 

    } 

    //only do the hit test once per frame for efficiency (store the result) 
    onGround = naruto.hitTestObject(ground); 

    if (upPressed && onGround) { 
     trace("START JUMP"); 
     isJumping = true; 
     jumpSpeed = defaultJumpSpeed; //set the initial jump velocity 
    } 

    if(isJumping){ //if jumping 
     naruto.y -= jumpSpeed; //move the player based off the current jump velocity 
     jumpSpeed -= jumpGravity; //slow the jump every frame (eventually causing it to be negative making the player fall) 
    } 

    //touching the ground and the up key is not pressed 
    //it's very important to put !upPressed so this doesn't run at the time as the initial jump above 
    if (!upPressed && onGround) { 
     //stop any jump motion 
     jumpSpeed = 0; 
     isJumping = false; 

     //you probably also want to snap the player to the ground 
     naruto.y = ground.y - naruto.height + 2; //the plus 2 ensures that the ground and the player touch so the hit test succeeds 
    } 
} 
+0

請你能寫完整的代碼,因爲我真的很掙扎。謝謝。 – Hamza

+0

我不確定你希望看到的其他代碼。也許如果你對你遇到的問題有更具體的瞭解。 – BadFeelingAboutThis

+0

我的用戶根本沒有跳轉我試圖更換遊戲循環功能,但沒有任何作用。 – Hamza