andengine编程之Scene

上一篇已经对andengine做了简单介绍,接下来,我们将在介绍scene这个类



首先,你要准备两张图片,一张作为背景图,一张作为button,button的最好是两张背景不同的,用来表现出按下与不按下的效果。

素材准备好后,可以开始代码的编写了,接着上一个TestAndEngine继续写。

在onCreateEngineOptions中,构建EngineOptions的参数设置:

[java]  view plain copy
  1. private static final int CAMERA_WIDTH = 800;  
  2.     private static final int CAMERA_HEIGHT = 480;  
  3.     private static final float CAMERA_MOVE_VELOCITY = 3000;  
  4.     private static final float CAMERA_ZOOM_VELOCITY = 5;  
  5.   
  6.     public EngineOptions onCreateEngineOptions() {  
  7.         SmoothCamera mCamera = new SmoothCamera(00, CAMERA_WIDTH,  
  8.                 CAMERA_HEIGHT, CAMERA_MOVE_VELOCITY, CAMERA_MOVE_VELOCITY,  
  9.                 CAMERA_ZOOM_VELOCITY);  
  10.   
  11.         EngineOptions mEngineOptions = new EngineOptions(true,  
  12.                 ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(),  
  13.                 mCamera);  
  14.   
  15.         return mEngineOptions;  
  16.     }  


然后我们来加载资源:

[java]  view plain copy
  1. public void onCreateResources(  
  2.             OnCreateResourcesCallback pOnCreateResourcesCallback)  
  3.             throws Exception {  
  4.         // 1024, 512:这里的图片都要创建成2几次方的形式,例如128、256、512,原始图片也要创建成这样大小  
  5.         // TextureOptions.DEFAULT:使用默认的纹理效果,当然你也可选其他的形式  
  6.         BitmapTextureAtlas backgroundTextureAtlas = new BitmapTextureAtlas(  
  7.                 getTextureManager(), 1024512, TextureOptions.DEFAULT);  
  8.         // 图片要保存在asset文件夹下,如果图片很多,可以采用再建立文件夹加以区分  
  9.         // 然后用来设置根目录:  
  10.         // BitmapTextureAtlasTextureRegionFactory.setAssetBasePath(final String  
  11.         // pAssetBasePath);  
  12.         background = BitmapTextureAtlasTextureRegionFactory.createFromAsset(  
  13.                 backgroundTextureAtlas, this"background.png"00);  
  14.   
  15.         // 最后一定要调用这个方法,否则图片不会加载上来  
  16.         backgroundTextureAtlas.load();  
  17.   
  18.         // 同上  
  19.         BitmapTextureAtlas buttonTextureAtlas = new BitmapTextureAtlas(  
  20.                 getTextureManager(), 256256, TextureOptions.DEFAULT);  
  21.         // 通过帧序列块的方式创建button,注意顺序:第一张为正常效果,第二张为按下效果  
  22.         // 1, 2:共1列,有2行  
  23.         button = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(  
  24.                 buttonTextureAtlas, this"button.png"0012);  
  25.         // 加载一下  
  26.         buttonTextureAtlas.load();  
  27.   
  28.         // 最后一定要调用,通知程序可以开始调用onCreateScene方法  
  29.         pOnCreateResourcesCallback.onCreateResourcesFinished();  
  30.     }  



图片加载完后,我们还要构建场景,这个里面涉及到了sprite,我们用sprite来构建背景和按钮,sprite的扩展类中有ButtonSprite,很好的实现了button的操作。

[java]  view plain copy
  1. public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)  
  2.             throws Exception {  
  3.   
  4.         // 构建场景  
  5.         final Scene mScene = new Scene();  
  6.   
  7.         // 建立背景Sprite  
  8.         Sprite backgroundSprite = new Sprite(00, background,  
  9.                 getVertexBufferObjectManager());  
  10.   
  11.         // 建立按钮Sprite  
  12.         // 480, 200:显示的位置  
  13.         // button:为按钮图片帧  
  14.         buttonSprite = new ButtonSprite(480200, button,  
  15.                 getVertexBufferObjectManager(), new OnClickListener() {  
  16.   
  17.                     // 建立监听,当用户点住不放的时候,button图片会切换,但不会执行onClick里的操作  
  18.                     // 当用户松开按钮的时候,才会执行  
  19.                     public void onClick(ButtonSprite pButtonSprite,  
  20.                             float pTouchAreaLocalX, float pTouchAreaLocalY) {  
  21.   
  22.                         // 当用户点下后,我们将这个button从场景中移除掉  
  23.                         mScene.detachChild(buttonSprite);  
  24.                     }  
  25.                 });  
  26.   
  27.         // 将两个Sprite添加进场景  
  28.         mScene.attachChild(backgroundSprite);  
  29.         mScene.attachChild(buttonSprite);  
  30.   
  31.         // 注册buttonSprite的触摸机制  
  32.         mScene.registerTouchArea(buttonSprite);  
  33.   
  34.         // 最后一定要调用,通知系统我们创建了哪个scene  
  35.         pOnCreateSceneCallback.onCreateSceneFinished(mScene);  
  36.     }  


最后,记得,一定要写好:

[java]  view plain copy
  1. public void onPopulateScene(Scene pScene,  
  2.         OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {  
  3.     // 最后千万不要忘记了这个  
  4.     pOnPopulateSceneCallback.onPopulateSceneFinished();  
  5. }  

否则,线程是不会跑起来刷屏的!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值