Programmatically fading in an image in PlayN

一曲冷凌霜 提交于 2020-01-02 19:28:23

问题


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:


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).



来源:https://stackoverflow.com/questions/8890321/programmatically-fading-in-an-image-in-playn

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!