I'm new to JavaFX and am trying to write a game where an animated 2D character walks across the screen (for example like the original Legend of Zelda game). I had done this in Swing, by creating my own Sprite class and overriding the paintComponent() method in Swing, and putting my own g2d.drawImage(...); call in there, where it would copy the correct subframe from a Sprite to the correct x,y destination in a JPanel, thus achieving the movement of an animated (walking) 2D image across the screen.
How can I do this in JavaFX? I found this excellent example on how to create a sprite: http://blog.netopyr.com/2012/03/09/creating-a-sprite-animation-with-javafx/ in JavaFX, but how do I do the PaintComponent and drawImage(...) parts? Does JavaFX have an equivalent method to PaintComponent where everything is redrawn? I tried calling the primaryStage.show(); method, but that didn't work.
I guess I'm not clear on how exactly to set up my master game loop, where a refresh is triggered and an image is painted on the scene at a different x,y location?
What's the JavaFX equivalent to the drawImage(...) method? this method allows me to copy a part of a source image into a destination scene at any given x,y offset. That is how I achieved moving an animated sprite on screen in Swing.
Best Regards,
Zareh
Your question is too much to be covered on SO. However I created a simple "engine" for you to start with. It's general purpose, so also suited for your needs.
The main class with the game loop where the game is loaded, input is checked, sprites are moved, collision is checked, score is updated etc
A base class for sprites which includes common methods like movement, etc
Subclasses of the sprite class like player ...
... and enemies
You also need an input mechanism to control the player sprite
And some global settings
You can use any image for the sprites. I took mine from Wikipedia:
player.png
enemy.png
If you put it all into a game package, you can start the Game.java. It'll give you a controllable smiley with zombie smilies that scroll down. You have to evade them. I left the images without transparency for you so that you'll notice that I use a simple rectangle collision detection. You'll probably go for a per-pixel-collision detection.
It looks like this:
I don't claim this to be the solution, it's just a solution. For example you'd have to limit the animation timer. Or you may want to set the movement per seconds instead of per frame, etc.
If you want more information, feel free to check out my blog in which I discover myself How to create a 2D Shoot'em'up with JavaFX. There you'll also find e. g. how to add animated sprites (I also learned that from the link you posted), a scrolling background, a cloud layer on top of the other layers, etc. I hope it helps you.