andengine编程之sprite(四)

在andengine编程之sprite(三)中,我们介绍了一些sprite对位置的设置以及对触摸的监听。这次,我们要再来介绍介绍sprite对位置的设置,但这一次,我们要借助另一个方式——物理引擎。


PhysicsHandler是一个比较基本的物理类,它主要是对sprite设置移动速度,加速度,角速度等。
下面,我们先来看看它的一些方法:
速度:
public void setVelocityX(final float pVelocityX) 和public void setVelocityY(final float pVelocityY):分别对X方向和Y方向设置速度。
public void setVelocity(final float pVelocity) :X方向和Y方向设置同一数值的速度。
public void setVelocity(final float pVelocityX, final float pVelocityY)对X方向和Y方向设置速度。

加速度:
public void setAccelerationX(final float pAccelerationX)public void setAccelerationY(final float pAccelerationY):分别对X方向和Y方向设置加速度。
public void setAcceleration(final float pAcceleration):X方向和Y方向设置同一数值的速度。
public void setAcceleration(final float pAccelerationX, final float pAccelerationY):对X方向和Y方向设置加速度。
public void accelerate(final float pAccelerationX, final float pAccelerationY):对X方向和Y方向加速度数值做累加修正,例如加速度一直在提升,非匀加速状态。

角速度:
public void setAngularVelocity(final float pAngularVelocity):设置角速度。

重置,清零:
public void reset():将所有设置都设置为零,即重置。


接下来,我们用一张坦克的图片来实现一下精灵的移动:

[java]  view plain copy
  1. public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)  
  2.         throws Exception {  
  3.     Scene mScene = new Scene();  
  4.   
  5.     final AnimatedSprite tank = new AnimatedSprite(400200,  
  6.             mSpriteTiledTextureRegion, getVertexBufferObjectManager());  
  7.   
  8.     // 设置背景  
  9.     mScene.setBackground(background);  
  10.     mScene.attachChild(tank);  
  11.     // 注册精灵要实现触摸效果  
  12.     mScene.registerTouchArea(tank);  
  13.   
  14.     // 构建运动机制  
  15.     final PhysicsHandler physicsHandler = new PhysicsHandler(tank);  
  16.     tank.registerUpdateHandler(physicsHandler);  
  17.   
  18.     // 为场景注册触摸监听事件  
  19.     mScene.setOnSceneTouchListener(new IOnSceneTouchListener() {  
  20.         public boolean onSceneTouchEvent(Scene pScene,  
  21.                 TouchEvent pSceneTouchEvent) {  
  22.   
  23.             // 求得角度根据两个坐标,这里取一下坦克中心点作为坦克的坐标  
  24.             final float angleRad = MathUtils.atan2(  
  25.                     tank.getY() + tank.getHeight() / 2  
  26.                             - pSceneTouchEvent.getY(),  
  27.                     (tank.getX() + tank.getWidth() / 2)  
  28.                             - pSceneTouchEvent.getX());  
  29.   
  30.             // 求出移动的速度,这里也是根据三角形的计算法则,已知角度和总速度,求出X方向速度和Y方向速度  
  31.             // 100f:即为其总速度  
  32.             float VelocityX = FloatMath.cos(angleRad) * 100f;  
  33.             float VelocityY = FloatMath.sin(angleRad) * 100f;  
  34.   
  35.             switch (pSceneTouchEvent.getAction()) {  
  36.             // 这里,我们只取了按下抬起时的效果,方便我们观察  
  37.             case TouchEvent.ACTION_DOWN:  
  38.             case TouchEvent.ACTION_MOVE:  
  39.   
  40.                 // 为其设置X方向速度和Y方向速度  
  41.                 physicsHandler.setVelocity(-VelocityX, -VelocityY);  
  42.   
  43.                 // 为图片设置旋转角度(+ 90:为了修正旋转方向)  
  44.                 tank.setRotation(MathUtils.radToDeg(angleRad) + 90);  
  45.                 break;  
  46.             case TouchEvent.ACTION_UP:  
  47.                 // 松开按键后,将运动状态还原  
  48.                 physicsHandler.reset();  
  49.                 break;  
  50.             }  
  51.             return true;  
  52.         }  
  53.     });  
  54.     pOnCreateSceneCallback.onCreateSceneFinished(mScene);  
  55. }  

如果对上面提到的速度求法不太明白的话,我这里用图片示例来说明一下,以便大家理解:

通过精灵当前坐标和触摸点的坐标,我们可以求得两条直角边的长度:X方向touch.x-sprite.x和Y方向touch.y-sprite.y,然后求得当前所夹角度。

然后根据角度和速度,可以分别求的X方向速度和Y方向速度:即代码中所写的FloatMath.cos(angleRad) * 100f和FloatMath.sin(angleRad) * 100f。

