質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

Q&A

解決済

1回答

978閲覧

JavaScript入門者 クラスのメンバ関数が参照できない理由が知りたい。

退会済みユーザー

退会済みユーザー

総合スコア0

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

0グッド

0クリップ

投稿2022/01/11 05:28

提示コードの///コメント部class PuyoRenderer()関数下部のコードですがこのコードで以下のエラー出ます。これはなぜでしょうか?

確認したこと

Vector.jsファイルの中身を見て定義を確認
this,newのつけ忘れを確認
他のコードが間違ていないか一通り確認

エラーコード
Game.js:78 Uncaught TypeError: Cannot read properties of undefined (reading 'x') at Puyo.Renderer (Game.js:78:30) at Game.js:184:18 at Array.forEach (<anonymous>) at PuyoPuyo.Renderer (Game.js:182:19) at Game.Renderer (Game.js:216:19) at draw (Main.js:20:10) at p5._main.default.redraw (p5.js:70809:27) at _draw (p5.js:63046:25)
Game.js

javascirpt

1"use strict" 2 3const PUYO_SIZE = 25; //ぷよサイズ 4 5 6// ################################################################ 7// # ぷよ 8// ################################################################ 9class Puyo 10{ 11 12 getColor(rand) 13 { 14 15 16 } 17 18 constructor() 19 { 20 this.colorType = Math.floor(random(0,5)); //種類 21 this.position = new Vector(0,0); //座標 22 } 23 24 setPosition(pos) 25 { 26 this.position = pos; 27 } 28 29 30 31 Renderer() 32 { 33 //色 34 switch(this.colorType) 35 { 36 //赤 37 case 0: 38 { 39 fill(255,0,0); 40 } 41 break; 42 43 //青 44 case 1: 45 { 46 fill(24,235,249); 47 } 48 break; 49 50 51 //黄色 52 case 2: 53 { 54 fill(255,255,0); 55 } 56 break; 57 58 59 //緑 60 case 3: 61 { 62 fill(0,128,0); 63 } 64 break; 65 66 //紫 67 case 4: 68 { 69 fill(0,0,128); 70 } 71 break; 72 73 74 } 75 76 noStroke(); 77 78//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 79 circle(this.position.x,this.position.y,PUYO_SIZE); 80//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 81 } 82} 83 84 85// ################################################################ 86// # 操作 87// ################################################################ 88class Control 89{ 90 91 constructor() 92 { 93 this.position = new Vector(0,0); 94 this.press = false; 95 } 96 97 Update() 98 { 99 100 if(keyIsDown(UP_ARROW)) 101 { 102 103 } 104 else if(keyIsDown(LEFT_ARROW) && (this.press == false) ) 105 { 106 this.position.x += -PUYO_SIZE; 107 this.press = true; 108 } 109 else if(keyIsDown(RIGHT_ARROW) && (this.press == false) ) 110 { 111 this.position.x += PUYO_SIZE; 112 this.press = true; 113 114 } 115 116 if( (keyIsDown(LEFT_ARROW) == false) && (keyIsDown(RIGHT_ARROW) == false) ) 117 { 118 this.press = false; 119 } 120 121 122 } 123 124 125 getPosition() 126 { 127 return this.position; 128 } 129 130} 131 132// ################################################################ 133// # ぷよぷよ 134// ################################################################ 135class PuyoPuyo 136{ 137 constructor() 138 { 139 this.positon = new Vector(0,0); 140 this.type = Math.floor(random(0,2)); 141 this.puyo; 142 143 if(this.type == 0) 144 { 145 this.puyo = new Array(new Puyo,new Puyo); 146 } 147 else 148 { 149 this.puyo = new Array(new Puyo,new Puyo,new Puyo,new Puyo); 150 } 151 } 152 153 setPosition(pos) 154 { 155 this.position = pos; 156 } 157 158 Update() 159 { 160 if(this.type == 0) 161 { 162 this.puyo[0].setPosition(this.position); 163 this.puyo[1].setPosition(this.position + new Vector(0,PUYO_SIZE)); 164 } 165 else 166 { 167 this.puyo[0].setPosition(this.position); 168 this.puyo[1].setPosition(this.position + new Vector(0,PUYO_SIZE)); 169 this.puyo[2].setPosition(this.position + new Vector(PUYO_SIZE,PUYO_SIZE)); 170 this.puyo[3].setPosition(this.position + new Vector(PUYO_SIZE,PUYO_SIZE * 2)); 171 172 } 173 } 174 175 Renderer() 176 { 177 this.puyo.forEach(function(item) 178 { 179 item.Renderer(); 180 }) 181 } 182 183 184} 185 186 187 188 189// ################################################################ 190// # ループ 191// ################################################################ 192class Game 193{ 194 195 196 constructor() 197 { 198 this.puyo = new PuyoPuyo(); 199 this.control = new Control(); 200 } 201 202 Update() 203 { 204 this.control.Update(); 205 this.puyo.Update(); 206 this.puyo.setPosition(this.control.getPosition()); 207 } 208 209 Renderer() 210 { 211 this.puyo.Renderer(); 212 //this.puyo.Renderer(); 213 //fill(color(0,255,0,255)); 214 //noStroke(); 215 //circle(this.position.x,this.position.y,CELL_SIZE); 216 } 217 218 219}

index.html

<!DOCTYPE html> <html lang=ja> <head> <meta charset="utf-8"> <title>puyopuyo</title> <body> <link rel="stylesheet" href="style.css"> <script src=../p5/p5.js></script> <!-- ユーティリティ --> <script src=../Utility/Math.js></script> <script src=../Utility/Vector.js></script> <script src=../Utility/Collision.js></script> <!-- ゲームループ --> <script src="Script/Game.js"></script> <script src=Script/Main.js></script> </body> </head> </html>
utillity/Vector.js
const PI = 3.14159; //円周率 const sinSpeed = PI / 100.0; //sin波 加算 const speed = 10; //移動速度 //ベクトルの引き算  //引数 Vector型 function VectorSub(a,b) { let xx = a.x - b.x; let yy = a.y - b.y; return new Vector(xx,yy); } function Range(a,b) { return sqrt((a.x * a.x) + (b.y * b.y)); } //ベクター class Vector { constructor(xx,yy) { this.x = xx; this.y = yy; } } //線分 class LineSegment { constructor(a,b) { this.start = new Vector(a.x,a.y); this.end = new Vector(b.x,b.y); } }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

デバッグしましょう。エラーメッセージの意味はわかりますよね。

ざっと見ると、まずは PuyoクラスのsetPosition()が変な引数で呼ばれていないかをチェックすべきかと思いました。setPosition()console.assert(pos instanceof Vector); とか入れておくと役立ちそうです。

投稿2022/01/11 05:43

編集2022/01/11 05:48
int32_t

総合スコア20880

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2022/01/11 05:49

this.position = new Vector(0,0); のthis.postionをスペルミスしていました。
m.ts10806

2022/01/11 05:50

一行目は永遠に伝わらなそうですね(多数から何度も指摘されてることを今更まだ指摘されてるから)
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問