| 网站首页 | 业界新闻 | 群组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 编程论坛
共有 276 人关注过本帖
标题:javaFX的timeline时间轴循环??贪吃蛇
只看楼主 加入收藏
渐渐鱼
Rank: 1
等 级:新手上路
帖 子:74
专家分:0
注 册:2018-5-11
结帖率:100%
  已结贴   问题点数:20  回复次数:4   
javaFX的timeline时间轴循环??贪吃蛇
程序代码:
package application;
   
import java.util.Iterator;
import java.util.LinkedList;

import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.stage.Stage;
import javafx.util.Duration;
import javafx.scene.Group;

import javafx.scene.Scene;

import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
import javafx.scene.shape.Circle;



public class Main extends Application
{
   
    @Override
    public void start(Stage stage)
    {
        
        
            Group g = new Group();
            
            Scene scene = new Scene(g,600,600);
            
            //画蛇
            LinkedList<Circle> list = new LinkedList<>();
            for(int i=0;i<5;i++)
            {
                Circle cir = new Circle();
                cir.setCenterX(275-35*i);
                cir.setCenterY(200);
                cir.setRadius(20);
                cir.setFill(Color.rgb(0, 255 ,127));
                list.add(cir);
               
            }
            
                Arc arc = new Arc();
                arc.setFill(Color.rgb(0, 255 ,127) );
                arc.setCenterX(310);
                arc.setCenterY(200);
                arc.setRadiusX(20);
                arc.setRadiusY(20);
                arc.setStartAngle(45);
                arc.setLength(270);
                arc.setType(ArcType.ROUND);
               
                class Move
                {
                    public void A()
                    {

                        System.out.println("A");
                        Timeline timeline=new Timeline();
                        KeyValue ARCxValue = new KeyValue(arc.centerXProperty(),arc.getCenterX()-50);
                        KeyFrame ARCkeyFrame=new KeyFrame(Duration.millis(1000), ARCxValue);
                        timeline.getKeyFrames().add(ARCkeyFrame);
                        
                        //body
                        Iterator<Circle> iterator = list.iterator();
                        Circle cir1 = iterator.next();
                        KeyValue cir1xValue = new KeyValue(cir1.centerXProperty(),arc.getCenterX());
                        KeyValue cir1yValue = new KeyValue(cir1.centerYProperty(),arc.getCenterY());
                        KeyFrame keyFrame1=new KeyFrame(Duration.millis(1000), cir1xValue,cir1yValue);
                        timeline.getKeyFrames().add(keyFrame1);
                        
                        
                        Circle cir3 = iterator.next();
                        KeyValue cir3xValue = new KeyValue(cir3.centerXProperty(),cir1.getCenterX());
                        KeyValue cir3yValue = new KeyValue(cir3.centerYProperty(),cir1.getCenterY());
                        KeyFrame keyFrame2=new KeyFrame(Duration.millis(1000), cir3xValue,cir3yValue);
                        timeline.getKeyFrames().add(keyFrame2);
                        
                        Circle cir4 = iterator.next();
                        KeyValue cir4xValue = new KeyValue(cir4.centerXProperty(),cir3.getCenterX());
                        KeyValue cir4yValue = new KeyValue(cir4.centerYProperty(),cir3.getCenterY());
                        KeyFrame keyFrame3=new KeyFrame(Duration.millis(1000), cir4xValue,cir4yValue);
                        timeline.getKeyFrames().add(keyFrame3);
                        
                        Circle cir5 = iterator.next();
                        KeyValue cir5xValue = new KeyValue(cir5.centerXProperty(),cir4.getCenterX());
                        KeyValue cir5yValue = new KeyValue(cir5.centerYProperty(),cir4.getCenterY());
                        KeyFrame keyFrame4=new KeyFrame(Duration.millis(1000), cir5xValue,cir5yValue);
                        timeline.getKeyFrames().add(keyFrame4);
                        
                        Circle cir6 = iterator.next();
                        KeyValue cir6xValue = new KeyValue(cir6.centerXProperty(),cir5.getCenterX());
                        KeyValue cir6yValue = new KeyValue(cir6.centerYProperty(),cir5.getCenterY());
                        KeyFrame keyFrame5=new KeyFrame(Duration.millis(1000), cir6xValue,cir6yValue);
                        timeline.getKeyFrames().add(keyFrame5);
                        
                        timeline.play();
                    }
                    public void W()
                    {
                        
                        System.out.println("W");
                        Timeline timeline=new Timeline();
                        //head
                        KeyValue ARCyValue = new KeyValue(arc.centerYProperty(),arc.getCenterY()-50);
                        KeyFrame ARCkeyFrame=new KeyFrame(Duration.millis(1000), ARCyValue);
                        timeline.getKeyFrames().add(ARCkeyFrame);
                        
                        //body
                        Iterator<Circle> iterator = list.iterator();
                        Circle cir1 = iterator.next();
                        KeyValue cir1xValue = new KeyValue(cir1.centerXProperty(),arc.getCenterX());
                        KeyValue cir1yValue = new KeyValue(cir1.centerYProperty(),arc.getCenterY());
                        KeyFrame keyFrame1=new KeyFrame(Duration.millis(1000), cir1xValue,cir1yValue);
                        timeline.getKeyFrames().add(keyFrame1);
                        
                        
                        Circle cir3 = iterator.next();
                        KeyValue cir3xValue = new KeyValue(cir3.centerXProperty(),cir1.getCenterX());
                        KeyValue cir3yValue = new KeyValue(cir3.centerYProperty(),cir1.getCenterY());
                        KeyFrame keyFrame2=new KeyFrame(Duration.millis(1000), cir3xValue,cir3yValue);
                        timeline.getKeyFrames().add(keyFrame2);
                        
                        Circle cir4 = iterator.next();
                        KeyValue cir4xValue = new KeyValue(cir4.centerXProperty(),cir3.getCenterX());
                        KeyValue cir4yValue = new KeyValue(cir4.centerYProperty(),cir3.getCenterY());
                        KeyFrame keyFrame3=new KeyFrame(Duration.millis(1000), cir4xValue,cir4yValue);
                        timeline.getKeyFrames().add(keyFrame3);
                        
                        Circle cir5 = iterator.next();
                        KeyValue cir5xValue = new KeyValue(cir5.centerXProperty(),cir4.getCenterX());
                        KeyValue cir5yValue = new KeyValue(cir5.centerYProperty(),cir4.getCenterY());
                        KeyFrame keyFrame4=new KeyFrame(Duration.millis(1000), cir5xValue,cir5yValue);
                        timeline.getKeyFrames().add(keyFrame4);
                        
                        Circle cir6 = iterator.next();
                        KeyValue cir6xValue = new KeyValue(cir6.centerXProperty(),cir5.getCenterX());
                        KeyValue cir6yValue = new KeyValue(cir6.centerYProperty(),cir5.getCenterY());
                        KeyFrame keyFrame5=new KeyFrame(Duration.millis(1000), cir6xValue,cir6yValue);
                        timeline.getKeyFrames().add(keyFrame5);
                        
               
        
                        timeline.play();
                        
                        
                    }
                    public void S()
                    {
                        System.out.println("S");
                        Timeline timeline=new Timeline();
                        KeyValue ARCyValue = new KeyValue(arc.centerYProperty(),arc.getCenterY()+50);
                        KeyFrame ARCkeyFrame=new KeyFrame(Duration.millis(1000), ARCyValue);
                        timeline.getKeyFrames().add(ARCkeyFrame);
                        
                        //body
                        Iterator<Circle> iterator = list.iterator();
                        Circle cir1 = iterator.next();
                        KeyValue cir1xValue = new KeyValue(cir1.centerXProperty(),arc.getCenterX());
                        KeyValue cir1yValue = new KeyValue(cir1.centerYProperty(),arc.getCenterY());
                        KeyFrame keyFrame1=new KeyFrame(Duration.millis(1000), cir1xValue,cir1yValue);
                        timeline.getKeyFrames().add(keyFrame1);
                        
                        
                        Circle cir3 = iterator.next();
                        KeyValue cir3xValue = new KeyValue(cir3.centerXProperty(),cir1.getCenterX());
                        KeyValue cir3yValue = new KeyValue(cir3.centerYProperty(),cir1.getCenterY());
                        KeyFrame keyFrame2=new KeyFrame(Duration.millis(1000), cir3xValue,cir3yValue);
                        timeline.getKeyFrames().add(keyFrame2);
                        
                        Circle cir4 = iterator.next();
                        KeyValue cir4xValue = new KeyValue(cir4.centerXProperty(),cir3.getCenterX());
                        KeyValue cir4yValue = new KeyValue(cir4.centerYProperty(),cir3.getCenterY());
                        KeyFrame keyFrame3=new KeyFrame(Duration.millis(1000), cir4xValue,cir4yValue);
                        timeline.getKeyFrames().add(keyFrame3);
                        
                        Circle cir5 = iterator.next();
                        KeyValue cir5xValue = new KeyValue(cir5.centerXProperty(),cir4.getCenterX());
                        KeyValue cir5yValue = new KeyValue(cir5.centerYProperty(),cir4.getCenterY());
                        KeyFrame keyFrame4=new KeyFrame(Duration.millis(1000), cir5xValue,cir5yValue);
                        timeline.getKeyFrames().add(keyFrame4);
                        
                        Circle cir6 = iterator.next();
                        KeyValue cir6xValue = new KeyValue(cir6.centerXProperty(),cir5.getCenterX());
                        KeyValue cir6yValue = new KeyValue(cir6.centerYProperty(),cir5.getCenterY());
                        KeyFrame keyFrame5=new KeyFrame(Duration.millis(1000), cir6xValue,cir6yValue);
                        timeline.getKeyFrames().add(keyFrame5);
                        
                        
                        
                        timeline.play();

                    }
                    public void D()
                    {
                        System.out.println("D");
                        Timeline timeline=new Timeline();
                        KeyValue ARCxValue = new KeyValue(arc.centerXProperty(),arc.getCenterX()+50);
                        KeyFrame ARCkeyFrame=new KeyFrame(Duration.millis(1000), ARCxValue);
                        timeline.getKeyFrames().add(ARCkeyFrame);
                        
                        //body
                        Iterator<Circle> iterator = list.iterator();
                        Circle cir1 = iterator.next();
                        KeyValue cir1xValue = new KeyValue(cir1.centerXProperty(),arc.getCenterX());
                        KeyValue cir1yValue = new KeyValue(cir1.centerYProperty(),arc.getCenterY());
                        KeyFrame keyFrame1=new KeyFrame(Duration.millis(1000), cir1xValue,cir1yValue);
                        timeline.getKeyFrames().add(keyFrame1);
                        
                        
                        Circle cir3 = iterator.next();
                        KeyValue cir3xValue = new KeyValue(cir3.centerXProperty(),cir1.getCenterX());
                        KeyValue cir3yValue = new KeyValue(cir3.centerYProperty(),cir1.getCenterY());
                        KeyFrame keyFrame2=new KeyFrame(Duration.millis(1000), cir3xValue,cir3yValue);
                        timeline.getKeyFrames().add(keyFrame2);
                        
                        Circle cir4 = iterator.next();
                        KeyValue cir4xValue = new KeyValue(cir4.centerXProperty(),cir3.getCenterX());
                        KeyValue cir4yValue = new KeyValue(cir4.centerYProperty(),cir3.getCenterY());
                        KeyFrame keyFrame3=new KeyFrame(Duration.millis(1000), cir4xValue,cir4yValue);
                        timeline.getKeyFrames().add(keyFrame3);
                        
                        Circle cir5 = iterator.next();
                        KeyValue cir5xValue = new KeyValue(cir5.centerXProperty(),cir4.getCenterX());
                        KeyValue cir5yValue = new KeyValue(cir5.centerYProperty(),cir4.getCenterY());
                        KeyFrame keyFrame4=new KeyFrame(Duration.millis(1000), cir5xValue,cir5yValue);
                        timeline.getKeyFrames().add(keyFrame4);
                        
                        Circle cir6 = iterator.next();
                        KeyValue cir6xValue = new KeyValue(cir6.centerXProperty(),cir5.getCenterX());
                        KeyValue cir6yValue = new KeyValue(cir6.centerYProperty(),cir5.getCenterY());
                        KeyFrame keyFrame5=new KeyFrame(Duration.millis(1000), cir6xValue,cir6yValue);
                        timeline.getKeyFrames().add(keyFrame5);
                        
                        timeline.play();
                    }
                }
               
                //监听键盘事件
            scene.setOnKeyReleased(new EventHandler<KeyEvent>()
            {
               
                public void handle(KeyEvent event)
                {
                     Move move = new Move();
                     
                    switch(event.getCode())
                    {
                    case W:
                    {        
                            move.W();
                            break;
                    }
                    case S:
                    {
                        move.S();
                        break;
                    }
                        
                    case A:
                        
                    {
                        move.A();
                    break;
                        
                    }
                    case D:
                    {
                        move.D();
                        break;
                    }
                    }
                    }
                    
                }
              
               
            
               
            
            );
            
            
            
   
            g.getChildren().addAll(list);
            g.getChildren().add(arc);
            
           
            stage.setScene(scene);
            stage.show();
            
            
            
                        
    }
   
   
    public static void main(String[] args) {
        launch(args);
    }
}


