在PlayN中以编程方式淡入图像 [英] Programmatically fading in an image in PlayN
问题描述
我正在使用PlayN进行游戏,在某处我想拍摄图像(当前为PNG格式,已经有8位Alpha),并且我想将图像乘以额外的alpha因子,基于我的代码中的值.
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.
具体来说,我有一张当前生活在ImageLayer中的脸的图片,而我想要的效果是拥有这样的东西:
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);
}
我希望有一些方法可以对GroupLayers和混合模式进行一些棘手的操作,也许可以将图像与CanvasLayer混合在一起,该CanvasLayer涂有由我的代码控制透明度的纯白色矩形,但是对于我来说,这不是最好的方法实现看似很普通的效果.
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.
推荐答案
如果您只想将图像从完全透明变淡为完全不透明,则只需执行以下操作:
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);
}
alpha范围从0(完全透明)到1(完全不透明).
Where alpha ranges from 0 (fully transparent) to 1 (fully opaque).
这篇关于在PlayN中以编程方式淡入图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!