2016-04-27 57 views
0

在我的platformcollision函數中,當玩家在左側檢查碰撞時,一切正常。我的球員沒有通過,他可以以相反的方式移動。但是,當我將戰爭法師放在左邊並檢查是否與右側的平臺發生碰撞時,玩家立即傳送到平臺旁邊。我不明白爲什麼我所做的只是翻轉算術符號,如果從另一個球員的聲明檢查其右側。使用actionscript 3 flash的水平碰撞問題

import flash.events.Event; 
import flash.display.MovieClip; 

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 = .5;//Friction 
var floor:int = 800;//Bottom of the stage 
//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 
     { 
     hsp -= 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 
     { 
     hsp += 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.DOWN)) 
     { 
     warMage.gotoAndStop("CrouchWarmage"); 

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

      warMage.y -= 60;//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):void 
{ 
    warMage.y += grav;//Apply gravity to the player 
    hsp *= fric;//Friction is applied to hsp to prevent infinite acceleration 

    warMage.x += hsp;//The plater moves horizontal position 
    if(warMage.x - warMage.width/2 < 0)//If the player goes past the left side 
     { 
      warMage.x = warMage.width/2; 
     } 
    if(warMage.x + warMage.width/2 > 1400)//If the player goes past right 
     { 
      warMage.x = 1400 - warMage.width/2;//Player cant go past 
     } 
    if(warMage.y < floor)//If we are above the floor 
     { 
     // warMage.y += grav;//Apply gravity to the player 
      grav++;//Accelerate gravity in the process 
      warMage.gotoAndStop("JumpWarmage");//Play the jump animation 

     } else //if(warMage.y - warMage.height/2 > floor) 
      { 

      jumped = false;//If we are on the floor then we're not jumping 
      grav = 0;//Gravity can no longer be applied 
      warMage.y = floor;//Player sits on top of the floor 
      } 
} 

function platformCollision(e:Event):void 
{ 
//If the player.x is less then the left side and the player can go into the box and if the warMage.y is equal to the height 
if(warMage.x - warMage.width/2 < platform.x + platform.width/2 + 2 && warMage.y - platform.y == platform.height/2) 
    { 
     warMage.x = platform.x + platform.width/2 + warMage.width/2; 
     //Player.x is equal to the left side of the platform 

    } 
if(warMage.x + warMage.width/2 > platform.x - platform.width/2 - 2 && warMage.y - platform.y == platform.height/2) 
    { 
     rDown = false; 
     warMage.x = platform.x - platform.width/2 - warMage.width/2; 
     //Player.x is equal to the left side of the platform 

    } 
} 

回答

0

你不需要的if/else,如果在這裏而不是2名IFS?

如果該位是正確的:

if(warMage.x - warMage.width/2 < platform.x + platform.width/2 + 2 && warMage.y - platform.y == platform.height/2) 

並設置

warMage.x = platform.x + platform.width/2 + warMage.width/2; 

那麼如果第二將是真實的時候了。 現在warMage.x是platform.x + platform.width/2 + warMage.width/2; 所以,檢查第二,如果:

if(warMage.x + warMage.width/2 > platform.x - platform.width/2 - 2 && warMage.y - platform.y == platform.height/2) 

所以warMage.x(也就是現在的platform.x + platform.width/2 + warMage.width/2)+ warMage.width/2肯定比platform.x - platform.width/2 - 2更大,這就是爲什麼你設置

warMage.x = platform.x - platform.width/2 - warMage.width/2; 

之後。

看起來像platformCollision需要重新思考。

+0

感謝您指出。這個2D平臺代碼非常困難。 – user199845