My main loop also consists of an AnimationTimer, i just haven’t implemented the FPS cap.
The flickering function is pretty much exactly as you suggested, just with value of 0.25. Just like this:
if(!isInvincible)
{
for(Enemy e : EntityManager.getInstance().getEnemyList())
{
if(this.checkCollision(e))
{
health -= 1;
isInvincible = true;
invincibleCounter = 50;
}
}
}
else
{
invincibleCounter--;
if(invincibleCounter < 1)
{
isInvincible = false;
}
else
{
if(alpha == 1.0 || alpha == 0.0)
{
opacityChange = -opacityChange; //this is 0.25 and defined in the constructor of my player
}
alpha += opacityChange;
}
}
I have an animation class, where every possible state has an Array of Images saved. I load them into a BufferedImage(awt) and by using BufferedImage.getSubImage(…) I convert each frame into an image. The animation class then loops through the current state array and returns the current frame. Then I use GraphicsContext.drawImage(animation.getcurrentframe().getimage(),x,y,width,height) to render it onto the screen. My Player.render(gc) looks something like this:
if(facingLeft && isInvincible)
{
gc.setGlobalAlpha(alpha);
gc.drawImage(animation.getImage(), (int) (tx + x - width / 2), (int) (ty + y - height / 2));
}
The GraphicsContext is passed as an argument and fills the whole screen, therefore my whole screen is “flickering” while the player is invincible.
It might be that I’m confused about what exactly a node is… Why would i use a ImageView if I can display with an javafx.Image object?
Converting the animation.getImage() into an ImageView and then displaying it worked, but the positions of the player before weren’t cleared. So there was a long chain of player sprites everywhere I went. If you could help me fix that it would be perfectly fine :DD
If you need the whole code, I can upload it anytime, just tell me.
Thx and Greez
HalfNOoB