pauish:
P5とアルカノイドをやって - 私と私はクラスのブロックを作成し、単にコンストラクタとメソッドの引き分けです。
class Block{
constructor(widthBlock, heightBlock, x, y, colorBlock){
this.widthBlock=widthBlock;
this.heightBlock=heightBlock;
this.x = x;
this.y=y;
this.colorBlock=colorBlock;
}
draw(){
fill(this.colorBlock);
rect(this.x, this.y, this.widthBlock, this.heightBlock);
console.log('bloke');
}
}
スケッチクラスでは、私はブロックと呼ばれる配列を満たし、私はドロー機能でそれを呼び出すが、それは、このエラースロー sketch.jsを:25キャッチされない例外TypeError:プロパティを読み取ることができません未定義の「描く」(ライン25は、ブロック[i]が.draw(あり) ;)
for(let i=0; i<=blockAmount; i++){
for(let j=0; i<=5; i++){
blocks.push(new Block(widthBlock*i, widthBlock*j ,widthBlock, 20, color('pink')));
console.log(blocks.length);
}
}
function draw() {
for(let i=0; i<=blocks.length; i++){
//console.log('aasas');
console.log(blocks[i]);
blocks[i].draw();
noLoop();
}
}
ジャック:
function draw() {
for(let i=0; i<=blocks.length; i++){
//console.log('aasas');
console.log(blocks[i]);
blocks[i].draw();
noLoop();
}
}
問題は、反復処理しているということですblocks
が、i<=blocks.length
条件は評価するために、最後のループ反復の原因とblocks[blocks.length]
されます、undefined
。
この問題を解決するには、次のいずれかに条件を修正することができi<blocks.length
、または使用for...of
:
function draw() {
for (const block of blocks) {
block.draw();
noLoop();
}
}