Programmatically fading in an image in PlayN

321 views Asked by At

I'm working on a game using PlayN, and there's a bit where I would like to take an image (which is currently in PNG format, with 8-bit alpha already) and I would like to multiply the image by an additional alpha factor, based on a value from my code.

Specifically, I have a picture of a face that currently lives in an ImageLayer, and the effect that I would like would be to have something like this:

void init() {
  faceImage = assetManager().getImage("images/face.png");
  graphics().rootLayer().add(faceImage);
}

void update(float deltaMilliseconds) {
  // start at fully transparent, fade to fully opaque 
  float transparency = calcTransparency(deltaMilliseconds);
  faceImage.setTransparency(transparency);
}

I expect that there's some way to do some trickiness with GroupLayers and blend modes, perhaps blending the image with a CanvasLayer painted with a solid white rectangle with transparency controlled by my code, but it's not obvious to me if that's the best way to achieve what seems like a pretty common effect.

1

There are 1 answers

1
samskivert On BEST ANSWER

If you just want to fade the image in from fully-transparent to fully-opaque, then just do the following:

ImageLayer faceLayer;
void init() {
  Image faceImage = assetManager().getImage("images/face.png");
  faceLayer = graphics().createImageLayer(faceImage);
  graphics().rootLayer().add(faceLayer);
}

void update(float delta) {
  float alpha = calcAlpha(delta);
  faceLayer.setAlpha(alpha);
}

Where alpha ranges from 0 (fully transparent) to 1 (fully opaque).