简体   繁体   English

Java FX 移动形状

[英]Java FX moving shapes

I wanted to ask if you have any ideas on how to solve my problem?我想问一下您对如何解决我的问题有任何想法吗? What i am trying to achieve is moving things in a direction but keep spawning instances in set y-position我想要实现的是将事物朝一个方向移动,但在设置的 y 位置保持生成实例

public class Balony extends Canvas {
    Group root;
    GraphicsContext gc;
    Timeline t;
    Scene scene;
    int poz = 100;
    Color farba=Color.RED;
    Random rand = new Random();

    int rychlost= rand.nextInt(20 - 2 + 1) + 2;

    public Balony(Scene scene){
        super(800,1000);
        this.root = root;
        this.scene = scene;
        gc = this.getGraphicsContext2D();
        setLayoutY(100);
        t = new Timeline(new KeyFrame(Duration.millis(100),e->chod()));
        t.setCycleCount(Animation.INDEFINITE);
        t.play();


    }
    private void kresli(){
        farebnost();
        spawn();

            gc.setFill(farba);
            gc.fillOval(poz-40, scene.getHeight()-100, 80, 100);
            gc.setFill(Color.BLACK);
            gc.fillRect(poz, scene.getHeight(), 2, 100);

    }
    public void chod(){
        kresli();
        setLayoutY(getLayoutY()-rychlost);
    }
    public void spawn(){
        int miesto = rand.nextInt((int) (scene.getWidth() - 50 + 1)) + 50;
        poz = miesto;
    }
    public void farebnost(){
        int colour= rand.nextInt(10 - 1 + 1) + 1;
        switch (colour) {
            case 1: farba = Color.RED; break;
            case 2: farba = Color.GREEN; break;
            case 3: farba = Color.BLUE; break;
            case 4: farba = Color.YELLOW; break;
            case 5: farba = Color.ORANGE; break;
            case 6: farba = Color.BROWN; break;
            case 7: farba = Color.YELLOWGREEN; break;
            case 8: farba = Color.ORANGERED; break;
            case 9: farba = Color.HOTPINK; break;
            case 10: farba = Color.VIOLET; break;
        }
    }

This is a simple program I made to explain my problem, when new balloons are spawned their y-position is moving upwards but I want to keep the new instances down + make them move up at different speeds (I do believe this is solvable by moving the entire process of movement into my spawning void so this is not as important).这是我为解释我的问题而制作的一个简单程序,当产生新气球时,它们的 y 位置向上移动,但我想保持新实例向下+使它们以不同的速度向上移动(我相信这可以通过移动来解决进入我的产卵空间的整个过程,所以这并不重要)。

  1. You need to introduce a class for a single balloon which will store it's position, speed and colour.您需要为单个气球引入 class,它将存储它的 position、速度和颜色。 Let's call it Balon .我们称它为Balon
  2. Inside class Balony , you should store List<Balon> balony;在 class Balony里面,你应该存储List<Balon> balony;
  3. At the end of chod() method, you should loop through balony , and update the position of each Balon .chod()方法结束时,您应该遍历balony ,并更新每个 Balon 的Balon balon.setPoz(balon.getPoz() + balon.getRychlost());
  4. Other methods (for example kresli() ) will have to change accordingly, to paint a list of balloons, instead of the single balloon其他方法(例如kresli() )将不得不相应更改,以绘制气球列表,而不是单个气球

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM