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

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

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

Processingは、オープンソースプロジェクトによるCGのためのプログラミング言語です。Javaをベースにしており、グラフィック機能に特化しています。イメージの生成やアニメーションなど、視覚的なフィードバックを簡単に得ることが可能です。

Q&A

解決済

2回答

1716閲覧

画面遷移機能 タイトル画面作り方

sho888

総合スコア14

Processing

Processingは、オープンソースプロジェクトによるCGのためのプログラミング言語です。Javaをベースにしており、グラフィック機能に特化しています。イメージの生成やアニメーションなど、視覚的なフィードバックを簡単に得ることが可能です。

1グッド

0クリップ

投稿2022/06/25 08:57

編集2022/06/26 03:16

イメージ説明

タイトル画面を設定したいです。

##スタート画面が始まり、ゲームが実行され、60秒経過したら、終了画面になるようにしたいです。
case0でエラーが出ます。誰か分かる方教えて下さい。

prosseing

1 2String question; 3String answer; 4String input; 5int score;![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-06-26/66e46fec-9b3a-4637-920e-6944c5255320.png) 6float imgX=900; 7float imgVX=1; 8PImage img; 9PImage[] imgs; 10int sce=0; 11 12Button button; 13void setup() { 14 size(800, 800); 15 textSize(48); 16 /* パネルの大きさ*/ 17 rectMode(CENTER, CENTER); 18 colorMode(HSB,360,100,100); 19 textAlign(CENTER,CENTER); 20 21 button=new Button(width/2,height/2,200,100,color(190,100,70),"PUSH!!"); 22 23 24 imgs=new PImage[]{ 25 loadImage("ika.png"), 26 loadImage("sake.png"), 27 }; 28 imgVX=random(1,5); 29float f = random(imgs.length); // 0以上2未満のfloat値(1.234のような) 30 int i = int(f); // 0か1(小数点以下を切り捨てる) 31 img = imgs[i]; 32 33 34} 35 36void draw() { 37 38 39 40 41 42 background(127); 43 44 switch(sce){ 45 case 0; 46 button.run0(); 47 48 if(button.isPush()){ 49 sce=1; 50 } 51 break; 52 53 case 1; 54 55 break; 56} 57 58 59 60 61 showPanel(); // パネルの表示 62 63 mato(); 64 65 66textAlign(LEFT, BASELINE); 67 text("SCORE: " + score, 10, 50); 68 //textAlign(LEFT, BOTTOM); 69 //text(answer, 10, 250); 70 textAlign(CENTER, BOTTOM); 71 text(input, 0, 0, width, 250); 72 73 ps.run(); 74 75 76} 77int reg = 0, val = 0; 78String op = "="; 79 80void calc(int id) { 81 switch(id) { 82 case 0: // 0以外の数字を入力した場合 83 case 1: 84 case 2: 85 case 4: 86 case 5: 87 case 6: 88 case 8: 89 case 9: 90 case 10: 91 if (disp == "0") { // "0" で始まる文字列にならないようにする 92 disp = ""; 93 } 94 disp = disp+label[id]; // dispの後ろに1文字つなぐ 95 break; 96 case 12: // "0" 97 if (disp != "0") { 98 disp = disp + "0"; 99 } 100 break; 101 case 3: // "+/-" プラスマイナス符号の入れ替え 102 if (disp == "0") { 103 } else if (disp.substring(0,1).equals("-")) { 104 // 文字列 disp の最初の文字がマイナスのとき 105 // マイナスを取り去った文字列をdispに代入 106 disp = disp.substring(1); 107 } else { // 先頭にマイナスをつける 108 disp = "-" + disp; 109 } 110 break; 111 case 11: // "+" 112 reg = int(disp); // dispの文字列を整数値にしてしまっておく 113 disp = "0"; 114 op = "+"; // 次にイコールがきたら足し算する 115 break; 116 case 7: // "-" 117 reg = int(disp); 118 disp = "0"; 119 op = "-"; // 次いイコールがきたら引き算する 120 break; 121 case 13: // "=" 計算して結果を文字列として disp に代入 122 val = int(disp); 123 if (op == "+") { 124 op = "="; 125 val = reg + val; 126 disp = str(val); 127 } else if (op == "-") { 128 op = "="; 129 val = reg - val; 130 disp = str(val); 131 } 132 break; 133 default: 134 break; 135 } 136} 137String disp = "0"; 138final float x = 20; 139final float y = 300; 140final float w = 800 - 40; 141final float h = 400; 142final String[] label = { 143 "7", "8", "9", 144 "4", "5", "6", 145 "1", "2", "3", 146 "0", "", "C" }; 147final ParticleSystem ps = new ParticleSystem(); 148 149void mousePressed() { 150 if (mouseX < x || mouseX > x + w) return; 151 if (mouseY < y || mouseY > y + h) return; 152 153 int i = int(3 * (mouseX - x) / w); 154 int j = int(4 * (mouseY - y) / h); 155 String s = label[getID(i, j)]; 156 157 if (s.equals("C")) { // クリア 158 input = ""; 159 return; 160 } 161 162 input += s; 163 if (input.equals(answer)) { // 正解 164 score++; 165 ps.addParticle(new PVector(imgX, 150), question); 166 imgX = 1000; // 範囲外に飛ばすことで、matoでの再設定を誘発させる 167 } 168} 169void showPanel() { 170 rect(x, y, w, h); 171 172 for (int i = 0; i < label.length; i++) { 173 showLabel(i); 174 } 175} 176 177void showLabel(int i) { 178 float bw = w / 3; 179 float bh = h / 4; 180 float bx = x + (i % 3) * bw; 181 float by = y + (i / 3) * bh; 182 183 fill(200); 184 rect(bx, by, bw, bh); 185 fill(0); 186 textAlign(CENTER, CENTER); 187 text(label[i], bx, by, bw, bh); 188} 189 190int getID(int i, int j) { 191 return j * 3 + i; 192} 193 194 195 196 void mato() { 197 if (width < imgX) { 198 imgX = 0; 199 int a = int(random(1, 100)); 200 int b = int(random(1, 100)); 201 question = a + "+" + b; // 問題を文字列化 202 answer = str(a + b); // 答えも文字列化 203 input = ""; 204 205 imgVX = random(1, 5); 206 img = imgs[int(random(imgs.length))];//画像がランダムで選ばれる。 207 } 208 209 image(img, imgX, 150, 100, 100);//X軸を等速直線に動く 210 textAlign(LEFT, BASELINE); 211 text(question, imgX, 150); 212 213 imgX += imgVX; 214} 215 216// [Simple Particle System / Examples / Processing.org](https://processing.org/examples/simpleparticlesystem.html) 217// をベースに小改造(文字列を一文字ずつばらばらに四散させる)運動する粒子で表現される事象 218class ParticleSystem { 219 ArrayList<Particle> particles = new ArrayList<Particle>(); 220 void addParticle(PVector position, String str) { 221 for (char c : str.toCharArray()) { 222 particles.add(new Particle(position, c)); 223 } 224 } 225 void run() { 226 for (int i = particles.size() - 1; i >= 0; i--) { 227 Particle p = particles.get(i); 228 p.run(); 229 if (p.isDead()) { 230 particles.remove(i); 231 } 232 } 233 } 234} 235class Particle { 236 PVector position; 237 PVector velocity = PVector.random3D().mult(10); 238 float lifespan = 255; 239 char c; 240 Particle(PVector l, char c) { 241 position = l.copy(); 242 this.c = c; 243 } 244 void run() { 245 update(); 246 display(); 247 } 248 void update() { 249 position.add(velocity); 250 lifespan -= 2; 251 } 252 void display() { 253 push(); 254 translate(position.x, position.y); 255 rotate(radians(position.z)); 256 fill(0, lifespan); 257 textAlign(CENTER, CENTER); 258 text(c, 0, 0); 259 pop(); 260 } 261 boolean isDead() { 262 return lifespan < 0.0; 263 } 264}class Button {float x,y; 265 float sizeX,sizeY; 266 int state; 267 268 color basecol; 269 float nb; 270 float sb; 271 float pb; 272 273 274 String str; 275 276 Button(float x,float y,float sizeX,float sizeY, 277 color baseCol,String str){ 278 this.x=x; 279 this.y=y; 280 this.sizeY=sizeY; 281 this.sizeX=sizeX; 282 this.baseCol= baseCol; 283 this.str=str; 284 285 nb=1; 286 sb=0.8; 287 pb=0.6; 288 } 289 290 void run0(){ 291 rogic(); 292 display0; 293 } 294 295 void display0(){ 296 noStroke(); 297 changeColor(); 298 rect(x,y,sizeX,sizeY); 299 300 fill(0,0,100); 301 textSize(30); 302 text(str,x,y); 303 } 304 305 void rogic(){ 306 state=checkState(); 307 } 308 309 310 boolean isPush(){ 311 if(checkState()==2) 312 return true; 313 return false; 314 } 315 316 int checkState(){ 317 if (!checkInMouse()) 318 return 0; 319 320 if(!mousePressed) 321 return 1; 322 return 2; 323 } 324 325boolean checkInMouse(){ 326 if (mouseX>x-sizeX/2 && mouseX <x+sizeX/2){ 327 if (mouseY>y-sizeY/2 && mouseY< y+sizeY/2) 328 { return true; 329 } 330 } 331 return false; 332} 333 334void changeColor() { 335 switch(state) { 336 case 0; 337 fill(hue(baseCol),saturation(baseCol),brightness(baseCol)*nb); 338 339 break; 340 341 case 1; 342 fill(hue(baseCol),saturation(baseCol),brightness(baseCol)*pb); 343 344 345 346 case 2; 347 348 fill(hue(baseCol),saturation(baseCol),brightness(baseCol)*pb); 349 350 351 352 break; 353 354 default; 355 fill(0,0,0); 356 break; 357 } 358 } 359 360 361 362
TN8001👍を押しています

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

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

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

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

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

guest

回答2

0

ベストアンサー

エラーに関してはthkanaさんの指摘通り、コロン(:)が必要なところにセミコロン(;)を書いています(それだけでもないですが)

というかButtonのコードはこちらですよね?(参考コードがある場合は質問に明記してください)
ボタンとシーン遷移の実装【Processing】 - トーフメモ

そのままコピペすればこのような間違いは起きなかったのですが、手で打ってるんでしょうか??
run0等微妙に変更している意図も図りかねます。
void calc(int id)等も使用していませんし、おそらくこれもどこかに元ネタがあるんでしょう。

rectModecolorModeは全体に影響しますので、単純に拾ってきたコードを組み合わせてもうまくいかない場合があります。


スタート画面が始まり、ゲームが実行され、60秒経過したら、終了画面になるようにしたいです。

このような単純な遷移であれば、今やっているような方法で十分でしょう。
intだとわかりにくいので、enumをお勧めします。

コードが長くなってきたので、独立性の高いButtonParticleSystemは別タブに分けました。

Processing

1final float x = 20; 2final float y = 300; 3final float w = 800 - 40; 4final float h = 400; 5final String[] label = { 6 "7", "8", "9", 7 "4", "5", "6", 8 "1", "2", "3", 9 "0", "", "C" }; 10final ParticleSystem ps = new ParticleSystem(); 11 12String question; 13String answer; 14String input; 15int score; 16 17float imgX = 900; 18float imgVX; 19PImage img; 20PImage[] imgs; 21 22Button button; 23 24Scene scene = Scene.TITLE; // 現在のシーン 25int startMS; // ゲーム開始時のmillis 26 27enum Scene { // シーンを表すenum 28 TITLE, PLAY, GAMEOVER, 29} 30 31void setup() { 32 size(800, 800); 33 textSize(48); 34 35 button = new Button(width/2, height/2, 200, 100, #0095B4, "PUSH!!"); 36 37 imgs = new PImage[] { 38 //loadImage("ika.png"), 39 //loadImage("sake.png"), 40 loadImage("https://www.gravatar.com/avatar/1f4f0a2782fdb4f59c0acc14a7455416?d=identicon", "png"), 41 loadImage("https://teratail-v2.storage.googleapis.com/uploads/avatars/u13/132786/KnkDDC5A_thumbnail.jpg"), 42 }; 43 imgVX = random(1, 5); 44 img = imgs[int(random(imgs.length))]; 45} 46 47void draw() { 48 background(127); 49 50 switch(scene) { 51 case TITLE: 52 button.run(); 53 if (button.isPush()) { 54 scene = Scene.PLAY; 55 startMS = millis(); 56 } 57 break; 58 59 case PLAY: 60 if (startMS + 60 * 1000 < millis()) { // ゲーム開始時から60秒以上経過していたら... 61 scene = Scene.GAMEOVER; 62 break; 63 } 64 65 showPanel(); 66 mato(); 67 68 textAlign(LEFT, BASELINE); 69 text("SCORE: " + score, 10, 50); 70 //textAlign(LEFT, BOTTOM); 71 //text(answer, 10, 250); 72 textAlign(CENTER, BOTTOM); 73 text(input, 0, 0, width, 250); 74 75 ps.run(); 76 break; 77 78 case GAMEOVER: 79 textAlign(CENTER, CENTER); 80 text("GAME OVER", 0, 0, width, height); 81 break; 82 } 83} 84 85void mousePressed() { 86 switch(scene) { 87 case TITLE: 88 break; 89 90 case PLAY: 91 if (mouseX < x || mouseX > x + w) return; 92 if (mouseY < y || mouseY > y + h) return; 93 94 int i = int(3 * (mouseX - x) / w); 95 int j = int(4 * (mouseY - y) / h); 96 String s = label[getID(i, j)]; 97 98 if (s.equals("C")) { 99 input = ""; 100 return; 101 } 102 103 input += s; 104 if (input.equals(answer)) { 105 score++; 106 ps.addParticle(new PVector(imgX, 150), question); 107 imgX = 1000; 108 } 109 break; 110 111 case GAMEOVER: 112 scene = Scene.TITLE; 113 break; 114 } 115} 116 117void showPanel() { 118 rect(x, y, w, h); 119 120 for (int i = 0; i < label.length; i++) { 121 showLabel(i); 122 } 123} 124 125void showLabel(int i) { 126 float bw = w / 3; 127 float bh = h / 4; 128 float bx = x + (i % 3) * bw; 129 float by = y + (i / 3) * bh; 130 131 fill(200); 132 rect(bx, by, bw, bh); 133 fill(0); 134 textAlign(CENTER, CENTER); 135 text(label[i], bx, by, bw, bh); 136} 137 138int getID(int i, int j) { 139 return j * 3 + i; 140} 141 142void mato() { 143 if (width < imgX) { 144 imgX = 0; 145 int a = int(random(1, 100)); 146 int b = int(random(1, 100)); 147 question = a + "+" + b; 148 answer = str(a + b); 149 input = ""; 150 151 imgVX = random(1, 5); 152 img = imgs[int(random(imgs.length))]; 153 } 154 155 image(img, imgX, 150, 100, 100); 156 textAlign(LEFT, BASELINE); 157 text(question, imgX, 150); 158 159 imgX += imgVX; 160}

Processing:Button.pde

1// [ボタンとシーン遷移の実装【Processing】 - トーフメモ](https://tofgame.hatenablog.com/entry/2019/07/18/175359) 2class Button { 3 float x, y; 4 float sizeX, sizeY; 5 color baseCol; 6 String str; 7 8 float nb = 1; 9 float sb = 0.8; 10 float pb = 0.6; 11 12 int state; 13 14 15 Button(float x, float y, float sizeX, float sizeY, color baseCol, String str) { 16 this.x = x; 17 this.y = y; 18 this.sizeX = sizeX; 19 this.sizeY = sizeY; 20 this.baseCol = baseCol; 21 this.str = str; 22 } 23 24 void run() { 25 rogic(); 26 display(); 27 } 28 29 void display() { 30 push(); 31 32 rectMode(CENTER); 33 colorMode(HSB, 360, 100, 100); 34 textAlign(CENTER, CENTER); 35 36 noStroke(); 37 changeColor(); 38 rect(x, y, sizeX, sizeY); 39 40 fill(0, 0, 100); 41 textSize(30); 42 text(str, x, y); 43 44 pop(); 45 } 46 47 void rogic() { 48 state = checkState(); 49 } 50 51 boolean isPush() { 52 return checkState() == 2; 53 } 54 55 int checkState() { 56 if (!checkInMouse()) return 0; 57 if (!mousePressed) return 1; 58 return 2; 59 } 60 61 boolean checkInMouse() { 62 return mouseX > x - sizeX / 2 && mouseX < x + sizeX / 2 63 && mouseY > y - sizeY / 2 && mouseY < y + sizeY / 2; 64 } 65 66 void changeColor() { 67 switch (state) { 68 case 0: 69 fill(hue(baseCol), saturation(baseCol), brightness(baseCol) * nb); 70 break; 71 case 1: 72 fill(hue(baseCol), saturation(baseCol), brightness(baseCol) * sb); 73 break; 74 case 2: 75 fill(hue(baseCol), saturation(baseCol), brightness(baseCol) * pb); 76 break; 77 default: 78 fill(0, 0, 0); 79 break; 80 } 81 } 82}

Processing:ParticleSystem.pde

1// [Simple Particle System / Examples / Processing.org](https://processing.org/examples/simpleparticlesystem.html) 2// をベースに小改造(文字列を一文字ずつばらばらに四散させる)運動する粒子で表現される事象 3class ParticleSystem { 4 ArrayList<Particle> particles = new ArrayList<Particle>(); 5 6 void addParticle(PVector position, String str) { 7 for (char c : str.toCharArray()) { 8 particles.add(new Particle(position, c)); 9 } 10 } 11 12 void run() { 13 for (int i = particles.size() - 1; i >= 0; i--) { 14 Particle p = particles.get(i); 15 p.run(); 16 if (p.isDead()) { 17 particles.remove(i); 18 } 19 } 20 } 21} 22 23class Particle { 24 PVector position; 25 PVector velocity = PVector.random3D().mult(10); 26 float lifespan = 255; 27 char c; 28 29 Particle(PVector l, char c) { 30 position = l.copy(); 31 this.c = c; 32 } 33 34 void run() { 35 update(); 36 display(); 37 } 38 39 void update() { 40 position.add(velocity); 41 lifespan -= 2; 42 } 43 44 void display() { 45 push(); 46 translate(position.x, position.y); 47 rotate(radians(position.z)); 48 fill(0, lifespan); 49 textAlign(CENTER, CENTER); 50 text(c, 0, 0); 51 pop(); 52 } 53 54 boolean isDead() { 55 return lifespan < 0.0; 56 } 57}

投稿2022/06/25 11:36

編集2023/07/30 10:03
TN8001

総合スコア9317

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

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

sho888

2022/06/26 03:17

すいません。enumのところでエラーがでます。 編集を行い、画像を挿入しました。
TN8001

2022/06/26 03:30

どこかに全角スペース「 」が紛れています。 検索して探して削除するか、 回答コードには入っていないので、「すべて選択」したうえで「張り付け」て上書きしてみてください。
sho888

2022/07/03 07:42

ありがとうございます。解決しました。
guest

0

エラーメッセージを読みましょう。
エラー画面
これと同じエラーが出ているものと思います。

(そのエラー以外は見ていませんが...)

投稿2022/06/25 09:11

thkana

総合スコア7629

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問