2011-04-26 26 views
0

我在Actionscript中創建了一個簡單的可控船隻,並且它已經得到了前進,左轉和右轉,但無法讓它轉向。我希望船沿它指向的方向前進。在ActionScript中的簡單船隻

function moveBoat(event:Event):void 
{ 
    if(rightKeyIsDown) 
    { 
     player_mc.x += speed; 
     player_mc.rotationZ += speed; 
    } 
    if(leftKeyIsDown) 
    { 
     player_mc.x -= speed; 
     player_mc.rotationZ -= speed; 

    } 
    if(upKeyIsDown) 
    { 
     player_mc.y -= speed; 
    } 
} 

在此先感謝任何能告訴我我做錯了什麼的人。

編輯:

function moveBoat(event:Event):void 
{ 
    if(rightKeyIsDown) 
    { 
     player_mc.rotationZ += turnFactor; 
    } 
    if(leftKeyIsDown) 
    { 
     player_mc.rotationZ -= turnFactor; 
    } 
    if(upKeyIsDown) 
    { 
     player_mc.x += speed * Math.cos(player_mc.rotationZ * Math.PI/180); 
     player_mc.y -= speed * Math.sin(player_mc.rotationZ * Math.PI/180); 
    } 
} 

編輯

function moveBoat(event:Event):void 
{ 
    if(rightKeyIsDown) 
    { 
     player_mc.rotation += turnFactor; 
    } 
    if(leftKeyIsDown) 
    { 
     player_mc.rotation -= turnFactor; 
    } 
    if(upKeyIsDown) 
    { 
     // convert our rotation to radians first 
     var rads:Number = player_mc.rotation * (Math.PI/180.0); 
     player_mc.x += speed * Math.cos(rads); 
     player_mc.y += speed * Math.sin(rads); 
    } 
} 
+0

而不是'player_mc.y - = ...',設置'player_mc.y + = ...',它會工作。就像我在我的回答中提到的那樣... – divillysausages 2011-04-26 16:42:05

回答

1

試着這麼做:

function moveBoat(event:Event):void 
{ 
    if(rightKeyIsDown) 
    { 
     player_mc.rotationZ -= turnFactor; 
    } 
    if(leftKeyIsDown) 
    { 
     player_mc.rotationZ += turnFactor; 
    } 
    if(upKeyIsDown) 
    { 
     player_mc.x += speed * Math.cos(player_mc.rotationZ * Math.PI/180); 
     player_mc.y -= speed * Math.sin(player_mc.rotationZ * Math.PI/180); 
    } 
} 

我現在不能測試代碼,但應該是足以解釋這個想法。我做的不同之處在於使用左右鍵來控制船舶(通過改變其角度),並在點擊向上鍵時將其沿着它所指向的方向移動。

移動船隻時,您不能簡單地將它沿X軸和Y軸移動一定量,您還需要考慮船的方向。爲此,請使用正弦和餘弦函數。

+0

謝謝。越來越近,但現在當按下時船隻只能左轉或右轉,這取決於它旋轉了多遠。 – RapsFan1981 2011-04-26 15:44:52

+0

rotationZ屬性使用度數,數學函數使用弧度。調用數學函數時,一定要將度數轉換爲弧度(player_mc.rotationZ * Math.PI/180)。如果這不起作用,請發佈您使用的確切代碼。 – TokPhobia 2011-04-26 16:00:00

+0

做了一些編輯,但仍然向上鍵在x軸上移動船 – RapsFan1981 2011-04-26 16:12:21

-1

TokPhobia幾乎擁有了正確的

private function _moveBoat(event:Event):void 
{ 
    if(rightKeyIsDown) 
     this.m_player.rotation += turnFactor; 

    if (leftKeyIsDown) 
     this.m_player.rotation -= turnFactor; 

    if(upKeyIsDown) 
    { 
     // convert our rotation to radians first 
     var rads:Number = this.m_player.rotation * (Math.PI/180.0); 
     this.m_player.x += speed * Math.cos(rads); 
     this.m_player.y += speed * Math.sin(rads); 
    } 
} 

順便說一句,除非你在3D這樣做,rotation屬性將工作得很好,你不需要使用rotationZ

+0

同樣的問題在這裏可以看到: http://hedonsoft.com/test.html – RapsFan1981 2011-04-26 16:53:04

+0

它實際上工作,你只需要改變你的船舶圖像的旋轉Flash需要旋轉0作爲面向右側,而你的船被設計爲朝上(閃光旋轉-90)。只需編輯你的船舶圖像指向右側,你會很好 – divillysausages 2011-04-26 17:06:19

+0

謝謝divilly得到它:) – RapsFan1981 2011-04-26 17:12:42

0

使用一些簡單的三角函數來計算X軸和Y軸上物體的移動量。創建一個直角三角形,其中船是A點,它的速度是h,從船的方位角度答:

right triangle

現在使用的正弦和餘弦找出邊的長度a和b。這是在X和Y軸上移動多遠。