做的一个贪吃蛇的小游戏,设置wasd四个按键只能单步实现移动,加while循环似乎不行

求问:怎么实现按下一个键可以一直执行单步的循环移动???
2018-10-01 19:28
林月儿
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:湖南
等 级:版主
威 望:104
帖 子:1668
专家分:7280
注 册:2015-3-19
  得分:20 
程序代码:
package application;
   
import java.util.LinkedList;

import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;



public class Main extends Application
{
   
    @Override
    public void start(Stage stage)
    {
        Group g = new Group();
        Scene scene = new Scene(g,600,600);
        //画蛇
        LinkedList<Circle> list = new LinkedList<>();
        for(int i=0;i<5;i++)
        {
            Circle cir = new Circle();
            cir.setCenterX(275-35*i);
            cir.setCenterY(200);
            cir.setRadius(20);
            cir.setFill(Color.rgb(0, 255 ,127));
            list.add(cir);
        }
   
        Arc arc = new Arc();
        arc.setFill(Color.rgb(0, 255 ,127) );
        arc.setCenterX(310);
        arc.setCenterY(200);
        arc.setRadiusX(20);
        arc.setRadiusY(20);
        arc.setStartAngle(45);
        arc.setLength(270);
        arc.setType(ArcType.ROUND);
        
        class Move
        {
            public void moveObj(DoubleProperty dire, double arcLoc)
            {
                Timeline timeline = new Timeline();
                KeyValue ARCxValue = new KeyValue(dire, arcLoc);
                KeyFrame ARCkeyFrame=new KeyFrame(Duration.millis(1000), ARCxValue);
                timeline.getKeyFrames().add(ARCkeyFrame);

                double prevX = arc.getCenterX();
                double prevY = arc.getCenterY();
                boolean isFirst = true;
                //body
                for(int i=0; i<list.size(); i++) {
                    if(!isFirst) {
                        prevX = list.get(i-1).getCenterX();
                        prevY = list.get(i-1).getCenterY();
                    }
                    KeyValue cirxValue = new KeyValue(list.get(i).centerXProperty(), prevX);
                    KeyValue ciryValue = new KeyValue(list.get(i).centerYProperty(), prevY);
                    KeyFrame keyFrame=new KeyFrame(Duration.millis(1000), cirxValue,ciryValue);
                    timeline.getKeyFrames().add(keyFrame);
                    isFirst = false;
                }
               
                timeline.play();
            }
        }        
        //监听键盘事件
        scene.setOnKeyReleased(new EventHandler<KeyEvent>()
        {
            public void handle(KeyEvent event)
            {
                Move move = new Move();
                DoubleProperty dire = null;
                double arcLoc = 0;
                switch(event.getText())
                {
                case "W":
                case "w":                    
                    dire = arc.centerYProperty();
                    arcLoc = arc.getCenterY() - 50;
                    break;
                case "S":
                case "s":
                    dire = arc.centerYProperty();
                    arcLoc = arc.getCenterY() + 50;
                    break;
                case "A":
                case "a":
                    dire = arc.centerXProperty();
                    arcLoc = arc.getCenterX() - 50;
                    break;
                case "D":
                case "d":
                    dire = arc.centerXProperty();
                    arcLoc = arc.getCenterX() + 50;
                    break;
                }
                if(dire != null)
                move.moveObj(dire, arcLoc);
            }
        });
        
        g.getChildren().addAll(list);
        g.getChildren().add(arc);
        stage.setScene(scene);
        stage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }
}

