我在为子实体设置 Alpha 时遇到问题。 我创建一个 Rectangle 实体并将 Text 实体附加到矩形,如下所示:
m_background = new Rectangle(0.0f, 0.0f, m_activity.getCamera().getWidth(), m_activity.getCamera().getHeight(), m_activity.getVertexBufferObjectManager());
m_background.setColor(0.0f, 0.0f, 0.0f);
FontFactory.setAssetBasePath("font/");
final ITexture fontTexture = new BitmapTextureAtlas(m_activity.getTextureManager(), 256, 256, TextureOptions.BILINEAR);
this.m_Font = FontFactory.createFromAsset(m_activity.getFontManager(), fontTexture, m_activity.getAssets(), "Droid.ttf", 48, true, android.graphics.Color.WHITE);
this.m_Font.load();
Text text = new Text(100, 300, this.m_Font, "LoadingScreen", new TextOptions(HorizontalAlign.RIGHT), m_activity.getVertexBufferObjectManager());
m_background.attachChild(text);
现在我想将这个背景实体的 Alpha 设置为 0:
m_background.setAlpha(0.0f);
我的印象是子实体也会受到影响,是不是这样?除了直接在子实体上设置 alpha 之外,我怎么能这样做呢?有没有更好的办法?
提前致谢, 泽德
最佳答案
很遗憾,子实体仅受其父实体(已附加)位置的影响。如果你想要一个带有文本的矩形并对它们都应用 alpha 修改,你要么将 alpha 也应用到文本,要么你想更频繁地使用这个概念并做除了改变 alpha channel 之外的其他事情,你创建自己的类。
可能是这样的:
public class Background extends Entity {
private Text text;
public Background(float x, float y, float width, float height, Font font, String textMessage, VertexBufferObjectManager vertexBufferObjectManager) {
this.setPosition(x, y);
this.attachChild(new Rectangle(0, 0, width, height, vertexBufferObjectManager));
this.text = new Text(0, 0, font, textMessage, vertexBufferObjectManager);
this.attachChild(text);
}
@Override
public void setAlpha(float pAlpha) {
super.setAlpha(pAlpha);
this.text.setAlpha(pAlpha);
}
}
这只是一个例子。如果您需要对矩形做更多的事情(调整大小等),您只需创建自己的方法来处理矩形和文本。唯一自动为两者设置的是位置(这里我将文本放在矩形的位置 0,0)。
希望对你有帮助
- 克里斯托夫
关于java - AndEngine SetAlpha 不影响子实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13868080/