2016-06-22 2 views
0

私はjavascript上で非常に新しいです、私はいくつかの小惑星を殺さなければならない船の賭博をしていました、そしてあなたがスクリーン上の "オブジェクト"のいくつかの異なるものを取るとき、私たちは弾丸の数を広げます。オキー、ポイントに行くと、私はdiffirentsオブジェクトを取るときに画面上に3つの弾丸を得ることができましたが、今は、配列の3つの弾丸のうち2つに異なる方向を与えたいと思います。私が試したとき、私は3つの弾丸に同じ方向を与えるという問題があります、なぜ私は知っていますが、少なくとも5時間この問題を解決しようとすると私は傾ける。1つの配列に異なる弾丸の方向を与える方法は?

異なるクラスのFlash Builder 4.7でのプログラミングでは、メインにある配列のコードと、弾丸クラスもヒーロークラスに渡します。ここ

メインアレイ

public function evUpdateBullet():void//here execute update of my class Bullets 
    { 
     var i:int; 
     for(i=myBullets.length-1; i >= 0; i--) 
     { 
      if(myBullets != null) //to be ? 
      { 
       if(myBullets[i].isDestroyed) //is destroyed Bullets? 
       { 
        myBullets[i] = null; 
        myBullets.splice(i, 1); //deleted elements. 
       }else 
       { 

        myBullets[i].evUpdate(); 





       } 
      } 
     } 
    } 

私は配列をプッシュし、箇条書きを作成し、myBulletsを覚えている配列の名前です。ここで

public function evShoot(posX:int, posY:int):void//here create the bullet and push in the array 
    { 
     attack1 = new Bullet; 
     attack1.Spawn(posX, posY); 
     myBullets.push(attack1); 

}

私は弾丸の位置が画面上に出現しようとしている定義ヒーローコードを示します。ここで

if (isPressing_Shoot && !isDestroyed)// Here execute the event shoot without power 
     { 
      Main.instace.evShoot(model.x, model.y); 


      isPressing_Shoot = false; 
      canShoot = false; 

     } 


     evDestroyed(); 
    } 

第ブレットクラス次にスポーン

public function Spawn(posX:int, posY:int):void 
    { 
     isDestroyed = false;//first parameter of my bullet 

     model = new MCbullet; 
     Main.layer1.addChild(model);//painting the hero in the stage 
     model.x = posX;//position in the stage wiht the hero 
     model.y = posY; 
     model.tigger.visible = false; 

    } 

iはYの動きを設定し、このアップデートで更新

public function evUpdate():void//here conect with update general 
    { 
     if (model != null)//to be? 
     { 

      model.y -= 12;//move of my bullet 
      //model.x -= 12; 





      if (model.y <= 0) 
      { 
       evDestroyed(); 
      } 
     } 

    } 

からのコードであるので、私私はx.moveを追加しようとすると、私はすべての配列のために行うので、私はどのように私が与えることができます知ってほしい同じ配列の異なった箇条書きのために移動する。

+0

JavaScriptの新機能には何がありますか? –

答えて

0

配列要素を繰り返します。これを行うにはいくつかの方法がありますが、私が最も慣れているのはforループです。それは次のようになります。

// loop through myBullets array to update 
// each x and y position dependent on unique 
// property value _xSpeed and _ySpeed. 
for (var i:int = 0; i < myBullets.length; i++) 
{ 
    myBullets[i].x += myBullets[i]._xSpeed; 
    myBullets[i].y += myBullets[i]._ySpeed; 
} 

を明らかに、あなたは動的な値に設定する配列要素の_xSpeed_ySpeedプロパティが必要になります。最初に弾丸クラスにこれらのプロパティを与え、弾丸をインスタンス化するときにそれらの値を設定する必要があります。 、

function makeBullet():void{ 
    var b:Bullet = new Bullet(); 
    b.x = hero.x; 
    b.y = hero.y; 
    b._xSpeed = hero._xSpeed; // or put here whatever makes sense for assigning the right value in your application 

そして、あなたの弾丸クラスのコンストラクタで、function前しかしclassカッコ内のプロパティを追加します:それは次のようになります

var _xSpeed:Number = 0; 
var _ySpeed:Number = 0; 

基本的に、これは各弾丸が保持できるようにされることがありますクラスの他のインスタンスから独立した独自の特別なプロパティです。

私は役立つことを願っています。

関連する問題