2017-11-30 20 views
0

私は船を、直面している角度に向かって設定速度で移動する必要があるゲームを作成しています。私はこのコードを使って、ゲームのどこかで特異船を移動させましたが、配列の中に複雑なものがあると仮定します。AS3 - 角度に関連して配列オブジェクトを移動する

ご協力いただければ幸いです。

var ship1 = this.addChild(new Ship()); 
var ship2 = this.addChild(new Ship()); 
var ship3 = this.addChild(new Ship()); 
var ship4 = this.addChild(new Ship()); 

var shipSpeed1 = 10; 

var shipArray: Array = []; 

shipArray.push(ship1, ship2, ship3, ship4); 

for (var i: int = 0; i < shipArray.length; i++) { 
var randomX: Number = Math.random() * stage.stageHeight; 
var randomY: Number = Math.random() * stage.stageHeight; 

shipArray[i].x = randomX; 
shipArray[i].y = randomY; 

shipArray[i].rotation = 90; 

shipArray[i].x += Math.sin(shipArray[i].rotation * (Math.PI/180)) * shipSpeed1; 
shipArray[i].y -= Math.cos(shipArray[i].rotation * (Math.PI/180)) * shipSpeed1; 

} 

私もこれを同じ機能の中に組み込んでいますが、これはどちらでも動作しません。もう一度、私が最初にこの作業

if (shipArray[i].x < 0) { //This allows the boat to leave the scene and 
enter on the other side. 
    shipArray[i].x = 750; 
} 
if (shipArray[i].x > 750) { 
    shipArray[i].x = 0; 
} 
if (shipArray[i].y < 0) { 
    shipArray[i].y = 600; 
} 
if (shipArray[i].y > 600) { 
    shipArray[i].y = 0; 
} 
+0

あなたは単数形の船を移動するために使用されるコードを表示します。今のコードでは、最初の配置を超えて何も移動していません。 – BadFeelingAboutThis

+0

@BadFeelingAboutThis var ship = evt.currentTarget;\t ship.x + = Math.sin(ship.rotation *(Math.PI/180))* randomSpeed(4,15); //私の乱数を使って船をコントロールする \t ship.y - = Math.cos(ship.rotation *(Math.PI/180))* randomSpeed(4,15);それはまったく同じです。 – AndyGUY

答えて

0

を持っていた、あなたは産卵/初期化相と更新相にあなたのコードを分離する必要があります。

次のような何か:

var shipArray: Array = []; 

//spawn and set initial values (first phase) 
//spawn 4 new ships 
var i:int; 
for(i=0;i<4;i++){ 
    //instantiate the new ship 
    var ship:Ship = new Ship(); 
    //add it to the array 
    shipArray.push(ship); 

    //set it's initial x/y value 
    ship.x = Math.random() * (stage.stageWidth - ship.width); 
    ship.y = Math.random() * (stage.stageHeight - ship.height); 

    //set it's initial rotation 
    ship.rotation = Math.round(Math.random() * 360); 

    //set the ships speed (dynamic property) 
    ship.speed = 10; 

    //add the new ship to the display 
    addChild(ship); 
} 

//NEXT, add an enter frame listener that runs an update function every frame tick of the application 
this.addEventListener(Event.ENTER_FRAME, gameUpdate); 

function gameUpdate(e:Event):void { 
    //loop through each ship in the array 
    for (var i: int = 0; i < shipArray.length; i++) { 
     //move it the direction it's rotated, the amount of it's speed property 
     shipArray[i].x += Math.sin(shipArray[i].rotation * (Math.PI/180)) * shipArray[i].speed; 
     shipArray[i].y -= Math.cos(shipArray[i].rotation * (Math.PI/180)) * shipArray[i].speed; 
    } 
} 
+0

新しい問題は何ですか?船は画面を離れますか? – BadFeelingAboutThis

関連する問題