接着andengine编程之background(一)

好了,你看明白了吗,以下是源代码:
[java]  view plain copy
  1. package com.testsprite;  
  2.   
  3. import org.andengine.engine.camera.Camera;  
  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.modifier.LoopEntityModifier;  
  8. import org.andengine.entity.scene.IOnSceneTouchListener;  
  9. import org.andengine.entity.scene.Scene;  
  10. import org.andengine.entity.scene.background.AutoParallaxBackground;  
  11. import org.andengine.entity.scene.background.ParallaxBackground.ParallaxEntity;  
  12. import org.andengine.entity.sprite.AnimatedSprite;  
  13. import org.andengine.entity.sprite.Sprite;  
  14. import org.andengine.input.touch.TouchEvent;  
  15. import org.andengine.opengl.texture.TextureOptions;  
  16. import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;  
  17. import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;  
  18. import org.andengine.opengl.texture.region.TextureRegion;  
  19. import org.andengine.opengl.texture.region.TiledTextureRegion;  
  20. import org.andengine.ui.activity.BaseGameActivity;  
  21.   
  22. public class TestSprite extends BaseGameActivity {  
  23.     private static final int CAMERA_WIDTH = 720;  
  24.     private static final int CAMERA_HEIGHT = 480;  
  25.   
  26.     private BitmapTextureAtlas mBitmapTextureAtlas;  
  27.     private TiledTextureRegion mPlayerTextureRegion;  
  28.   
  29.     private BitmapTextureAtlas mAutoParallaxBackgroundTexture;  
  30.   
  31.     private TextureRegion mParallaxLayerBack;  
  32.     private TextureRegion mParallaxLayerMid;  
  33.     private TextureRegion mParallaxLayerFront;  
  34.   
  35.     public EngineOptions onCreateEngineOptions() {  
  36.         Camera mCamera = new Camera(00, CAMERA_WIDTH, CAMERA_HEIGHT);  
  37.         EngineOptions mEngineOptions = new EngineOptions(true,  
  38.                 ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(),  
  39.                 mCamera);  
  40.         return mEngineOptions;  
  41.     }  
  42.   
  43.     public void onCreateResources(  
  44.             OnCreateResourcesCallback pOnCreateResourcesCallback)  
  45.             throws Exception {  
  46.         this.mBitmapTextureAtlas = new BitmapTextureAtlas(getTextureManager(),  
  47.                 128128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);  
  48.         this.mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory  
  49.                 .createTiledFromAsset(this.mBitmapTextureAtlas, this,  
  50.                         "player.png"0034);  
  51.   
  52.         this.mAutoParallaxBackgroundTexture = new BitmapTextureAtlas(  
  53.                 getTextureManager(), 10241024, TextureOptions.DEFAULT);  
  54.         this.mParallaxLayerFront = (TextureRegion) BitmapTextureAtlasTextureRegionFactory  
  55.                 .createFromAsset(this.mAutoParallaxBackgroundTexture, this,  
  56.                         "parallax_background_layer_front.png"00);  
  57.         this.mParallaxLayerBack = (TextureRegion) BitmapTextureAtlasTextureRegionFactory  
  58.                 .createFromAsset(this.mAutoParallaxBackgroundTexture, this,  
  59.                         "parallax_background_layer_back.png"0188);  
  60.         this.mParallaxLayerMid = (TextureRegion) BitmapTextureAtlasTextureRegionFactory  
  61.                 .createFromAsset(this.mAutoParallaxBackgroundTexture, this,  
  62.                         "parallax_background_layer_mid.png"0669);  
  63.   
  64.         mBitmapTextureAtlas.load();  
  65.         mAutoParallaxBackgroundTexture.load();  
  66.   
  67.         pOnCreateResourcesCallback.onCreateResourcesFinished();  
  68.     }  
  69.   
  70.     LoopEntityModifier mLoopEntityModifier;  
  71.   
  72.     public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)  
  73.             throws Exception {  
  74.         Scene mScene = new Scene();  
  75.   
  76.         // 最后一个参数原来是5,我们这里设为0,意思是让它一开始不要滚动  
  77.         final AutoParallaxBackground autoParallaxBackground = new AutoParallaxBackground(  
  78.                 0000);  
  79.   
  80.         // 0.0f,-0.5f,-10.0f:你可以把它们理解为相对位置差  
  81.         autoParallaxBackground  
  82.                 .attachParallaxEntity(new ParallaxEntity(0.0f,  
  83.                         new Sprite(0, CAMERA_HEIGHT  
  84.                                 - this.mParallaxLayerBack.getHeight(),  
  85.                                 this.mParallaxLayerBack,  
  86.                                 getVertexBufferObjectManager())));  
  87.         autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(-5.0f,  
  88.                 new Sprite(080this.mParallaxLayerMid,  
  89.                         getVertexBufferObjectManager())));  
  90.         autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(-10.0f,  
  91.                 new Sprite(0, CAMERA_HEIGHT  
  92.                         - this.mParallaxLayerFront.getHeight(),  
  93.                         this.mParallaxLayerFront,  
  94.                         getVertexBufferObjectManager())));  
  95.   
  96.         // 设置背景  
  97.         mScene.setBackground(autoParallaxBackground);  
  98.   
  99.         final int playerX = (int) (CAMERA_WIDTH - this.mPlayerTextureRegion  
  100.                 .getWidth()) / 2;  
  101.         final int playerY = (int) (CAMERA_HEIGHT  
  102.                 - this.mPlayerTextureRegion.getHeight() - 5);  
  103.         final AnimatedSprite player = new AnimatedSprite(playerX, playerY,  
  104.                 this.mPlayerTextureRegion, getVertexBufferObjectManager());  
  105.         player.setScaleCenterY(this.mPlayerTextureRegion.getHeight());  
  106.         player.setScale(2);  
  107.         player.animate(new long[] { 200200200 }, 35true);  
  108.   
  109.         mScene.attachChild(player);  
  110.   
  111.         // 我们新增加一个触摸事件监听  
  112.         mScene.setOnSceneTouchListener(new IOnSceneTouchListener() {  
  113.             public boolean onSceneTouchEvent(Scene pScene,  
  114.                     TouchEvent pSceneTouchEvent) {  
  115.                 switch (pSceneTouchEvent.getAction()) {  
  116.                 case TouchEvent.ACTION_UP:// 当触屏抬起的时候  
  117.                     // 如果在右边触摸,我们让屏幕向左滚动  
  118.                     if (pSceneTouchEvent.getX() > 400) {  
  119.                         // 设置每秒钟背景滚动的距离  
  120.                         autoParallaxBackground.setParallaxChangePerSecond(10);  
  121.                         // 设置一下小人的帧序列  
  122.                         player.animate(new long[] { 200200200 }, 35true);  
  123.                     }  
  124.                     // 如果在左边触摸,我们让屏幕向右滚动  
  125.                     else {  
  126.                         // 设置每秒钟背景滚动的距离  
  127.                         autoParallaxBackground.setParallaxChangePerSecond(-10);  
  128.                         // 设置一下小人的帧序列  
  129.                         player.animate(new long[] { 200200200 }, 911,  
  130.                                 true);  
  131.                     }  
  132.                     break;  
  133.                 }  
  134.                 return true;  
  135.             }  
  136.         });  
  137.         pOnCreateSceneCallback.onCreateSceneFinished(mScene);  
  138.     }  
  139.   
  140.     public void onPopulateScene(Scene pScene,  
  141.             OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {  
  142.         pOnPopulateSceneCallback.onPopulateSceneFinished();  
  143.     }  
  144. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值