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.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.sprite.batch.DynamicSpriteBatch;
import org.andengine.entity.sprite.batch.SpriteBatch;
import org.andengine.entity.util.FPSLogger;
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.TextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.color.Color;
import android.util.DisplayMetrics;
/**
* 使用SpriteBatch批量绘制Sprite
*/
public class SpriteBatchActivity extends SimpleBaseGameActivity
{
private static int winWidth = 854;
private static int winHeight = 480;
//图片纹理区域
private BitmapTextureAtlas mBitmapTextureAtlas;
private TextureRegion mTextureRegion;
@Override
public EngineOptions onCreateEngineOptions()
{
setScreenDisplay();
Camera camera = new Camera(0, 0, winWidth, winHeight);
EngineOptions engineOptions = new EngineOptions(
true, ScreenOrientation.LANDSCAPE_FIXED,
new RatioResolutionPolicy(winWidth, winHeight),
camera);
return engineOptions;
}
@Override
protected void onCreateResources()
{
//设置图片的路径
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("images/");
mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 32, 32, TextureOptions.DEFAULT);
//后面两个参数是指该纹理加载到纹理区域上的什么位置
mTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mBitmapTextureAtlas, this, "face_box.png", 0, 0);
//真正加载
mBitmapTextureAtlas.load();
}
@Override
protected Scene onCreateScene()
{
this.mEngine.registerUpdateHandler(new FPSLogger());
Scene scene = new Scene();
//颜色:真实值/255
scene.setBackground(new Background(new Color(0.6f,0.8f,0.6f)));
final float centerX = (winWidth - mTextureRegion.getWidth()) / 2;
final float centerY = (winHeight - mTextureRegion.getHeight()) / 2;
final Sprite sprite1 = new Sprite(-50, 0, mTextureRegion, this.getVertexBufferObjectManager());
final Sprite sprite2 = new Sprite(-50, 150, mTextureRegion, this.getVertexBufferObjectManager());
//缩放原来的3倍
sprite1.setScale(3);
//精灵中心为轴顺时针方向旋转50度
sprite1.setRotation(50);
/**
* SpriteBatch的使用,我们知道精灵无非是一个纹理和他对应范围的组合;
* 那精灵族可以直观的把它看成同一纹理对应多个范围的形式。我们可以通过draw方法在其中增加新的显示范围,包括定义其大小方向等等,
* 但是mTexture是不能改变的,相当于同一张图片渲染了多次。
* 可以使用SpriteBatch批量绘制Sprite,
* SpriteBatch又分为静态的和动态的,把SpriteBatch设想为画布,
* 动态的无需在初始化的时候指定范围,而是根据每个Sprite自身的参数绘制,
* 当需要渲染的时候他会回调onUpdateSpriteBatch让程序更新当时情况设置。
*
* 静态的必须要指定每个Sprite在画布中的绘制位置,长宽,比例,旋转等参数,
* 添加完成之后一定要提交,这样才能完成渲染,动态的提交写如begin方法里面,我们可以不管。
* 最后,要把SpriteBatch添加到Scene中。
*/
//动态的精灵族
SpriteBatch dynamicSpriteBatch = new DynamicSpriteBatch(mBitmapTextureAtlas,2,this.getVertexBufferObjectManager())
{
/**
* 该方法是一直在循环执行的
*/
@Override
protected boolean onUpdateSpriteBatch()
{
draw(sprite1);
draw(sprite2);
return true;
}
};
//静态精灵组
SpriteBatch staticSpriteBatch = new SpriteBatch(mBitmapTextureAtlas, 2, this.getVertexBufferObjectManager());
//后面的颜色是精灵的背景色
staticSpriteBatch.draw(mTextureRegion, 20, 20, mTextureRegion.getWidth(), mTextureRegion.getHeight(), -45, 1, 1, 1, 1);
staticSpriteBatch.draw(mTextureRegion, 20, 120, mTextureRegion.getWidth(), mTextureRegion.getHeight(), 45, 1, 1, 1, 0.3f);
//添加完成之后一定要提交,这样才能完成渲染,动态的提交写如begin方法里面,我们可以不管
staticSpriteBatch.submit();
//设置整组精灵的初始位置
dynamicSpriteBatch.setPosition(centerX, centerY);
scene.attachChild(dynamicSpriteBatch);
scene.attachChild(staticSpriteBatch);
return scene;
}
/**
* 设置屏幕大小
*/
private void setScreenDisplay()
{
DisplayMetrics outMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
winWidth = outMetrics.widthPixels;
winHeight = outMetrics.heightPixels;
}
}
相关推荐
AndEngine_分析之二-----BaseGameActivity. 中文
AndEngine_分析之三-----AsyncTask_BaseActivity备忘
AndEngine_分析之一----载入资源 这个是英文的
用andEngine最新的代码,然后用javadoc和easychm工具将它做成了chm格式的帮助文档,现在传到了上面,有需要的朋友可以下载
封装了个andengine的动画精灵类,可以直接传入多张小图片的名称,直接生成动画精灵
AndEngine游戏 DeliveryBoy 源代码,可以用来做AndEngine游戏实例教程进行参考
AndEngine游戏开发指南-初级篇.pdf android基于AndEngine游戏引擎开发
AndEngine 2013-3-12 最新Jar包,直接拉进项目libs即可,可查看源代码
##摘要以下是andEngine和andEnginePhysicsBox2DExtension的可编译就绪源代码集,仅在Android Studio中使用。 JNI本机扩展已被编译并包含在内,而所需要做的只是开始编写您的游戏。 这是我们的游戏《 Dot!Panic》的...
AndEngine 2013-2月 最新Example,直接导入Eclipse即可,API15, 环境 Android 4.0.3
基于AndEngine的SpriteBatch绘制二维数组的地图
AndEngine游戏引擎进阶之自定义Tiled精灵,AndEngine游戏引擎是一个开源的Android 2D游戏引擎。
Android-游戏引擎AndEngine范例-源码
我根据andengine文档和网上的例子,自己加工在androidstadio上运行代码中有我照着文档写的例子,根据自己理解做了一点注释,希望对新人有帮助
在制作动画精灵的时候,为了方便计算,常常需要把场景中的坐标转换为精灵的内部坐标,或者需要把精灵的内部坐标转换为场景坐标.如果精灵没有进行过旋转操作,他们之间只差 一个offse而已
AndEngine下载 AndEngine
这使得开发者在遇到问题时可以直接从源码上找到答案,也能按照自己的需要对AndEngine进行修改和扩展。AndEngine的源码在github上托管[2]。 高效 AndEngine主要使用Java语言开发,但在大运算量的耗时功能时,...
这使得开发者在遇到问题时可以直接从源码上找到答案,也能按照自己的需要对AndEngine进行修改和扩展。AndEngine的源码在github上托管[2]。 高效 AndEngine主要使用Java语言开发,但在大运算量的耗时功能时,...
官方版本,亲测可用
本源码为使用android开源游戏引擎andengine开发的捕鱼达人游戏,引擎中使用的是andengine游戏引擎的老版本。可供大家参考研究。