Операторы if в AnimationTimer, находящиеся в цикле for-eachJAVA

Программисты JAVA общаются здесь
Ответить Пред. темаСлед. тема
Anonymous
 Операторы if в AnimationTimer, находящиеся в цикле for-each

Сообщение Anonymous »

Я пытаюсь создать игру, в которой каждый раз, когда круг, управляемый игрой, пересекается со спрайтами «врага», рядом с кругом будет отображаться метка с надписью «ой»

Код: Выделить всё

Label ouch = new Label("ouch");
pane.getChildren().add(ouch);

for(StackPane s:sprites){
new AnimationTimer(){
double speed = (Math.random()*1 +.1 );
double dx = speed * Math.cos(Math.random()*5 +1);
double dy = speed * Math.sin(Math.random()*5 +1);
@Override
public void handle(long now){
s.setLayoutX(s.getLayoutX()+dx);
Bounds bounds = pane.getLayoutBounds();
s.setLayoutY(s.getLayoutY()+dy);
if((s.getLayoutX() =(bounds.getMaxX()))){
dx = -dx;
}
if((s.getLayoutY() -10 =(bounds.getMaxY()))){
dy = -dy;
}
if(c.getBoundsInParent().intersects(s.getBoundsInParent())){
ouch.setVisible(true);
ouch.setTranslateX(c.getTranslateX());
ouch.setTranslateY(c.getTranslateY()+c.getRadius()+20);

}
else{ouch.setVisible(false);}
}
}.start();
}
При необходимости приведем полный код:

Код: Выделить всё

import java.util.Random;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.scene.control.Button;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.PasswordField;
import javafx.scene.control.Label;
import javafx.geometry.Pos;
import javafx.scene.control.TextField;
import javafx.scene.text.Font;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.animation.KeyFrame;
import javafx.scene.text.FontWeight;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.Pane;
import javafx.geometry.Insets;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.CubicCurve;
import javafx.scene.shape.Line;
import javafx.util.Duration;
import javafx.animation.Timeline;
import javafx.scene.control.Alert;
import javafx.scene.shape.Arc;
import javafx.scene.input.MouseEvent;
import javafx.scene.Node;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.text.Text;
import javafx.scene.shape.StrokeType;
import java.util.ArrayList;
import javafx.geometry.Bounds;

public class Game extends Application{
Stage window;

Text bottomText = new Text();
private final Integer startTime = 10;
private Integer seconds = startTime;
private Integer score = 30;

@Override
public void start(Stage PrimaryStage){

Image mouse = new Image("C:\\Users\\1559990\\Desktop\\images\\mouse.jpg");
ImageView image = new ImageView(mouse);
Label label = new Label("Times up!");
Label label2 = new Label("Your score is: " + score);
VBox vbox = new VBox(20);
vbox.getChildren().addAll(image,label,label2);
vbox.setAlignment(Pos.CENTER);
Scene scene2 = new Scene(vbox,600,600);

Color yaleBlue = Color.web("154179");
Color lightBlue = Color.web("9FC2CC");

window = PrimaryStage;
window.setTitle("Game");
BorderPane borderPane = new BorderPane();

bottomText.setFont(Font.font("Calibri",FontWeight.BOLD,100));
bottomText.setFill(lightBlue);
//bottomText.setStrokeWidth(2);
//bottomText.setStroke(lightBlue);
//bottomText.setStrokeType(StrokeType.OUTSIDE);
bottomText.setText(startTime.toString());

Text topText = new Text("GAME");
topText.setFont(Font.font("Arial",FontWeight.BOLD,50));
topText.setFill(yaleBlue);
topText.setStrokeWidth(1);
topText.setStroke(lightBlue);
topText.setStrokeType(StrokeType.OUTSIDE);

//borderPane.setBottom(bottomText);
borderPane.setAlignment(bottomText,Pos.CENTER);
borderPane.setTop(topText);
borderPane.setAlignment(topText,Pos.CENTER);
borderPane.setPadding(new Insets(10,10,10,10));

Scene scene = new Scene(borderPane,600,600);
window.setScene(scene);
Pane pane = new Pane();

ArrayList sprites = new ArrayList();
String[] browTypes = new String[]{"angry","sad","normal"};

for(int i = 0;i= bounds.getMinY())){
c.setTranslateY(c.getTranslateY()-5 );
}
if(hasMultiple(allKeyPressed,"down") && (c.getTranslateY() + c.getRadius()= bounds.getMinX() + 20)){
c.setTranslateX(c.getTranslateX()-5);
}
if(hasMultiple(allKeyPressed,"right") && (c.getTranslateX() 

Подробнее здесь: [url]https://stackoverflow.com/questions/78177663/if-statements-in-animationtimer-thats-in-a-for-each-loop[/url]
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение
  • Как получить значение скрытого поля в цикле jquery checkbox.each?
    Anonymous » » в форуме Jquery
    0 Ответы
    23 Просмотры
    Последнее сообщение Anonymous
  • Переход метки с помощью AnimationTimer в javafx
    Anonymous » » в форуме JAVA
    0 Ответы
    8 Просмотры
    Последнее сообщение Anonymous
  • Переход метки с помощью AnimationTimer в javafx
    Anonymous » » в форуме JAVA
    0 Ответы
    7 Просмотры
    Последнее сообщение Anonymous
  • Переход метки с помощью AnimationTimer в javafx
    Anonymous » » в форуме JAVA
    0 Ответы
    8 Просмотры
    Последнее сообщение Anonymous
  • Переход метки с помощью AnimationTimer в javafx
    Anonymous » » в форуме JAVA
    0 Ответы
    10 Просмотры
    Последнее сообщение Anonymous

Вернуться в «JAVA»