Java-Gaming.org Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (595)
Games in Android Showcase (169)
games submitted by our members
Games in WIP (647)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Java Game APIs & Engines / JavaFX / Re: JavaFX Game tutorials ? on: 2015-03-18 20:37:10
Well I got the time to put toguether a simple barebones example of a JavaFX 8 Game loop
maybe not the best but it get the work done.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
package library;

import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.geometry.Bounds;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class MainLoop extends Application {

   //size in pixels of the main game canvas
   private final int SIZE_W = 800;
   private final int SIZE_H = 600;
   //one second in nanoseconds
   private final long ONE_SECOND = 1000000000;
   //used to store the current time to calculate fps
   private long currentTime = 0;
   //used to store the last time to calculate fps
   private long lastTime = 0;
   //fps counter
   private int fps = 0;
   //acumulated difference between current time and last time
   private double delta = 0;
   //text to display fps
   private Text tFPS;
   //the game loop timer
   private AnimationTimer mainLoop;
   //the shape to test the main loop
   private Polygon star;
   //direction x,y of the star shape
   private int dirx = 1;
   private int diry = 1;
   
   
   @Override
   public void start(Stage stage) throws Exception {
     
      //main game canvas
      Pane root = new Pane();
      root.setPrefSize(SIZE_W, SIZE_H);
      root.setBackground(new Background(new BackgroundFill(Color.BLACK, null, null)));
     
      //initialize all objects of the main loop
      initNodes(root.getChildren());
      //define the main loop
      initMainLoop(root.getChildren());
      //main scene
      Scene scene = new Scene(root);
      //the main window
      stage.setScene(scene);
      stage.setResizable(false);
      stage.setOnCloseRequest(event -> {
         mainLoop.stop();
         System.exit(0);
      });
      stage.show();
      //start the main loop
      mainLoop.start();
     
     
   }
   
   public void initNodes(ObservableList<Node> rootPane){
      tFPS = new Text("FPS : ");
      tFPS.setTranslateX(SIZE_W-180);
      tFPS.setTranslateY(60);
      tFPS.setFill(Color.WHITE);
      tFPS.setFont(new Font(40));
      rootPane.add(tFPS);
      initAnimation(rootPane);
   }
   
   public void initAnimation(ObservableList<Node> rootPane){
      star = new Polygon();
      Double[] starCoord = new Double[]{
            35.0, 120.5, 37.9, 129.1, 46.9, 129.1, 39.7, 134.5, 42.3,
            143.1, 35.0 , 139.0, 27.7, 143.1, 30.3, 134.5, 23.1, 129.1,
            32.1,129.1};
      star.getPoints().addAll(starCoord);
      star.setTranslateX(100);
      star.setTranslateY(100);
      star.setFill(Color.RED);
      rootPane.add(star);

   }
   
   public void initMainLoop(ObservableList<Node> rootPane){
      lastTime = System.nanoTime();
      mainLoop = new AnimationTimer() {
         
         @Override
         public void handle(long now) {
            currentTime = now;
            fps++;
            delta += currentTime-lastTime;
           
            updateAnimation(rootPane);
           
            if(delta > ONE_SECOND){
               tFPS.setText("FPS : "+fps);
               delta -= ONE_SECOND;
               fps = 0;
            }
           
            lastTime = currentTime;
         }
      };
   }
   
   public void updateAnimation(ObservableList<Node> rootPane){
      Bounds starbounds = star.getBoundsInParent();
     
      double posx = star.getTranslateX();
      double posy = star.getTranslateY();

      if((starbounds.getMaxX() >= SIZE_W) || (starbounds.getMinX() <= 0.0)){
         dirx = -dirx;
         star.setScaleX(1);
         star.setScaleY(1);
      }
      if((starbounds.getMaxY() >= SIZE_H) || (starbounds.getMinY() <= 0.0)){
         diry = -diry;
         star.setScaleX(1);
         star.setScaleY(1);
      }
      star.setTranslateX(posx+(3*dirx));
      star.setTranslateY(posy+(3*diry));
      star.setScaleX(star.getScaleX()+0.1);
      star.setScaleY(star.getScaleY()+0.1);

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

}


more info here:

http://blog.netopyr.com/2012/06/14/using-the-javafx-animationtimer/#comments

hope somebody found this usefull.
2  Java Game APIs & Engines / JavaFX / JavaFX Game tutorials ? on: 2015-03-11 18:09:10
I am looking for some JavaFX game related tutorials, books or videos

I Have found these so far

http://www.hascode.com/2013/06/creating-and-packaging-a-game-in-java-fx-2-2/
https://carlfx.wordpress.com/2012/03/29/javafx-2-gametutorial-part-1/

and here some videos, didn't see it yet

https://www.youtube.com/channel/UCmjXvUa36DjqCJ1zktXVbUA/videos

do anyone have some more information on JavaFX games ?

I know JavaFX is not the best suited tool for games better use x, y or z, blah, blah, blah Smiley
I just want to give a try for myself.

Thanks

3  Discussions / Miscellaneous Topics / Re: Good language for prototyping on: 2015-03-02 04:22:54
Java is pretty easy, more easy than that you can try Actionscript with Flashpunk or Flixel and the easiest PureBasic, BlitzMax or MonkeyX
4  Java Game APIs & Engines / Java 2D / Re: Simple Bump Mapping in Java 2D + Download on: 2015-02-07 13:35:28
This is really interesting thanks
5  Games Center / WIP games, tools & toy projects / Look my project on Indiegogo: Paperdoll Sprites on: 2012-12-23 22:06:50
Paperdoll Sprites will help you to assemble, organize and visualize your sprites, export sprite-sheets (combined and with clothes) and gif's.
I'm planning to add editing features with layers in the next releases but on the initial release of the application editing is not planned.

http://igg.me/p/291132

Pages: [1]
 
KaiHH (11 views)
2015-07-07 11:39:58

deepthought (54 views)
2015-06-30 15:39:44

deepthought (57 views)
2015-06-30 15:39:09

deepthought (66 views)
2015-06-30 15:36:52

Za\'Anzabar (44 views)
2015-06-29 05:44:54

TritonDreyja (50 views)
2015-06-24 17:10:40

CopyableCougar4 (52 views)
2015-06-23 00:34:45

BurntPizza (58 views)
2015-06-21 20:36:46

cookiecompiler (99 views)
2015-06-11 15:42:53

cookiecompiler (58 views)
2015-06-11 15:41:14
How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!