2013-07-18 31 views
0

我想創建一個「蠕蟲」風格的回合炮兵遊戲是as3。我可以在角色的x/y位置添加我的球,並且在發出咔嗒聲時讓球向着鼠標位置的方向射擊,但是我現在難以向球施加重力。如果任何人都可以幫助我,那麼我需要做的就是讓球在被擊出後以弧線形式落下,這很棒。as3將重力應用於導彈

我的用於添加的球和具有它觸發在選定方向代碼是: 遊戲機主機:

功能redFollow(E:MouseEvent)方法:無效 {

 var a1 = mouseY - redPower.y; 
     var b1 = mouseX - redPower.x; 
     var radians1 = Math.atan2(a1,b1); 
     var degrees1 = radians1/(Math.PI/180); 
     redPower.rotation = degrees1; 


    } 


    public function redFIRE(Event:MouseEvent) 
    { 

     removeChild(redPower); 
     turnchangeover(); 
     addChild(RPBall); 
     trace("FIRE!"); 
     RPBall.x = red.x; 
     RPBall.y = red.y; 
     RPBall.rotation = redPower.rotation + 90; 

    } 

,然後球的類代碼爲:

包{

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

public class redBall extends MovieClip { 


    public function redBall() { 
     this.addEventListener(Event.ENTER_FRAME, moveShot); 
    } 
    function moveShot(event:Event){ 
     var thisMissile:redBall = redBall(event.target); 
     var _missileSpeed:int = 7; 
     var gravity:int = 0.8; 


     //get the x,y components of the angle 
     var missileAngleRadians = ((-thisMissile.rotation - 180) * Math.PI /180); 
     //trace("missle angle: " + missileAngleRadians); 

     var yMoveIncrement = Math.cos(missileAngleRadians) * _missileSpeed; 
     var xMoveIncrement = Math.sin(missileAngleRadians) * _missileSpeed; 




     thisMissile.y = thisMissile.y +yMoveIncrement; 


     thisMissile.x = thisMissile.x + xMoveIncrement; 

     trace(thisMissile.y); 

    } 



} 

}

回答

0

你需要通過X有Y見單獨存儲速度,你的球已經啓動後,你不關心angle,但只在乎速度,而速度是何等的受重力影響。你已經通過yMoveIncrement,xMoveIncrement中的X和Y分量來分割速度,現在你只需要讓它們成爲球的屬性(它將是它的速度),並且爲速度的Y分量增加一個很小的每幀增量。

public class redBall extends MovieClip { 

    public var yVelocity:Number=0; 
    public var xVelocity:Number=0; // these will be assigned when the ball is shot 
    public static var gravity:Number=0.5; // Number, not "int" 
    // also "static" so that it'll affect all balls similarly 
    public function redBall() { 
     this.addEventListener(Event.ENTER_FRAME, moveShot); 
    } 
    function moveShot(event:Event){ 
     // var thisMissile:redBall = redBall(event.target); 
     // why is^this? You can use "this" qualifier in this function 
     // we already have the x,y components of the angle 
     this.y = this.y + this.yVelocity; 
     this.x = this.x + this.xVelocity; 
     this.yVelocity = this.yVelocity + gravity; 
     // also, you care for rotation - let's add a trick 
     var angle:Number=Math.atan2(this.yVelocity,this.xVelocity); 
     // direction angle, in radians 
     this.rotation=angle*180/Math.PI; // make into degrees and rotate 
     trace(thisMissile.y); 
    } 
} 

這將是一個很好的做法,如果你將添加一個shoot功能redBall類,將接受角和初始速度,分配xVelocityyVelocity你聽者做。

+0

感謝您的幫助,但我仍然有點迷路,似乎無法執行您告訴我的內容。還有什麼建議嗎?先謝謝你! – user2594986

+0

你是否介意用你現在擁有的內容更新問題中的代碼,以便我可以查看並提供詳細的建議? – Vesper

0

爲了重新創建拋射體運動,您需要引入3個變量重力(常數),速度(在x和y上,類似於移動增量)和衰減(也稱爲摩擦)。

摩擦力應該低於1的值。以便它隨着時間的推移降低速度。一旦速度小於系統中的重力,物品應該開始落回地面。該項目也會在x軸上減速。

velocity.y *= friction; 
thisMissile.y += velocity.y + gravity.y; 

這裏有一個generic tutorial on projectile physics可能會感興趣和AS3 tutorial on Angry Bird like ground to ground projectile