2016-04-16 8 views
0

私は最終的なプロジェクトに取り組んでおり、ランダムにウィンドウの周りを動くドットを持つゲームを作っています。クリックすると、ユーザーのスコアが増加します。私はどのようにドットをランダムに1/2秒ごとに動かすのかと思っています。ここまで私が今までに持っていたすべてのコードをありがとう!ペインのランダムな場所にシェイプを配置する方法は?

import javafx.application.Application; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.stage.Popup; 
import javafx.stage.Stage; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.input.MouseEvent; 
import javafx.scene.layout.Pane; 
import javafx.scene.shape.Circle; 
import javafx.scene.shape.Line; 
import javafx.scene.text.Text; 
import java.util.Random; 
import java.util.Timer; 
import java.util.TimerTask; 


public class CatchTheDot extends Application{ 

//create ball for game 
public static Circle dot; 

//create pane to run game 
public static Pane window; 

//create score counter 
int score = 0; 

//creat random 
Random r = new Random((21)+1); 

@Override 
public void start(Stage primaryStage) throws Exception { 

    window.getChildren().addAll(dot); 

    // create scene and place on pane 
    Scene s = new Scene(window, 800, 800); 
    primaryStage.setTitle("Catch The Dot"); 
    primaryStage.setScene(s); 
    primaryStage.show(); 

    //move dot 
    Timer timer = new Timer(); 
    timer.schedule(new TimerTask() 
     { 
     @Override 
     public void run(){ 
     window.getChildren().remove(dot); 
     int randX = 1 + (int)(Math.random()*800); 
     int randY = 1 + (int)(Math.random()*800); 
     window.add(dot, randX, randY); 
     } 
     }, 0, 5000);   


    // create listener 
    ActionEvent mouseClicked(ActionEvent event) 
    { 
     if(event.getSource()==dot) 
     { 
      score = score + 10; 
      if(score == 50) 
      { 
       popUp(primaryStage); 
      } 
     } 
    } 
} 

public void popUp(final Stage primaryStage) 
{ 
    primaryStage.setTitle("You won!"); 
    final Popup popup = new Popup(); 
    popup.setX(300); 
    popup.setY(200); 
    Text t = new Text("You won! Nice job!"); 
    Text tt = new Text("Play again?"); 
    Button yes = new Button("yes"); 
    Button no = new Button("no"); 
    popup.getContent().addAll(t, tt, yes, no); 
    yes.setOnAction(e -> Yes()); 
    no.setOnAction(e -> No()); 
} 

public void Yes() 
{ 
    restartGame(); 
} 
public void No() 
{ 
    System.exit(0); 
} 
public void restartGame() 
{ 
    score = 0; 

} 
public static void main(String[] args) 
{ 
    launch(args); 
} 

}の代わりにMath.random()は、私はRandomを使用することをお勧めし使用しての

+0

ランダムな位置にジャンプしたいのですか、それとも連続的に移動する必要がありますか? –

+0

@FrankPufferランダムな位置にジャンプ – Adam7397

答えて

-1

Random random = new Random(); 
int ranX = random.nextInt(width-1); // random value from 0 to width 
int ranY = random.nextInt(height-1); // random value from 0 to height 

残りはうまく見えます。

+0

ありがとう!私は行にエラーが表示されます "window.add(dot、ranX、ranY);"このタイプの枠がこのタイプの枠に対して定義されていないと言っています。どのような種類のペインを使うべきなのか分かりますか?このエラーは出ませんか? – Adam7397

+0

私はそれが事実の窓と関係があると思うのは窓です。私は'add(foo bar) 'はAPIの一部だとは思わない。 JFrame、JPanel、Canvasがこれを持っていて、あなたが探しているものかも知れません。あなたが使うはずのものに答えるために、私はキャンバスがあなたの最善の策だと言うでしょう。 –

+0

よかった!私はそれらを試してみましょう!ありがとうございました! – Adam7397

関連する問題