Waq*_*qas 11 java code-reuse android opengl-es-2.0 andengine
目前我正在对我的所有精灵进行静态引用,并在SimpleBaseGameActivity的OnCreateResource方法中加载和初始化它们,但是现在我必须在spirtes上覆盖onAreaTouched监听器以及在初始化Sprite时我可以覆盖它的方式.但我有一个静态方法为每个精灵创建Atlas和Texture Region.我在我的场景类中使用这些精灵,我想在那里覆盖onAreaTouched.我可以在我的场景中为特定的精灵注册TouchArea,这样就可以完成但是我想以某种方式覆盖OnAreaTouched,以便可以完成代码的可重用性.这是我目前正在创建和加载精灵的方式.
defualtCageSprite = createAndLoadSimpleSprite("bg.png", this, 450, 444);
Run Code Online (Sandbox Code Playgroud)
这是我的方法createAndLoadSimpleSprite.
public static Sprite createAndLoadSimpleSprite(String name,
SimpleBaseGameActivity activity, int width, int height) {
BitmapTextureAtlas atlasForBGSprite = new BitmapTextureAtlas(
activity.getTextureManager(), width, height);
TextureRegion backgroundSpriteTextureRegion = BitmapTextureAtlasTextureRegionFactory
.createFromAsset(atlasForBGSprite, activity, name, 0, 0);
Sprite sprite = new Sprite(0, 0, backgroundSpriteTextureRegion,
activity.getVertexBufferObjectManager());
activity.getTextureManager().loadTexture(atlasForBGSprite);
return sprite;
}
Run Code Online (Sandbox Code Playgroud)
现在,如何在不丢失代码可重用性的情况下覆盖某些精灵的onAreaTouched.
你有什么理由需要在运行时加载纹理吗?通常的方法是在加载应用程序时将所需纹理全部加载到单个图集上,以便稍后可以快速使用它们.
至于代码的可重用性,Todilo关于枚举的想法似乎就是你所需要的.例如,您有两种对象 - 当您触摸它们时消失的对象和触摸它们时飞起来的对象.您枚举两个类别并将一段代码放入触摸事件处理代码中,以检查对象是否应该消失或飞起来.
如果在运行应用程序之前您不知道对象应该在触摸时做什么,那么有一种更动态的方法可以实现相同的结果.只需在运行时创建两个列表,并根据触摸时对象应该执行的操作,在其中一个列表中引用该对象.然后在触摸事件处理中执行以下操作:
if (disappearList.contains(touchedObject)) {
disappear(object)
}
if (flyUpList.contains(touchedObject)) {
flyUp(object)
}
Run Code Online (Sandbox Code Playgroud)
太糟糕AndEngine不允许用户在精灵上设置监听器,这会让事情变得更容易一些.
编辑:添加了使用BlackPawnTextureBuilder的解释:你的Atlas必须是BuildableBitmapTextureAtlas类型,然后你添加这样的所有纹理
BitmapTextureAtlasTextureRegionFactory.createFromAsset(buildableAtlas, this, "image.png"));
Run Code Online (Sandbox Code Playgroud)
在那之后
try {
this.buildableAtlas.build(new BlackPawnTextureBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(1));
} catch (final TextureAtlasSourcePackingException e) {
Debug.e(e);
}
Run Code Online (Sandbox Code Playgroud)
我不知道这是否适用于动画精灵,你将不得不尝试.此外,没有重写onTouch,您必须在onAreaTouched方法中执行此操作.这种情况的一个例子是
if (pSceneMotionEvent.getAction() == MotionEvent.ACTION_DOWN && disappearList.contains(pTouchArea)) {disappear();}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3384 次 |
最近记录: |