AndEngine---绘制形状,创建精灵_microbit创建精灵_MrLC的博客-程序员宝宝

技术标签: AndEngine  android  

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

先贴代码:

<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();
         }

 

 

 

 

 

 

 

 

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/lyt819/article/details/44041735

智能推荐

qt 设置标题栏图标、任务栏图标和启动图标_qt设置标题栏图标_sbodakes的博客-程序员宝宝

一. 标题栏图标、任务栏图标设置(只需3步)1.建议准备正方形png图像,带透明通道。2.qrc文件添加名称。3.窗口属性中选择图片。二. 启动图标设置(只要2步)1.准备.ico格式图片,建议32*32大小。2.在vs中导入ico,重新编译即可。...

C#详解struct和class的区别_c# struct的无参构造函数_HawkJony的博客-程序员宝宝

可以从数据类型、访问权限(封装)、继承、构造函数、实例化方面做比较3.struct 默认的访问权限是public,而class默认的访问权限是private.简单来说,struct是值类型,创建一个struct类型的实例被分配在栈上。class是引用类型,创建一个class类型实例被分配在托管堆上。但struct和class的区别远不止这么简

收藏一些视频教程_此生辽阔的博客-程序员宝宝

黑马程序员匠心之作|C++教程从0到1入门编程,学习编程不再难黑马程序员MFC教程(已完结)MFC进阶教程深入浅出版尚硅谷_Java零基础教程-java入门必备-初学者从入门到精通全套完整版(宋红康主讲)尚硅谷-Spring5框架2020最新版教程...

tensorflow学习笔记——计算图_tensorflow查看计算图_wxsy024680的博客-程序员宝宝

1、编辑with tf.name_scope(‘input’),将若干个变量打包起来并命名为input;2、编辑writer=tf.summary.FileWriter(‘logs’,sess.graph),将计算图保存在logs文件夹;3、运行Python代码,得到计算图文件;4、在命令行窗口运行tensorboard --logdir logs,得到一个网址;5、用谷歌浏览器打开该网址,即可查看计算图;import tensorflow as tffrom tensorflow.exampl

矩阵比较大小_比较矩阵_yangkexi1073的博客-程序员宝宝

**矩阵元素之间比较大小**矩阵比较大小一般指同型矩阵之间比较对应位置的元素大小情况,比如: a=magic(3),a =8 1 63 5 74 9 2b=ones(3,3)b =1 1 11 1 11 1 1a&gt;bans =1 0 11 1 11 1 1如果矩阵不同型,应该说比较大小没有意义.我们对Markdown编辑器进行了一些功能拓展与语法支持,除了标准的Markdown编辑器功能,我们增加了如下几点新功能,帮助你用它写博客:全新的界面设计

随便推点

【TensorFlow】tf.transpose()详解_roguesir的博客-程序员宝宝

官方API文档:transpose( a, # 输入张量 perm=None, # 转置规则,后面详细介绍 name='transpose')Args:a: A Tensor.perm: A permutation of the dimensions of a.name: A name for the operation (optional).Returns:A transposed Tensor

搭建STM8S001开发环境_队长-Leader的博客-程序员宝宝

1、IAR。由于keil不支持STM8,所以改用IAR。IAR For STM8 3.10或以上版本才支持STM8S001,可以直接在官网免费下载,如下图所示。破解工具用IAR Offline License Generator v1.4 by unis。如下图所示。注意:v1.3是无法破解IAR For STM8 3.10及以上版本。v1.4的破解工具可以在CSDN上找到...

JDBC相关技术知识_jdbc连接数据库时需要4个基本属性_I'm you.的博客-程序员宝宝

一、概述1、介绍2、编写步骤二、获取数据库连接要素1、Driver接口实现类要素2、URL要素3、用户名和密码代码实现连接示例:方式一:方式二:三、PreparedStatement实现CRUD操作...

程序员真的是吃青春饭的吗?(献给即将进入职场的程序员们)_大家怎么看待程序员是吃青春饭的观点呢?_八马难追的博客-程序员宝宝

又有学生问我:程序员真的是吃青春饭的吗?我是不是做到三十岁就该考虑转型了?      我告诉他们:      这是中国的记者们用统计数字造下的一个弥天大谎,当我们看到微软集团内的许多白发程序员在兢兢业业地工作的时候,我们又用&quot;观念&quot;来说明中国的程序员吃青春饭的原因。实际上,不仅美国的微软,甲骨文,Adobe,暴雪,在中国的金山,寰宇,腾讯,盛大,都有或者将要有年龄很大的程序员,关键是他们做的东西和...

jQuery 2.0.3 源码分析 回调对象 - Callbacks_weixin_33694172的博客-程序员宝宝

源码API:http://api.jquery.com/jQuery.Callbacks/jQuery.Callbacks()是在版本1.7中新加入的。它是一个多用途的回调函数列表对象,提供了一种强大的方法来管理回调函数队列。那么jQuery.Callbacks使用场景在哪里?在很多时候需要控制一系列的函数顺序执行。那么一般就需要一个队列函数来...

C# class_c#class_做游戏的小轩轩的博客-程序员宝宝

static 的方法和变量是 类本身的 不能通过实例化去改变  class Person { static int age;}main{ var person = new Person(); //person.age不行 Person.age = 5;}class 属性Class Perspn{ int age; public int Age{

推荐文章

热门文章

相关标签