AndEngine---绘制形状,创建精灵

来源:互联网 发布:淘宝怎么增加流量 编辑:程序博客网 时间:2024/05/21 22:59

关于绘制图形,和创建精灵,贴图的一些简单操作,根据所学知识和理解程度会不断更新

先贴代码:

<p>public class DrawShape extends SimpleBaseGameActivity { private final int ScreamWidth = Myapplication.ScreenW; private final int ScreamHeight = Myapplication.ScreenH;</p><p> private Camera mCamera; private EngineOptions mOptions; private Scene mScene = new Scene();</p><p> private BitmapTexture mBitmapITexture1; private ITextureRegion mBitmapITexture1Region; private BitmapTextureAtlas mBitmapTextureAtlas; private ITextureRegion mBitmapITexture2Region, mBitmapITexture3Region; private BitmapTextureAtlas mBitmapTextureAtlas4; private ITextureRegion mITextureRegion4; private Sprite mSprite1, mSprite2, mSprite3, mSprite4;</p><p> @Override public EngineOptions onCreateEngineOptions() {  // TODO Auto-generated method stub  mCamera = new Camera(0, 0, ScreamWidth, ScreamHeight);</p><p>  /*   * new EngineOptions(pFullscreen, pScreenOrientation, pResolutionPolicy,   * pCamera) pFullscreen:是否全屏显示, pScreenOrientation:横竖屏,   * pResolutionPolicy: 处理不同屏幕大小的情况, ------   * 四种对象:RationResolutionPolicy(保持长宽比拉伸);   * -------FillResolutionPolicy(直接拉伸至全屏)   * -------FixedResolutionPolicy(设置固定大小来显示)   * -------RelativeResolutionPolicy(设置放大倍数) pCamera:镜头   */  mOptions = new EngineOptions(true, ScreenOrientation.PORTRAIT_SENSOR,    new FillResolutionPolicy(), mCamera);  return mOptions; }</p><p> @Override protected void onCreateResources() {  // TODO Auto-generated method stub  /*   * 创建一个贴图区域 assets目录 BitmapTexture---ITextureRegion   */  try {   mBitmapITexture1 = new BitmapTexture(getTextureManager(),     new IInputStreamOpener() {</p><p>      @Override      public InputStream open() throws IOException {       // TODO Auto-generated me thod stub       return getAssets().open("gfx/face_box.png");      }     });   mBitmapITexture1.load();   mBitmapITexture1Region = TextureRegionFactory     .extractFromTexture(mBitmapITexture1);  } catch (IOException e) {   // TODO Auto-generated catch block   e.printStackTrace();  }</p><p>  /*   * 创建贴图, assets目录 BitmapTexture---ITextureRegion(接口)   * ITexture(接口)对象构造出了一个空白区域,这个区域的大小不能小于位图对象ITextureRegion大小   * ITextureRegionFactory(接口)对象将图像载入   */  BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");// 通过assets目录必须放在                  // gfx文件夹下  mBitmapTextureAtlas = new BitmapTextureAtlas(getTextureManager(),    ScreamWidth, ScreamHeight);  mBitmapITexture2Region = BitmapTextureAtlasTextureRegionFactory    .createFromAsset(mBitmapTextureAtlas, getAssets(),      "face_box.png", 0, 0);  mBitmapITexture3Region = BitmapTextureAtlasTextureRegionFactory    .createFromAsset(mBitmapTextureAtlas, getAssets(),      "madmat.png",0,0);//最后两个参数是载入的图片在Itexture中占据的位置,但暂时未发现实际意义<span style="color:#cc66cc;">【注1】</span>,只要不大于camera大小就好,一般设置为0,                                                                  mBitmapTextureAtlas.load();// 加载了两个位图</p><p>                                                                                                                                         /*   * 创建贴图区域,Resource   */  mBitmapTextureAtlas4 = new BitmapTextureAtlas(getTextureManager(),    ScreamWidth, ScreamHeight);  mITextureRegion4 = BitmapTextureAtlasTextureRegionFactory    .createFromResource(mBitmapTextureAtlas4, this,      R.drawable.ic_launcher, 0, 0);  mBitmapTextureAtlas4.load(); }</p><p> @Override protected Scene onCreateScene() {  // TODO Auto-generated method stub  mScene.setBackground(new Background(0.59f, 0.224f, 0.897f));// 给场景设置背景色</p><p>  Line mLine1 = new Line(0.0f, 0.0f, 340.0f, 640.0f,    getVertexBufferObjectManager());// 新建线条,参数是起点的坐标,宽高,和顶点流管理器(get不要每次new)  Rectangle mRectangle1 = new Rectangle(100, 100, 100, 100,    getVertexBufferObjectManager());// 新建矩形,参数是起点的坐标,宽高,和顶点流管理器  mRectangle1.setColor(Color.CYAN);// 给矩形上色  mScene.attachChild(mRectangle1);// 添加到场景中  mScene.attachChild(mLine1);</p><p>  mSprite1 = new Sprite(200, 200, mBitmapITexture1Region,    getVertexBufferObjectManager());  mScene.attachChild(mSprite1);</p><p>  mSprite2 = new Sprite(100, 100, mBitmapITexture2Region,    getVertexBufferObjectManager());  mScene.attachChild(mSprite2);    mSprite3 = new Sprite(200, 100, mBitmapITexture3Region,    getVertexBufferObjectManager());  mSprite3.setScale(2);// 放大,以中心放大  mSprite3.setAlpha(0.5f);// 透明度  mScene.attachChild(mSprite3);  mSprite4 = new Sprite(50, 100, mITextureRegion4,    getVertexBufferObjectManager());  mScene.attachChild(mSprite4);    /*   * 删除精灵   */  final EngineLock engineLock = this.mEngine.getEngineLock();  engineLock.lock();  /* Now it is save to remove the entity! */  mScene.detachChild(mSprite4);  mSprite4.dispose();  mSprite4 = null;  engineLock.unlock();</p><p>  return mScene; }</p><p>}</p>


 

 

【注1】

 书上原话:开发者可能会注意到一个缺陷:每次必须告诉TextureRegionFactory所加载的图片在Texture对象中所处的位置……BuildableTexture可以自动安排图片位置的方法。

    之前不理解,因为公用一个Teture 贴图区域对象时,并未发生贴图对象重叠的现象,估计是在创建精灵的时候定义了精灵位置,同时这些精灵都是静态的,在做简单的动画效果时,动画贴图和另一个静态贴图共用了一个贴图区域对象,他们的坐标都统一设置成了0,0,导致动画实现的时候出现的另一贴图的影响,发生了重叠影响。实际上多个贴图对象共用一个贴图区域就相当于TiledTextureRegion对象,一张图上有多个图像,再分割显示。

贴图对象共用一个区域对象减少区域对象的创建可以减少内存占用,要注意位置设置,尽量避免重叠!

 

 当有动画的时候这个方法就相当麻烦了,图与图之间时常影响,并且找不到位置设置的规律,因此在全是静态贴图的时候可以用这样的方法来创建,否则还是使用BuildTexture来自动排列贴图好得多,调用build方法:如下
mRegion3 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
                  mAtlas2 , getAssets(), "SpriteTest/p1.png" );
 mRegion2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
                  mAtlas2 , getAssets(), "SpriteTest/p2.png" );
  mRegion4 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
                  mAtlas2 , getAssets(), "face_box.png" );
 try {
 mAtlas2 .build( new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource,             
                  BitmapTextureAtlas>( 0, 0, 0));//设置贴图间的间隔,与边界的间隔
         } catch (TextureAtlasBuilderException e) {
              // TODO Auto-generated catch block
             e.printStackTrace();
         }

 

 

 

 

 

 

 

 

0 0
原创粉丝点击