andengine源码及示例程序,这是最新版。 上课用的andengine是上一个版本,变化较大。
示例程序也重新进行了修改,导入示例程序时需要附加的一些库。
步骤如下:
导入这些库程序项目,并为分别为它们指定最新版的
1 把示例程序项目和这些库程序项目(在下图中所有红色叉号的都是需要的项目)都导入andengine库文件andengine.jar
2 导入示例程序AndEngineExamples新建一个目录libs并把第1步中生成的库文件都复制到这个目录,然后添加路径,而不是像下图所示的方法(通过所所有库项目都导入)。
一个简单的示例程序:
import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.particle.SpriteParticleSystem;
import org.andengine.entity.particle.emitter.CircleOutlineParticleEmitter;
import org.andengine.entity.particle.initializer.AlphaParticleInitializer;
import org.andengine.entity.particle.initializer.BlendFunctionParticleInitializer;
import org.andengine.entity.particle.initializer.ColorParticleInitializer;
import org.andengine.entity.particle.initializer.RotationParticleInitializer;
import org.andengine.entity.particle.initializer.VelocityParticleInitializer;
import org.andengine.entity.particle.modifier.AlphaParticleModifier;
import org.andengine.entity.particle.modifier.ColorParticleModifier;
import org.andengine.entity.particle.modifier.ExpireParticleInitializer;
import org.andengine.entity.particle.modifier.ScaleParticleModifier;
import org.andengine.entity.scene.IOnSceneTouchListener;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.util.FPSLogger;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import android.opengl.GLES20;
import android.widget.Toast;
public class MainActivity extends SimpleBaseGameActivity {
// ===========================================================
// Constants
// ===========================================================
private static final int CAMERA_WIDTH = 720;
private static final int CAMERA_HEIGHT = 480;
// ===========================================================
// Fields
// ===========================================================
private Camera mCamera;
private BitmapTextureAtlas mBitmapTextureAtlas;
private ITextureRegion mParticleTextureRegion;
// ===========================================================
// Constructors
// ===========================================================
// ===========================================================
// Getter & Setter
// ===========================================================
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
@Override
public EngineOptions onCreateEngineOptions() {
Toast.makeText(this, "Touch the screen to move the particlesystem.", Toast.LENGTH_LONG).show();
this.mCamera = new Camera(0, 0, MainActivity.CAMERA_WIDTH, MainActivity.CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(MainActivity.CAMERA_WIDTH, MainActivity.CAMERA_HEIGHT), this.mCamera);
}
@Override
protected void onCreateResources() {
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 32, 32, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
this.mParticleTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "particle_point.png", 0, 0);
this.mBitmapTextureAtlas.load();
}
@Override
protected Scene onCreateScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
final Scene scene = new Scene();
final CircleOutlineParticleEmitter particleEmitter = new CircleOutlineParticleEmitter(MainActivity.CAMERA_WIDTH * 0.5f, MainActivity.CAMERA_HEIGHT * 0.5f + 20, 80);
final SpriteParticleSystem particleSystem = new SpriteParticleSystem(particleEmitter, 60, 60, 360, this.mParticleTextureRegion, this.getVertexBufferObjectManager());
scene.setOnSceneTouchListener(new IOnSceneTouchListener() {
@Override
public boolean onSceneTouchEvent(final Scene pScene, final TouchEvent pSceneTouchEvent) {
particleEmitter.setCenter(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
return true;
}
});
particleSystem.addParticleInitializer(new ColorParticleInitializer<Sprite>(1, 0, 0));
particleSystem.addParticleInitializer(new AlphaParticleInitializer<Sprite>(0));
particleSystem.addParticleInitializer(new BlendFunctionParticleInitializer<Sprite>(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE));
particleSystem.addParticleInitializer(new VelocityParticleInitializer<Sprite>(-2, 2, -20, -10));
particleSystem.addParticleInitializer(new RotationParticleInitializer<Sprite>(0.0f, 360.0f));
particleSystem.addParticleInitializer(new ExpireParticleInitializer<Sprite>(6));
particleSystem.addParticleModifier(new ScaleParticleModifier<Sprite>(0, 5, 1.0f, 2.0f));
particleSystem.addParticleModifier(new ColorParticleModifier<Sprite>(0, 3, 1, 1, 0, 0.5f, 0, 0));
particleSystem.addParticleModifier(new ColorParticleModifier<Sprite>(4, 6, 1, 1, 0.5f, 1, 0, 1));
particleSystem.addParticleModifier(new AlphaParticleModifier<Sprite>(0, 1, 0, 1));
particleSystem.addParticleModifier(new AlphaParticleModifier<Sprite>(5, 6, 1, 0));
scene.attachChild(particleSystem);
return scene;
}
// ===========================================================
// Methods
// ===========================================================
// ===========================================================
// Inner and Anonymous Classes
}
使用了新库,添加了资源,如下图所示:
<img src="https://img-blog.csdn.net/20141108125119375?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYWhiYnNoZW5mZW5n/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
这里的函数框架名与老版本的已经有很大变化,不过本质内容还是没变!
如果出现如下错误:
Unable to execute dex: Multiple dex files define
请查看这个。