wechat    PrinceThumb
2018-10-02 07:56
林月儿
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:湖南
等 级:版主
威 望:104
帖 子:1668
专家分:7280
注 册:2015-3-19
  得分:0 
实现按下一个键可以一直执行单步的循环移动的话

程序代码:
package application;
   
import java.util.LinkedList;
import java.util.Timer;
import java.util.TimerTask;

import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;



public class Main extends Application
{
   
    @Override
    public void start(Stage stage) throws Exception
    {
        Group g = new Group();
        Scene scene = new Scene(g, 600, 600);
        //画蛇
        LinkedList<Circle> list = new LinkedList<>();
        for(int i = 0; i < 5; i++)
        {
            Circle cir = new Circle();
            cir.setCenterX(275 - 35 * i);
            cir.setCenterY(200);
            cir.setRadius(20);
            cir.setFill(Color.rgb(0, 255 ,127));
            list.add(cir);
        }
   
        Arc arc = new Arc();
        arc.setFill(Color.rgb(0, 255 ,127) );
        arc.setCenterX(310);
        arc.setCenterY(200);
        arc.setRadiusX(20);
        arc.setRadiusY(20);
        arc.setStartAngle(45);
        arc.setLength(270);
        arc.setType(ArcType.ROUND);
        
        class Move
        {
            public void moveObj(DoubleProperty dire, double arcLoc)
            {
                Timeline timeline = new Timeline();
                KeyValue arcValue = new KeyValue(dire, arcLoc);
                KeyFrame ARCkeyFrame = new KeyFrame(Duration.millis(1000), arcValue);
                timeline.getKeyFrames().add(ARCkeyFrame);

                double prevX = arc.getCenterX();
                double prevY = arc.getCenterY();
                boolean isFirst = true;
                //body
                for(int i = 0; i < list.size(); i++) {
                    if(!isFirst) {
                        prevX = list.get(i-1).getCenterX();
                        prevY = list.get(i-1).getCenterY();
                    }
                    KeyValue cirxValue = new KeyValue(list.get(i).centerXProperty(), prevX);
                    KeyValue ciryValue = new KeyValue(list.get(i).centerYProperty(), prevY);
                    KeyFrame keyFrame = new KeyFrame(Duration.millis(1000), cirxValue, ciryValue);
                    timeline.getKeyFrames().add(keyFrame);
                    isFirst = false;
                }
               
                timeline.play();
            }
            
        }        
        //监听键盘事件
        scene.setOnKeyReleased(new EventHandler<KeyEvent>()
        {
            public void handle(KeyEvent event)
            {
                DoubleProperty dire = null;
                double arcLoc = 0;
                switch(event.getText())
                {
                case "W":
                case "w":
                    dire = arc.centerYProperty();
                    arcLoc = arc.getCenterY() - 50;
                    break;
                case "S":
                case "s":
                    dire = arc.centerYProperty();
                    arcLoc = arc.getCenterY() + 50;
                    break;
                case "A":
                case "a":
                    dire = arc.centerXProperty();
                    arcLoc = arc.getCenterX() - 50;
                    break;
                case "D":
                case "d":
                    dire = arc.centerXProperty();
                    arcLoc = arc.getCenterX() + 50;
                    break;
                }
                Move move = new Move();
                if(dire != null) {
                    keyCode = event.getText();
                    move.moveObj(dire, arcLoc);
                }
            }
        });
        
        g.getChildren().addAll(list);
        g.getChildren().add(arc);
        stage.setScene(scene);
        stage.show();
        new Timer().schedule(new TimerTask() {
            
            @Override
            public  void run() {
                if(keyCode == null) return;
                DoubleProperty dire = null;
                double arcLoc = 0;
                switch(keyCode)
                {
                case "W":
                case "w":
                    dire = arc.centerYProperty();
                    arcLoc = arc.getCenterY() - 50;
                    break;
                case "S":
                case "s":
                    dire = arc.centerYProperty();
                    arcLoc = arc.getCenterY() + 50;
                    break;
                case "A":
                case "a":
                    dire = arc.centerXProperty();
                    arcLoc = arc.getCenterX() - 50;
                    break;
                case "D":
                case "d":
                    dire = arc.centerXProperty();
                    arcLoc = arc.getCenterX() + 50;
                    break;
                }
                Move move = new Move();
                if(dire != null) {
                    move.moveObj(dire, arcLoc);
                }
            }
        }, 10, 1000);
    }
    static String keyCode = null;
    public static void main(String[] args) {
        launch(args);
    }
}

