2017-01-29 4 views
1

オブジェクトの可変配列を作成しようとしていますが、forループを実行しようとすると、繰り返しは1回しか実行されずに終了します。オブジェクトの配列を作成するのは1回だけです

function generate() { 
     works = []; // Clear array 
     console.log(num.value); 
     for(i=0; i < num.value; i++) { 
      console.log(i); 
      var fire = new Firework(cxt); 
      works.push(fire); 
     } 
    } 

ここで、cxtはhtmlキャンバスコンテキストであり、num.valueは範囲です。

これは、1回の繰り返しを実行してから停止します。花火オブジェクトのコンストラクタを次に示します。

function Firework(Context, DestX, DestY, Speed, Radius, Color, Scale) { 
    // Basic Stuff 
    this.stat = 0; // Status of explosion 0 - 100 
    this.speed = Speed || 5; 
    this.color = Color || "blue"; 
    this.radius = Radius || 5; 
    this.scale = Scale || 1; 

    // Movment Stuff 
    this.curX = 0; 
    this.curY = 0; 
    this.destX = DestX || 100; 
    this.destY = DestY || 100; 
    this.dX = this.speed * Math.cos(Math.atan((this.destY)/(this.destX))); 
    this.dY = this.speed * Math.sin(Math.atan((this.destY)/(this.destX))); 

    // Spark Stuff 
    this.sparkNum = 100; // SNumber of sparks per level 
    this.sparkLvls = 4; // Number of levels of sparks 
    this.sparkAngle = []; 
    this.sparkDist = []; 

    // Setup Angles and Distances 
    for(i=0; i<this.sparkLvls; i++) { // 4 Levels of sparks 
     this.sparkAngle[i] = []; 
     this.sparkDist[i] = new Array(this.sparkNum); 
     this.sparkDist[i].fill(i*this.radius); // Set Distance init to 0 

     // Generate angles of sparks randomly 
     for(j=0; j<this.sparkNum; j++) { 
      this.sparkAngle[i][j] = Math.random()*2*Math.PI; 
     } 
     this.sparkAngle[i].sort(); // Sort angles for difference calc 

     // Store Angles as differences between them 
     var temp = []; 
     for(j=1; j<this.sparkNum; j++) { 
      temp[j] = this.sparkAngle[i][j] - this.sparkAngle[i][j-1]; 
     } 
     this.sparkAngle[i] = temp; 
    } 

    this.context = Context; 
} 

ご協力いただければ幸いです。おかげで事前に

+0

num.valueは、HTML上の範囲の現在位置ですdoc。私はまた、それが1よりも大きいことを確認した。 – Cirrith

答えて

2

あなたが使用するのと同じ変数i forループ内

追加するvar(varはグローバル変数があるなし)

for(var i=0; i < num.value; i++) { // in generate function 

for(var i=0; i<this.sparkLvls; i++) { // in Firework function 
+0

それは完璧に働いた。ありがとう! – Cirrith

関連する問題