源代码:
[java]  view plain copy
  1. package com.testsprite;  
  2.   
  3. import org.andengine.engine.camera.Camera;  
  4. import org.andengine.engine.handler.physics.PhysicsHandler;  
  5. import org.andengine.engine.options.EngineOptions;  
  6. import org.andengine.engine.options.EngineOptions.ScreenOrientation;  
  7. import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;  
  8. import org.andengine.entity.scene.IOnSceneTouchListener;  
  9. import org.andengine.entity.scene.Scene;  
  10. import org.andengine.entity.scene.background.RepeatingSpriteBackground;  
  11. import org.andengine.entity.sprite.AnimatedSprite;  
  12. import org.andengine.input.touch.TouchEvent;  
  13. import org.andengine.opengl.texture.TextureOptions;  
  14. import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;  
  15. import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;  
  16. import org.andengine.opengl.texture.atlas.bitmap.source.AssetBitmapTextureAtlasSource;  
  17. import org.andengine.opengl.texture.region.TiledTextureRegion;  
  18. import org.andengine.ui.activity.BaseGameActivity;  
  19. import org.andengine.util.math.MathUtils;  
  20.   
  21. import android.util.FloatMath;  
  22.   
  23. public class TestSprite extends BaseGameActivity {  
  24.     private static final int CAMERA_WIDTH = 800;  
  25.     private static final int CAMERA_HEIGHT = 480;  
  26.   
  27.     private RepeatingSpriteBackground background;  
  28.   
  29.     private TiledTextureRegion mSpriteTiledTextureRegion;  
  30.   
  31.     public EngineOptions onCreateEngineOptions() {  
  32.         Camera mCamera = new Camera(00, CAMERA_WIDTH, CAMERA_HEIGHT);  
  33.         EngineOptions mEngineOptions = new EngineOptions(true,  
  34.                 ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(),  
  35.                 mCamera);  
  36.         return mEngineOptions;  
  37.     }  
  38.   
  39.     public void onCreateResources(  
  40.             OnCreateResourcesCallback pOnCreateResourcesCallback)  
  41.             throws Exception {  
  42.   
  43.         this.background = new RepeatingSpriteBackground(800480,  
  44.                 getTextureManager(), AssetBitmapTextureAtlasSource.create(  
  45.                         this.getAssets(), "background.png"),  
  46.                 getVertexBufferObjectManager());  
  47.   
  48.         BitmapTextureAtlas mBitmapTextureAtlas = new BitmapTextureAtlas(  
  49.                 getTextureManager(), 128256, TextureOptions.DEFAULT);  
  50.         mSpriteTiledTextureRegion = BitmapTextureAtlasTextureRegionFactory  
  51.                 .createTiledFromAsset(mBitmapTextureAtlas, this"tank.png"0,  
  52.                         011);  
  53.         mBitmapTextureAtlas.load();  
  54.   
  55.         pOnCreateResourcesCallback.onCreateResourcesFinished();  
  56.     }  
  57.   
  58.     public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)  
  59.             throws Exception {  
  60.         Scene mScene = new Scene();  
  61.   
  62.         final AnimatedSprite tank = new AnimatedSprite(400200,  
  63.                 mSpriteTiledTextureRegion, getVertexBufferObjectManager());  
  64.   
  65.         // 设置背景  
  66.         mScene.setBackground(background);  
  67.         mScene.attachChild(tank);  
  68.         // 注册精灵要实现触摸效果  
  69.         mScene.registerTouchArea(tank);  
  70.   
  71.         // 构建运动机制  
  72.         final PhysicsHandler physicsHandler = new PhysicsHandler(tank);  
  73.         tank.registerUpdateHandler(physicsHandler);  
  74.   
  75.         // 为场景注册触摸监听事件  
  76.         mScene.setOnSceneTouchListener(new IOnSceneTouchListener() {  
  77.             public boolean onSceneTouchEvent(Scene pScene,  
  78.                     TouchEvent pSceneTouchEvent) {  
  79.   
  80.                 // 求得角度根据两个坐标,这里取一下坦克中心点作为坦克的坐标  
  81.                 final float angleRad = MathUtils.atan2(  
  82.                         tank.getY() + tank.getHeight() / 2  
  83.                                 - pSceneTouchEvent.getY(),  
  84.                         (tank.getX() + tank.getWidth() / 2)  
  85.                                 - pSceneTouchEvent.getX());  
  86.   
  87.                 // 求出移动的速度,这里也是根据三角形的计算法则,已知角度和总速度,求出X方向速度和Y方向速度  
  88.                 // 100f:即为其总速度  
  89.                 float VelocityX = FloatMath.cos(angleRad) * 100f;  
  90.                 float VelocityY = FloatMath.sin(angleRad) * 100f;  
  91.   
  92.                 switch (pSceneTouchEvent.getAction()) {  
  93.                 // 这里,我们只取了按下抬起时的效果,方便我们观察  
  94.                 case TouchEvent.ACTION_DOWN:  
  95.                 case TouchEvent.ACTION_MOVE:  
  96.   
  97.                     // 为其设置X方向速度和Y方向速度  
  98.                     physicsHandler.setVelocity(-VelocityX, -VelocityY);  
  99.   
  100.                     // 为图片设置旋转角度(+ 90:为了修正旋转方向)  
  101.                     tank.setRotation(MathUtils.radToDeg(angleRad) + 90);  
  102.                     break;  
  103.                 case TouchEvent.ACTION_UP:  
  104.                     // 松开按键后,将运动状态还原  
  105.                     physicsHandler.reset();  
  106.                     break;  
  107.                 }  
  108.                 return true;  
  109.             }  
  110.         });  
  111.         pOnCreateSceneCallback.onCreateSceneFinished(mScene);  
  112.     }  
  113.   
  114.     public void onPopulateScene(Scene pScene,  
  115.             OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {  
  116.         pOnPopulateSceneCallback.onPopulateSceneFinished();  
  117.     }  
  118. }  


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值