提示コードのコメント部//////////
内部コードですがプロパティを用意してもなぜ未定義と表示されるのでしょうか?参考サイトの通りにやってもエラーが出ます。
参考サイト: https://www.tohoho-web.com/js/class.htm
Error
1Uncaught TypeError: Cannot read properties of undefined (reading 'getPosition') 2 at Game.js:134 3 at Array.forEach (<anonymous>) 4 at Game.Update (Game.js:131) 5 at Entry.Loop (Entry.js:13) 6 at draw (Main.js:19) 7 at p5._main.default.redraw (p5.js:70809) 8 at _draw (p5.js:63046)
javascript
1 2class Enemy 3{ 4 constructor(pos) 5 { 6 this.position = new Vector(pos.x,pos.y); 7 this.alive = true; 8 this.size = 50; 9 } 10 11 Init(pos) 12 { 13 this.position = new Vector(pos.x,pos.y); 14 this.alive = true; 15 16 } 17 18 Update() 19 { 20 21 } 22 23 Renderer() 24 { 25 //fill(color(0,100,0)); 26 line(this.position.x,this.position.y,this.position.x + this.size,this.position.y + this.size); 27// triangle(this.position.x,this.position.y + this.size, 28 // this.position.x - this.size,this.position.y - this.size, 29 // this.position.x + this.size,this.position.y - this.size); 30 31 } 32 33} 34 35 36class Player 37{ 38 constructor() 39 { 40 this.acce = new Vector(0,0); //sin波 41 this.position = new Vector(300,300); 42 this.acce = new Vector(0,0); 43 this.alive = true; 44 this.size = 100; 45 } 46 47 get getPosition() 48 { 49 return this.position; 50 } 51 52 set setPosition(p) 53 { 54 this.position.x = p.x; 55 this.position.y = p.y; 56 57 } 58 59 60 Update() 61 { 62 KeyInput(this.position,this.acce); 63 } 64 65 66 Renderer() 67 { 68 line(this.position.x,this.position.y,this.position.x + this.size,this.position.y + this.size); 69 70 } 71 72} 73 74class Bullet 75{ 76 77} 78 79 80class Game 81{ 82 83 //オブジェクトプール 84 EnemyInstance(pos) 85 { 86 this.enemy.forEach(item => 87 { 88 89 if(item.alive === false) 90 { 91 item.Init(pos); 92 return; 93 } 94 95 96 }); 97 98 this.enemy.push(new Enemy(new Vector(pos.x,pos.y))); 99 return; 100 } 101 102 constructor() 103 { 104 105 this.enemy = new Array(); //Enemy 配列 106 this.Player = new Player(); //Player 107 108 for(let i = 0; i< 10; i++) 109 { 110 this.EnemyInstance(new Vector(100 + 40 * i,100)); 111 } 112 113 114 } 115 116 117 Collision(posA,posB) 118 { 119 if(Collision.LineSegment(posA,posB) == true) 120 { 121 return true; 122 } 123 else 124 { 125 return false; 126 } 127 } 128 129 Update() 130 { 131 this.enemy.forEach(item => 132 { 133 item.Update(); 134//////////////////////////////////////////////////////////////////////////////////////////////////////////// 135 if((this.Collision(this.player.getPosition,item.position) === true) && (item.alive === true)) 136 { 137 item.alive = false; 138 } 139//////////////////////////////////////////////////////////////////////////////////////////////////////////// 140 }); 141 142 143 144 145 } 146 147 148 Renderer() 149 { 150 this.enemy.forEach( item => item.Renderer()); 151 this.plyer.Renderer(); 152 } 153 154 155 156 157 158 159}
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2021/09/18 23:43