2016-04-24 106 views
0

我已經成功地讓我的角色跳躍,但在平臺的頂部只有完美的碰撞,但是當它碰到浮動平臺的底部時,它會穿過平臺,玩家不會落在這些平臺的邊緣。相反,他運行在實例的y軸上。另外,當遊戲最初開始時,玩家在特定的y座標上在半空中跑步。無論如何,我可以扭轉這些問題嗎?As3跳躍碰撞問題

import flash.events.KeyboardEvent; 
import flash.events.Event; 
import flash.text.engine.EastAsianJustifier; 
import flash.sensors.Accelerometer; 

var Key:KeyObject = new KeyObject(stage);//Help the stage checks for keypressed objects   
//Initialized variable integers 
var hsp:Number = 0;// horizontal speed 
var vsp:Number = 0;// vertical speed 
var grav:Number = 2;//Gravity 
var fric:Number = 4//Friction 

//Arrays list for platforms 


//All Booleans 
var lDown:Boolean = false; 
var rDown:Boolean = false; 
var jumped:Boolean = false; 
var attacking:Boolean = false; 

warMage.gotoAndStop("idleWarmage");//Initially starts at idle state 
stage.addEventListener(Event.ENTER_FRAME, keyPressed);//Listens for buttons pressed 
stage.addEventListener(Event.ENTER_FRAME, gameloop);// The physics applied to character 
stage.addEventListener(Event.ENTER_FRAME, platformCollision); 

function keyPressed(e:Event):void 
{ 
if(Key.isDown(Key.LEFT))//If we pressed the left arrow button 
    { 
     lDown = true;//Condition to check if player is in running state 
     if(lDown = true)//If we are running left 
      { 
      warMage.x -= 15;//Move left 
      warMage.gotoAndStop("RunWarmage");//Play the running animation 
      warMage.scaleX = -1;//Flip the image scale 
      } 
    }else if(Key.isDown(Key.RIGHT))//If we pressed the right arrow button 
    { 
     rDown = true;//Condition to check if player is in running state 
     if(rDown = true)//If we are moving right 
      { 
      warMage.x += 15;//Move the position right 
      warMage.gotoAndStop("RunWarmage");//Play the animation 
      warMage.scaleX = 1//Face right 
      } 
    }else if(Key.isDown(Key.SPACE))//If we press the spacebar 
     { 
      warMage.gotoAndStop("AttackWarmage");//Play teh attack animation 
      warMage.x += 5; //Lunge right 
      if(warMage.scaleX == -1)//If we are initially facing left 
       { 
        warMage.x -= 10;//Lunge left 
       } 

     }else if(Key.isDown(Key.UP) || jumped == true)//If we press the up arrow or we've jumped 
      { 

       vsp = -25;;//vertical speed goes up to 20 
       jumped = true;//We know that player has jumped 
       warMage.gotoAndStop("JumpWarmage");//Play teh jump animation 

      }else if(jumped == false)//If we're not jumping 
      { 
       warMage.gotoAndStop("idleWarmage");//Return to idle position 
      } 
    } 

function gameloop(e:Event)//This checks the laws per frame 
{ 

    if(warMage.x < 0)//Sets room boundaries for left 
    { 
     warMage.x = 0; 
    } 
if(warMage.x > 1000)//Sets room boundaries for right 
    { 
     warMage.x = 1000; 
    } 

} 

function platformCollision(e:Event):void//Collision for the platforms 
{ 
      vsp += grav; 
      if(!multiplePlatforms.hitTestPoint(warMage.x,warMage.y , true) && jumped == true) 
       { 

        grav++; 
        warMage.y += vsp; 
        warMage.gotoAndStop("JumpWarmage");//Play teh jump animation 
       } 
      for(var i:Number = 0;i < 34; i++) 
      { 
      if(multiplePlatforms.hitTestPoint(warMage.x,warMage.y , true))//Check if the player hits any platforms in the array 
       { 
        warMage.y --;//Place the character a pixel above the platforms 
        grav = 0;//Gravity isnt applied 
        vsp = 0; 
        jumped = false; 

       } 
      }  
      } 

回答

0

當你跳起來打平臺,如果塊將發生如下:

if(multiplePlatforms.hitTestPoint(warMage.x,warMage.y , true))//Check if the player hits any platforms in the array 
       { 
        warMage.y --;//Place the character a pixel above the platforms 
        grav = 0;//Gravity isnt applied 
        vsp = 0; 
        jumped = false; 

       } 
      } 

這將移動平臺上面的warmage並設置重力(和VSP)爲0,所以他永不脫落。我沒有看到你的代碼中的任何地方,你將它設置爲0(除非你跳轉),將重力設置爲0。我認爲你應該做一些事情。

1.)你應該不使用hitTestPoint,而是建立自己的命中檢測。下面是一個例子去:https://gamedev.stackexchange.com/questions/586/what-is-the-fastest-way-to-work-out-2d-bounding-box-intersection

2)判斷玩家是否在平臺或他相撞的平臺之上,並應用適當的改變VSP(0出來,如果玩家是高於還是令玩家如果他低於下降)。 3.如果玩家沒有被任何平臺阻擋,請確保將加速度(grav)設置回默認值,以便玩家再次開始下降。

4.)(可選)當您不需要時,您正在使用兩個Enter Frame事件。你可能應該在每一幀從你的gameloop裏面調用平臺碰撞函數。

+0

對不起,但我不能真正理解的例子,因爲我沒有在遊戲開發尚未。 – user199845

+0

如果我使用我自己的碰撞代碼,我怎麼能夠將它應用到多個平臺? – user199845

+0

您可以將碰撞檢測放入函數中,並將每個平臺+播放器傳遞給該函數。如果你不想這樣做,並希望繼續使用hitTestPoint,你仍然可以解決你的問題。您碰到的問題就是,當您觸摸平臺時,即使應該發生不同的事情,您是否運行相同的代碼,以確定是否觸摸平臺的頂部或底部。 – Snukus