接着 andengine编程之Scene

以下是完整的代码:

[java]  view plain copy
  1. package com.test;  
  2.   
  3. import org.andengine.engine.camera.SmoothCamera;  
  4. import org.andengine.engine.options.EngineOptions;  
  5. import org.andengine.engine.options.EngineOptions.ScreenOrientation;  
  6. import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;  
  7. import org.andengine.entity.scene.Scene;  
  8. import org.andengine.entity.sprite.ButtonSprite;  
  9. import org.andengine.entity.sprite.Sprite;  
  10. import org.andengine.entity.sprite.ButtonSprite.OnClickListener;  
  11. import org.andengine.opengl.texture.TextureOptions;  
  12. import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;  
  13. import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;  
  14. import org.andengine.opengl.texture.region.ITextureRegion;  
  15. import org.andengine.opengl.texture.region.TiledTextureRegion;  
  16. import org.andengine.ui.activity.BaseGameActivity;  
  17.   
  18. public class TestAndEngine extends BaseGameActivity {  
  19.   
  20.     private static final int CAMERA_WIDTH = 800;  
  21.     private static final int CAMERA_HEIGHT = 480;  
  22.     private static final float CAMERA_MOVE_VELOCITY = 3000;  
  23.     private static final float CAMERA_ZOOM_VELOCITY = 5;  
  24.   
  25.     private ITextureRegion background;  
  26.   
  27.     private TiledTextureRegion button;  
  28.   
  29.     private ButtonSprite buttonSprite;  
  30.   
  31.     public EngineOptions onCreateEngineOptions() {  
  32.         SmoothCamera mCamera = new SmoothCamera(00, CAMERA_WIDTH,  
  33.                 CAMERA_HEIGHT, CAMERA_MOVE_VELOCITY, CAMERA_MOVE_VELOCITY,  
  34.                 CAMERA_ZOOM_VELOCITY);  
  35.   
  36.         EngineOptions mEngineOptions = new EngineOptions(true,  
  37.                 ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(),  
  38.                 mCamera);  
  39.         mEngineOptions.getAudioOptions().setNeedsMusic(true);  
  40.         mEngineOptions.getAudioOptions().setNeedsSound(true);  
  41.   
  42.         return mEngineOptions;  
  43.     }  
  44.   
  45.     public void onCreateResources(  
  46.             OnCreateResourcesCallback pOnCreateResourcesCallback)  
  47.             throws Exception {  
  48.         // 1024, 512:这里的图片都要创建成2几次方的形式,例如128、256、512,原始图片也要创建成这样大小  
  49.         // TextureOptions.DEFAULT:使用默认的纹理效果,当然你也可选其他的形式  
  50.         BitmapTextureAtlas backgroundTextureAtlas = new BitmapTextureAtlas(  
  51.                 getTextureManager(), 1024512, TextureOptions.DEFAULT);  
  52.         // 图片要保存在asset文件夹下,如果图片很多,可以采用再建立文件夹加以区分  
  53.         // 然后用来设置根目录:  
  54.         // BitmapTextureAtlasTextureRegionFactory.setAssetBasePath(final String  
  55.         // pAssetBasePath);  
  56.         background = BitmapTextureAtlasTextureRegionFactory.createFromAsset(  
  57.                 backgroundTextureAtlas, this"background.png"00);  
  58.   
  59.         // 最后一定要调用这个方法,否则图片不会加载上来  
  60.         backgroundTextureAtlas.load();  
  61.   
  62.         // 同上  
  63.         BitmapTextureAtlas buttonTextureAtlas = new BitmapTextureAtlas(  
  64.                 getTextureManager(), 256256, TextureOptions.DEFAULT);  
  65.         // 通过帧序列块的方式创建button,注意顺序:第一张为正常效果,第二张为按下效果  
  66.         // 1, 2:共1列,有2行  
  67.         button = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(  
  68.                 buttonTextureAtlas, this"button.png"0012);  
  69.         // 加载一下  
  70.         buttonTextureAtlas.load();  
  71.   
  72.         // 最后一定要调用,通知程序可以开始调用onCreateScene方法  
  73.         pOnCreateResourcesCallback.onCreateResourcesFinished();  
  74.     }  
  75.   
  76.     public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)  
  77.             throws Exception {  
  78.   
  79.         // 构建场景  
  80.         final Scene mScene = new Scene();  
  81.   
  82.         // 建立背景Sprite  
  83.         Sprite backgroundSprite = new Sprite(00, background,  
  84.                 getVertexBufferObjectManager());  
  85.   
  86.         // 建立按钮Sprite  
  87.         // 480, 200:显示的位置  
  88.         // button:为按钮图片帧  
  89.         buttonSprite = new ButtonSprite(480300, button,  
  90.                 getVertexBufferObjectManager(), new OnClickListener() {  
  91.   
  92.                     // 建立监听,当用户点住不放的时候,button图片会切换,但不会执行onClick里的操作  
  93.                     // 当用户松开的时候,才会执行  
  94.                     public void onClick(ButtonSprite pButtonSprite,  
  95.                             float pTouchAreaLocalX, float pTouchAreaLocalY) {  
  96.                         // 当用户点下后,我们将这个button从场景中移除掉  
  97.                         mScene.detachChild(buttonSprite);  
  98.                     }  
  99.                 });  
  100.   
  101.         // 将两个Sprite添加进场景  
  102.         mScene.attachChild(backgroundSprite);  
  103.         mScene.attachChild(buttonSprite);  
  104.   
  105.         // 注册buttonSprite的触摸机制  
  106.         mScene.registerTouchArea(buttonSprite);  
  107.   
  108.         // 最后一定要调用,通知系统我们创建了哪个scene  
  109.         pOnCreateSceneCallback.onCreateSceneFinished(mScene);  
  110.     }  
  111.   
  112.     public void onPopulateScene(Scene pScene,  
  113.             OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {  
  114.         // 最后千万不要忘记了这个  
  115.         pOnPopulateSceneCallback.onPopulateSceneFinished();  
  116.     }  
  117.   
  118. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值