wechat    PrinceThumb
2018-10-02 08:34
渐渐鱼
Rank: 1
等 级:新手上路
帖 子:74
专家分:0
注 册:2018-5-11
  得分:0 
回复 3楼 林月儿
月姐姐,你的循环优化做的挺好的。谢谢啦!!
不过这个地方没太懂?

new Timer().schedule(new TimerTask() {
            
            @Override
            public  void run() {
                if(keyCode == null) return;
                DoubleProperty dire = null;
                double arcLoc = 0;
                switch(keyCode)
                {
                case "W":
                case "w":
                    dire = arc.centerYProperty();
                    arcLoc = arc.getCenterY() - 50;
                    break;
                case "S":
                case "s":
                    dire = arc.centerYProperty();
                    arcLoc = arc.getCenterY() + 50;
                    break;
                case "A":
                case "a":
                    dire = arc.centerXProperty();
                    arcLoc = arc.getCenterX() - 50;
                    break;
                case "D":
                case "d":
                    dire = arc.centerXProperty();
                    arcLoc = arc.getCenterX() + 50;
                    break;
                }
                Move move = new Move();
                if(dire != null) {
                    move.moveObj(dire, arcLoc);
                }
            }
        }, 10, 1000);
    }


这个是控制什么的??
2018-10-03 12:30
林月儿
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:湖南
等 级:版主
威 望:104
帖 子:1668
专家分:7280
注 册:2015-3-19
  得分:0 
定时任务呀
new Timer().schedule(new TimerTask() {
   @Override
            public  void run() {
                   .......
            }
 }, 10, 1000);
意思是10毫秒之后开始,每隔一秒执行一次run方法,这个可以查一下相关接口说明

当前需求是根据最近一次的按键的值进行定时任务处理,也就是根据'keyCode持续执行按键对应的移动操作

[此贴子已经被作者于2018-10-3 22:25编辑过]


wechat    PrinceThumb
2018-10-03 22:23







关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.036578 second(s), 8 queries.
Copyright©2004-2018, BCCN.NET, All Rights Reserved