zoukankan      html  css  js  c++  java
  • 二:虚拟游戏摇杆

    andengine中绘制虚拟游戏摇杆非常简单,只需要实现AnalogOnScreenControl模拟摇杆类,在设置一些属性即可。先看效果图:

    左边的摇杆是控制精灵上下左右移动,右边的摇杆空值精灵的旋转。代码结构跟andengine学习系列二一样,其中很多注释在系列二中有说明,在该章内便不多复述。

    onLoadEngine()方法:

    1. @Override  
    2.     public Engine onLoadEngine() {  
    3.         this.mCamera = new Camera(00, CAMERA_WIDTH, CAMERA_HEIGHT);  
    4.         final Engine engine = new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));  
    5.   
    6.         try {       //因为有两个摇杆,需要两个手指同时进行,所以这里要注册多点触控  
    7.             if(MultiTouch.isSupported(this)) {  
    8.                 engine.setTouchController(new MultiTouchController());  
    9.                 if(MultiTouch.isSupportedDistinct(this)) {  
    10.                     Toast.makeText(this"MultiTouch detected --> Both controls will work properly!", Toast.LENGTH_LONG).show();  
    11.                 } else {  
    12.                     this.mPlaceOnScreenControlsAtDifferentVerticalLocations = true;  
    13.                     Toast.makeText(this"MultiTouch detected, but your device has problems distinguishing between fingers. Controls are placed at different vertical locations.", Toast.LENGTH_LONG).show();  
    14.                 }  
    15.             } else {  
    16.                 Toast.makeText(this"Sorry your device does NOT support MultiTouch! (Falling back to SingleTouch.) Controls are placed at different vertical locations.", Toast.LENGTH_LONG).show();  
    17.             }  
    18.         } catch (final MultiTouchException e) {  
    19.             Toast.makeText(this"Sorry your Android Version does NOT support MultiTouch! (Falling back to SingleTouch.) Controls are placed at different vertical locations.", Toast.LENGTH_LONG).show();  
    20.         }  
    21.   
    22.         return engine;  
    23.     }  


     

    onLoadResources()方法:


    1. public void onLoadResources() {  
    2.   
    3.         this.mTexture = new Texture(3232, TextureOptions.BILINEAR_PREMULTIPLYALPHA);  
    4.         this.mFaceTextureRegion = TextureRegionFactory.createFromAsset(this.mTexture, this"face_box.png"00);  
    5.   
    6.         this.mOnScreenControlTexture = new Texture(256128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);  
    7.         this.mOnScreenControlBaseTextureRegion = TextureRegionFactory.createFromAsset(this.mOnScreenControlTexture, this"onscreen_control_base.png"00);   //这里是加载摇杆的地盘的纹理图片   
    8.         this.mOnScreenControlKnobTextureRegion = TextureRegionFactory.createFromAsset(this.mOnScreenControlTexture, this"onscreen_control_knob.png"1280); //这里是加载摇杆的纹理图片  
    9.   
    10.         this.mEngine.getTextureManager().loadTextures(this.mTexture, this.mOnScreenControlTexture);  
    11.     }  


    onLoadScene()方法,关键的业务逻辑便在该方法中:


      1. public Scene onLoadScene() {  
      2.         this.mEngine.registerUpdateHandler(new FPSLogger());  
      3.   
      4.         final Scene scene = new Scene(1);  
      5.         scene.setBackground(new ColorBackground(0.09804f, 0.6274f, 0.8784f));  
      6.   
      7.         final int centerX = (CAMERA_WIDTH - this.mFaceTextureRegion.getWidth()) / 2;  
      8.         final int centerY = (CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight()) / 2;  
      9.         final Sprite face = new Sprite(centerX, centerY, this.mFaceTextureRegion);  
      10.   
      11.         scene.getTopLayer().addEntity(face);  
      12.   
      13. //-------------------------------------------以下为左摇杆的实现----------------------------------------------------------------------                                                                final int x1 = 0;                                                   //y坐标为屏幕的高度减去摇杆底盘的高度,注意屏幕在前面已经被强制横屏  
      14.         final int y1 = CAMERA_HEIGHT - this.mOnScreenControlBaseTextureRegion.getHeight();                                                                              //AnalogOnScreenControl构造方法中:第一第二参数是该摇杆的坐标,第三个参数为上面定义camera,第四第五个参数为摇杆底盘和摇杆的纹理区域,第六个参数为pTimeBetweenUpdates界面的更新  
      15.         final AnalogOnScreenControl velocityOnScreenControl = new AnalogOnScreenControl(x1, y1, this.mCamera, this.mOnScreenControlBaseTextureRegion, this.mOnScreenControlKnobTextureRegion, 0.1f, new IAnalogOnScreenControlListener() {                                          //备注1  
      16.             @Override  
      17.             public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) {  
      18.                 Log.i("test","x1:"+x1+",y1:"+y1+",pValueX:"+pValueX+",pValueY:"+pValueY);  
      19.                 face.setVelocity(pValueX * 100, pValueY * 100); //备注2  
      20.             }  
      21.   
      22.             @Override  
      23.             public void onControlClick(final AnalogOnScreenControl pAnalogOnScreenControl) {    //备注3  
      24.                 /* Nothing. */  
      25.             }  
      26.         });  
      27.         velocityOnScreenControl.getControlBase().setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);                                          //备注4  
      28.         velocityOnScreenControl.getControlBase().setAlpha(0.5f);  
      29.   
      30.         scene.setChildScene(velocityOnScreenControl);  
      31. //-------------------------------------------------------end 坐摇杆的实现------------------------------------------------------------//-------------------------------------------------------以下为右摇杆的实现----------------------------------------------------------  
      32.   
      33.         final int y2 = (this.mPlaceOnScreenControlsAtDifferentVerticalLocations) ? 0 : y1;  
      34.         final int x2 = CAMERA_WIDTH - this.mOnScreenControlBaseTextureRegion.getWidth();  
      35.         final AnalogOnScreenControl rotationOnScreenControl = new AnalogOnScreenControl(x2, y2, this.mCamera, this.mOnScreenControlBaseTextureRegion, this.mOnScreenControlKnobTextureRegion, 0.1f, new IAnalogOnScreenControlListener() {  
      36.             @Override  
      37.             public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) {  
      38.                 Log.i("test","x2:"+x2+",y2:"+y2+",pValueX:"+pValueX+",pValueY:"+pValueY);  
      39.                 if(pValueX == x1 && pValueY == x1) {  
      40.                     face.setRotation(x1);  
      41.                 } else {  
      42.                     face.setRotation(MathUtils.radToDeg((float)Math.atan2(pValueX, -pValueY)));   
      43.                                 //备注5  
      44.                 }  
      45.             }  
      46.   
      47.             @Override  
      48.             public void onControlClick(final AnalogOnScreenControl pAnalogOnScreenControl) {  
      49.                 /* Nothing. */  
      50.             }  
      51.         });  
      52.         rotationOnScreenControl.getControlBase().setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);  
      53.         rotationOnScreenControl.getControlBase().setAlpha(0.5f);  
      54.   
      55.         velocityOnScreenControl.setChildScene(rotationOnScreenControl);     //备注6  
      56.   
      57.         return scene;  
      58.     }                                                       //---------------------------------------------end 右摇杆的实现-------
  • 相关阅读:
    星辰小队针对于软件“星遇”的10天冲刺——第2天
    石家庄地铁路线安排网站的最终版本——博客登记
    星辰小队针对于软件“星遇”的10天冲刺——第1天
    单词统计——基本Java实现(无特殊设置)
    周周总结——时时更新(第4学期,第10周)
    构建之法阅读笔记04
    用户模板和用户场景(星遇)
    周周总结——时时更新(第4学期,第9周)
    ios创建bundle的图片资源文件(转)
    GCD的多线程实现方式,线程和定时器混合使用
  • 原文地址:https://www.cnblogs.com/zhusd/p/3146158.html
Copyright © 2011-2022 走看看