2016-04-03 13 views
0

私のプロジェクトでは私のグループと私はゲームをプログラムしようとしています。このゲームの目的は、他のプレーヤーの船の色を変更することです。Crafty.jsで 'Uncaught TypeError:プロパティを読み込めません'という 'undefinedの呼び出し'を修正するにはどうすればよいですか?

例:プレーヤー1の船が赤色で、プレーヤー2の船が緑色の場合、プレイヤー1の各弾丸がプレーヤー2に当たると、プレーヤー2はゆっくりと赤色から緑色に変わります。これは、以下で説明するビットシフトの助けによって行われます。だから大部分の時間、私たちはUncaught Type Errorに遭遇しており、問題が何かを見つけることができません。このファイルでは、プログラムの実行に役立つように、https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js および http://craftyjs.com/release/0.4.2/crafty.js を使用しています。

$(document).ready(function() { 

Crafty.init(); 
Crafty.canvas(); 



Crafty.load(["player.png"], function() { 
    //splice the spritemap 
    Crafty.sprite(1, "player.png", 
    { 
     ship: [0,0] 
    }); 

    //start the main scene when loaded 
    Crafty.scene("main"); 
}); 

    //player entity for player 1 
    var player1 = Crafty.e("2D, Canvas, Controls, Collision, Color, ship, player") 
     .attr({move: {left: false, right: false, up: false, down: false}, xspeed: 0, yspeed: 0, decay: 0.9, h: 50, w: 50, radius: 50, start_time: 0, x: Crafty.viewport.width/2, y: Crafty.viewport.height/2 }) 
     .color('red') 
     .bind("keydown", function(e) { 
      //on keydown, set the move booleans 
      if(e.keyCode === Crafty.keys.RIGHT_ARROW) { 
       this.move.right = true; 
      } else if(e.keyCode === Crafty.keys.LEFT_ARROW) { 
       this.move.left = true; 
      } else if(e.keyCode === Crafty.keys.UP_ARROW) { 
       this.move.up = true; 
      } else if(e.keyCode === Crafty.keys.SPACE) { 
       var d = new Date(); 
       this.start_time = d.getTime(); 
      } 
     }).bind("keyup", function(e) { 
      //on key up, set the move booleans to false 
      if(e.keyCode === Crafty.keys.RIGHT_ARROW) { 
       this.move.right = false; 
      } else if(e.keyCode === Crafty.keys.LEFT_ARROW) { 
       this.move.left = false; 
      } else if(e.keyCode === Crafty.keys.UP_ARROW) { 
       this.move.up = false; 
      } else if(e.keyCode === Crafty.keys.SPACE) { 
       var time = new Date().getTime(); 
       if((time - this.start_time) >= 5000) 
        var charge = 5; 

       //else 
       //var charge = (time - this.start_time)/1000; 

       Crafty.e("2D, DOM, Color, bullet") 
        .attr({ 
         x: this._x, 
         y: this._y, 
         w: 1.5, 
         h: 1.5, 

         rotation: this._rotation, 
         xspeed: 20 * Math.sin(this._rotation/57.3), 
         yspeed: 20 * Math.cos(this._rotation/57.3), 



        }) 
        .color('red') 
        .bind("enterframe", function() {  
         this.x += this.xspeed; 
         this.y -= this.yspeed; 

         //destroy if it goes out of bounds 
         if(this._x > Crafty.viewport.width || this._x < 0 || this._y > Crafty.viewport.height || this._y < 0) { 
          this.destroy(); 
         } 
        }); 
      } 


     }).bind("enterframe", function() { 
      if(this.move.right) this.rotation += 5; 
      if(this.move.left) this.rotation -= 5; 

      //acceleration and movement vector 
      var vx = Math.sin(this._rotation * Math.PI/180) * 0.3, 
       vy = Math.cos(this._rotation * Math.PI/180) * 0.3; 

      //if the move up is true, increment the y/xspeeds 
      if(this.move.up) { 
       this.yspeed -= vy; 
       this.xspeed += vx; 
      } else { 
       //if released, slow down the ship 
       this.xspeed *= this.decay; 
       this.yspeed *= this.decay; 
      } 

      //move the ship by the x and y speeds or movement vector 
      this.x += this.xspeed; 
      this.y += this.yspeed; 

      //if ship goes out of bounds, put him back 
      if(this._x > Crafty.viewport.width) { 
       this.x = -64; 
      } 
      if(this._x < -64) { 
       this.x = Crafty.viewport.width; 
      } 
      if(this._y > Crafty.viewport.height) { 
       this.y = -64; 
      } 
      if(this._y < -64) { 
       this.y = Crafty.viewport.height; 
      } 

     }).collision() 
     .onHit("bullet", function(e) { 
     //basically the bullet is color A and hits ship B and changes the color to ship A 
     //bullets are based on ship A 
     //red to green 
      if(e.color() != 'red'){ 
       /* 
      if(e.color() === "#FF0000" && this.start_color === "#00FF00") 
      { 
       this.color = this.color + ("#010000" - "#000001") * e.radius; 

        //red to blue 
      } else if(e.color === "#FF0000" && this.color === "#0000FF") 
      { 
       this.color = this.color + ("#010000" - "#000001") * e.radius; 

      } 
      */ 
      //green to red 
      if(e.color() === "#00FF00") 
      { 
       this.color(this.color() + ("#010000" - "#000001") * e.radius); 

      } 
      /* 
      //green to blue 
      else if(e.color === "#00FF00" && this.color === "#0000FF") 
      { 
       this.color = this.color + ("#010000" - "#000001") * e.radius; 

      } 
      */ 
      //blue to red 
      else if(e.color() === "#0000FF") 
      { 
       this.color(this.color() + ("#010000" - "#000001") * e.radius); 
      } 
      /* 
      //blue to green 
      else (e.color === "#0000FF" && this.color === "#00FF00") 
      { 
       this.color = this.color + ("#010000" - "#000001") * e.radius; 
      } 
      */ 
      if(this.color() === e.color()){ 
        Crafty.scene("end"); 
      } 

      this.xspeed = this.xspeed - .1*e.xspeed; 
      this.yspeed = this.yspeed - .1*e.yspeed; 
      e[0].obj.destroy(); 
     } 

     }).onHit("player", function(e) { 

       var diff = "#ff" - (this.color()>>4); 
       this.color(this.color() + (.2*diff) << 4); 
       if(e.color() === "green") { 
        this.color(this.color() - (.2*diff) << 2); 
       } 
       else { 
        this.color(this.color() - .2*diff); 
       } 


      this.xspeed = this.xspeed - e.xspeed; 
      this.yspeed = this.yspeed - e.yspeed; 
     }); 

}); 

編集:私は基本的にHTMLファイル内のメインに新しい読み取りを行うと、プロンプト$(文書)を削除することによって、私は、エラーを修正し、コメントし、いくつかの無関係なコード

+0

あなたは無関係のコードを取り出せますか?それは1つのエラーを探すコードがたくさんあります –

+0

私はいくつかのコードを取り出しました。 –

+0

「色」のものは私にとって怪しいものです。 [api docs](http://craftyjs.com/api/Color.html)は、色を設定するための関数として使用されていることだけを述べています。プロパティーのようなアクセスやゲッター機能はありません。 – mucaho

答えて

0

を取ることができました。あなたのすべての協力に感謝します!

関連する問題