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

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

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

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

Q&A

解決済

1回答

1059閲覧

JavaScript 入門者 クラス + new クラス(0,PUYO_SIZE)のundefinedを対処する方法が知りたい。

退会済みユーザー

退会済みユーザー

総合スコア0

JavaScript

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

0グッド

0クリップ

投稿2022/01/11 07:21

編集2022/01/11 07:40

提示コードの///コメント部内部のコードですがthis.puy[0].setPosition(this.position + new Vector(0,PUYO_SIZE));ですがこのnew Vector()が原因でundefined という現象が起きてしまします。自分はこのような vectorクラス + new vecotrクラス という処理を作りたいのですがこれをどう記述すればいいのでしょうか?

javascript

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

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

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

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

int32_t

2022/01/11 07:28

最も重要な Vector クラスのコードがありません。 Vectorと + を使った簡単なコードがあれば、現在質問文に含まれているコードはすべて不要かと思います。
int32_t

2022/01/11 07:29

> クラス + new クラス(0,PUYO_SIZE)がundefinedを対処する方法 日本語がおかしいです
退会済みユーザー

退会済みユーザー

2022/01/11 07:40

文章を修正しました。
guest

回答1

0

ベストアンサー

this.position + new Vector(0,PUYO_SIZE)

this.positionundefinedVector のインスタンスだとしても、この結果が undefined になることはありません。これが undefined になるというのは、何かの勘違いでしょう。

vectorクラス + new vecotrクラス という処理を作りたいのですが

できません。JavaScript では + 演算子の挙動を変えることはできません。

代わりに、Vector にメソッドを足しましょう。

js

1class Vector { 2 ... 3 add(other) { 4 console.assert(other instanceof Vector); 5 return new Vector(this.x + other.x, this.y + other.y); 6 } 7 ... 8 9 10// 使用例 11.setPosition(this.position.add(new Vector(0, PUYO_SIZE))); 12

投稿2022/01/11 07:52

編集2022/01/11 07:54
int32_t

総合スコア20884

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

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

退会済みユーザー

退会済みユーザー

2022/01/11 08:01 編集

誤投稿です。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問