- 前提・実現したいこと
AnimationTimer()でサイクルをループさせたい
1.1つ目のボタンの表示
2.ボタン1(画像)が5回押されたら3に移行
(ボタン1が押された時のみ画像が変化
3.ボタン2(画像)が5回押されたら1に戻る
(ボタン2が押された時のみ画像が変化
またゲームなどの画面切り替えの際にsceneを複数用意して条件によって切り替えるのか
scene1つだけ作りsceneの中身を条件によって変更していくのは
どちらが一般的な方法でしょうか?
贅沢な質問申し訳ありません
- 対応するコード
java
1package novelgame; 2 3import javafx.animation.AnimationTimer; 4import javafx.application.Application; 5import javafx.event.EventHandler; 6import javafx.scene.Group; 7import javafx.scene.Scene; 8import javafx.scene.canvas.Canvas; 9import javafx.scene.canvas.GraphicsContext; 10import javafx.scene.image.Image; 11import javafx.scene.input.MouseEvent; 12import javafx.scene.media.AudioClip; 13import javafx.stage.Stage; 14 15public class test2 extends Application 16{ 17 int num = 1; 18 int count = 1; 19 double pw,pv= 0; 20 Scene theScene1; 21 Stage theStage1; 22 GraphicsContext graphicsContext; 23 Image image; 24 Image image2; 25 Image image3; 26 Image image4; 27 AudioClip plonkSound; 28 static int width = 512; 29 static int heigh = 256; 30 31 public static void main(String[] args) 32 { 33 launch(args); 34 } 35 36 @Override public void start(Stage theStage) 37 { 38 this.theStage1 = theStage; 39 40 new AnimationTimer() 41 { 42 @Override 43 public void handle(long now) 44 { 45 switch (num) 46 { 47 case 1: 48 System.out.println(1); 49 set(); 50 num = num + 1; 51 case 2: 52 System.out.println(2); 53 botten1(); 54 break; 55 case 3: 56 System.out.println(3); 57 botten2(); 58 break; 59 } 60 } 61 };//;を追記 62 63 theScene1.setOnMousePressed( 64 new EventHandler<MouseEvent>() 65 { 66 public void handle(MouseEvent pe) 67 { 68 pw=pe.getX(); 69 pv=pe.getY(); 70 71 System.out.println("Push X=" + pw + " Y=" + pv); 72 } 73 }); 74 75 } 76 77 void set(){ 78 theStage1.setTitle("てすと"); 79 Image image = new Image(getClass().getResource("kimi.png").toExternalForm()); 80 Image image2 = new Image(getClass().getResource("kimi2.png").toExternalForm()); 81 Image image3 = new Image(getClass().getResource("manzi.png").toExternalForm()); 82 Image image4 = new Image(getClass().getResource("manzi2.png").toExternalForm()); 83 //画像サイズは全て100*150 84 85 AudioClip plonkSound = new AudioClip(getClass().getResource("kimi.mp3").toExternalForm()); 86 87 Group root = new Group(); 88 Canvas canvas = new Canvas(width, heigh); 89 GraphicsContext graphicsContext = canvas.getGraphicsContext2D(); 90 graphicsContext.drawImage(image, 100 , 100); 91 root.getChildren().add(canvas); 92 Scene theScene = new Scene(root); 93 this.theScene1 = theScene; 94 this.image = image; 95 this.image2 = image2; 96 this.image3 = image3; 97 this.image4 = image4; 98 this.graphicsContext = graphicsContext; 99 this.plonkSound = plonkSound; 100 theStage1.setWidth(280); 101 theStage1.setHeight(280); 102 theStage1.setScene(theScene1); 103 theStage1.show(); 104 } 105 106 void botten1(){ 107 while(true){ 108 graphicsContext.clearRect(0, 0, width, heigh); 109 if(pw>100 && pw<250 && pv>100 && pv<200){ 110 System.out.println("ボタン1が押されたよ"); 111 graphicsContext.drawImage(image2, 100 , 100); 112 plonkSound.play(); 113 count = count + 1; 114 }else{ 115 graphicsContext.drawImage(image, 100 , 100); 116 } 117 if(count == 5){ 118 num = 3; 119 count = 1; 120 break; 121 } 122 123 } 124 } 125 126 void botten2(){ 127 while(true){ 128 graphicsContext.clearRect(0, 0, width, heigh); 129 if(pw>0 && pw<150 && pv>0 && pv<100){ 130 System.out.println("ボタン2が押されたよ"); 131 graphicsContext.drawImage(image4, 0 , 0); 132 System.out.println("音楽スタート"); 133 count = count + 1; 134 }else{ 135 graphicsContext.drawImage(image3, 0 , 0); 136 } 137 138 if(count == 5){ 139 count = 1; 140 num = 1; 141 break; 142 } 143 } 144 } 145}
- 追記エラーメッセージ
run:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
at novelgame.test2.start(test2.java:64)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Exception running application novelgame.test2
/Users/username/Library/Caches/NetBeans/8.2/executor-snippets/run.xml:53: Java returned: 1
ビルド失敗(合計時間: 2秒)
- 補足情報(言語/FW/ツール等のバージョンなど)
macのNetBeans 8.2環境
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/06/22 17:42