How does one repaint a canvas without repainting a scene using javafx? - java

I am trying to make an rpg style game in javafx, which means that I need to repaint the screen every time the player moves or does an action. My game currently works so that it creates a new scene every time the player does an action, but this has proved to make the game very slow, as well as cause a host of other issues (music restarting). Would there be a way to update the canvas that I put on the screen and have that show to the scene but not recreate the whole scene?
I have tried to create a new canvas but the scene does not recognize the change in the canvas.
The system is a bit convoluted to show the entire path, but if anyone has questions regarding a specific aspect of the structure I would be happy to answer.
Basically, the scene has many elements on it, one of which is the canvas that is fetched from another class that has a method called getCavnas that returns the canvas.

Related

How do I add something to be executed after ALL currently running animations are complete in JavaFX?

I subclassed GridPane to display tiles on my game (2048), and since animations don't work well on a GridPane, whenever the player shifts the tiles, I have a transparent Pane overlay that sits on top of the Gridpane that I shift a tile to and is the surface that my animations are run on. My view is very "dumb" and knows nothing about the game state and simply runs animations whenever told.
I want to be able to update a few things on the Pane and the GridPane after all of the animations are complete (which it currently isn't able to do since when the animation is run it doesn't know if it's a first, intermediate, or last animation for the current game move). This is what I use for updates after a single animation is done:
transition.setOnFinished(event -> {
// executed after a single animation
}
However, I want to be able to do something after all active animations are complete. Is this possible? If not what would be the best way around this?

How do I create a view following the player in a JavaFX game?

I'm trying to make my first game in JavaFX, where a player object (not a Node) will move inside a level. However, the full level shouldn't be visible inside the window, only a part of it surrounding the player should be. I've searched quite a bit for a solution to this that works with the way I've done my project, but haven't been successful. The following code describes how I've set up my GUI:
private VBox appRoot;
private Pane gameRoot;
private Canvas canvas;
private GraphicsContext gc;
private HBox uiRoot;
This is the different panes I use. The appRoot contains both the gameRoot and the uiRoot, in that order. The gameRoot contains the canvas. Everything inside the level, including the level itself, is drawn onto the canvas, like this:
canvas = new Canvas(ROOM_WIDTH, ROOM_HEIGHT);
gc = canvas.getGraphicsContext2D();
createLevel(ROOM_WIDTH, ROOM_HEIGHT, CELL_WIDTH, CELL_HEIGHT);
gc.scale(2, 2);
drawLevel(gc, ROOM_WIDTH, ROOM_HEIGHT, CELL_WIDTH, CELL_HEIGHT);
player.render(gc);
createLevel and drawLevel just creates a level and draws squares where different blocks should be. The player object's render method draws the player at its x- and y-coordinates. As you can see I've scaled the canvas, so as of now the entire level isn't shown when the game is started.
The problem is that I can't make it so that what is shown in the window is the things surrounding the player object. I've seen some code utilizing the methods setLayoutX and setLayoutY, but I've been unable to reproduce this so that it works.
I would be very grateful if anybody could help me out with this, and hopefully without changing too much of how the drawing is done.
Thanks! :)
Also, here are two pictures showing how it looks; the first is when the player is not inside the current view, and the second is with the player in the view.
Image without player in view
Image with player in view
Do not us the Canvas directly to display the level. Instead you can use an image that represents your level and then have the image in an ImageView. On that ImageView you can define a viewport of the area of the image that is visible.
To get the image you can either use your Canvas node and take a snapshot (See this question). This is probably the easiest way with what you have. Or you can forego the Canvas altogether and draw the image. If the levels are predefined (and not generated at runtime), this seems the best approach, then you can supply the images as resources along with the code.

Stop clearing graphics on repaint();?

I'm creating a simple Breakout game and I've encountered a couple of problems.
First off, I need to render the bricks, the paddle, etc... I need to generally render everything on the screen that's needed. However, I don't want, for example, the bricks to continuously render; you can see where the render is occurring as it happens. There's a flash of white.
So, my question is: is it possible to render only certain objects, or more specifically, only render what needs to be rendered?
Every time I call repaint(); I don't want the whole screen to clear and repaint.

How to do a fixed sprite (that don't move with my camera) in libgdx?

How can i do a fixed sprite that don't move with my camera (for example: a life bar in the left up corner)?
I have already tried to do this in the hard way and i would like to see if there is a simple way to do this.
Is there a simple way to do that or i need to do this in relative to the player position?
And i am sorry for my broken english, I would be very grateful to those who answer me.
thankyou.
Usually for the task of UI you would use an extra scene2d Stage and implement custom subclasses Actor which you add to the Stage and set their fixed position via Actor.setPosition(). There are already several predefined UI elements in the scene2d.ui package. To simulate a healthbar you could either use a ProgressBar, or you implement your own HealthBar extends Actor, which renders a Sprite of your choice.
The stage is stationary by default and has its own Camera. As long as you do not manipulate that camera anyhow, you can just draw the stage with all elements via stage.draw() and it will always remain at the same place (what you would expect from an ingame HUD).

Android Drawing on Multiple Canvas Multiple Activities

Here is my problem:
I currently have on Canvas in my main game activity that is constantly being drawn with OnDraw(). This is a board game with two players. When a player's turn begins, there are many options that the player can do, and I would like all of this to be put in a different class.
Also, this new class must draw NEW animations on top of the current canvas. These animations will came from attacks (I haven't even looked into animations yet). So basically I would like another canvas on top of the main one.
Furthermore, the OnDraw function in the main activity must PAUSE and wait for the other class with the canvas on top to complete.
Let me try and summarize this: I have a main game class with one canvas. This class handles the players' turns and setting up the game. It also draws the playing field. I need another class, when it is a player's turn, to draw animations and other things on top of the current canvas.
Can anybody help me with this?
NOTE : I looked into Fragments and FragmentManager, but it seems I can only use XML Views with that and not RenderViews.
Thank you!
You might want to consider using multiple bitmaps as buffers and then just add them on top of each other before rendering to the canvas.
There is a nice example in the "samples" pack that comes with the sdk that shows of how you can merge bitmaps on top of each other.
Let me know if you want to try this out and need some more pointers on how to do it :)

Categories

Resources