基于微云游戏引擎的游戏开发——《猴子跳》

来源:互联网 发布:bluetooth网络连接红叉 编辑:IT博客网 时间:2020/02/25 15:28

原文地址:http://blog.csdn.net/zhang463586719/article/details/6643359

 

本人android开发新手,借博客大赛希望和各位开发者共同交流下android游戏的开发心得。本人前几个练手的游戏都没有使用游戏引擎,开发出来效果一般般,之后便尝试了使用游戏引擎,第一款游戏引擎就是用的微云,之所以用微云,因为微云提供了一个不错的demo可以作为很好的功能演示,非常的直观,但是其实例确实不足,应该说在我学习微云引擎的时候,只有demo源代码可以供你参考的。但是这一切不能否认微云是一个很好的游戏引擎的事实,尤其是它整合的box2d物理引擎用起来也非常的顺手。


那么下面我就简单来介绍下我做的这个小游戏《猴子跳》,整个游戏风格简单,有点类似《doodle jump》

效果图如下:


 





gameview.java

 

view plain
  1. import com.wiyun.engine.nodes.Director;  
  2. import com.wiyun.engine.nodes.Scene;  
  3. import com.wiyun.engine.nodes.Director.IDirectorLifecycleListener;  
  4. import com.wiyun.engine.opengl.WYGLSurfaceView;  
  5. import com.wiyun.engine.transitions.JumpZoomTransition;  
  6.   
  7. import android.app.Activity;  
  8. import android.media.MediaPlayer;  
  9. import android.os.Bundle;  
  10. import android.view.Window;  
  11. import android.view.WindowManager.LayoutParams;  
  12.   
  13. public class GameView extends Activity implements IDirectorLifecycleListener {  
  14.     /** Called when the activity is first created. */  
  15.     protected WYGLSurfaceView mGLSurfaceView;  
  16.     static GameView gameview;  
  17.     static int fenshu, score;  
  18.     protected Scene mScene;  
  19.     static {  
  20.         System.loadLibrary("xml2");  
  21.         System.loadLibrary("wiengine");  
  22.         System.loadLibrary("lua");  
  23.         System.loadLibrary("chipmunk");  
  24.     }  
  25.     MediaPlayer mMediaPlayer, mMediaPlayer2;  
  26.   
  27.     @Override  
  28.     public void onCreate(Bundle savedInstanceState) {  
  29.         super.onCreate(savedInstanceState);  
  30.         getWindow().addFlags(LayoutParams.FLAG_FULLSCREEN);  
  31.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  32.         mGLSurfaceView = new WYGLSurfaceView(this);  
  33.         gameview = this;  
  34.         setContentView(mGLSurfaceView);  
  35.         // 设置显示帧率,程序发布时应该去掉  
  36.         Director.getInstance().setDisplayFPS(true);  
  37.   
  38.         // 创建第一个场景  
  39.         menu a = new menu();  
  40.           
  41.         mMediaPlayer = new MediaPlayer();  
  42.         // 添加一个生命周期监听器,如果不需要可以不添加  
  43.         Director.getInstance().addLifecycleListener(this);  
  44.   
  45.         // 开始运行第一个场景  
  46.         Director.getInstance().runWithScene(a.mScene);  
  47.   
  48.     }  
  49.   
  50.     public void play1() {  
  51.           
  52.         mMediaPlayer = MediaPlayer.create(GameView.this, R.raw.jump);  
  53.         mMediaPlayer.setLooping(false);  
  54.         mMediaPlayer.start();  
  55.     }  
  56.     public void play2() {  
  57.         mMediaPlayer = MediaPlayer.create(GameView.this, R.raw.boost);  
  58.         mMediaPlayer.setLooping(false);  
  59.         mMediaPlayer.start();  
  60.     }  
  61.     public void play3() {  
  62.         mMediaPlayer = MediaPlayer.create(GameView.this, R.raw.jump2);  
  63.         mMediaPlayer.setLooping(false);  
  64.         mMediaPlayer.start();  
  65.     }  
  66.   
  67.     public void change() {  
  68.         main a = new main();  
  69.         Director.getInstance().replaceScene(  
  70.                 JumpZoomTransition.make(1, a.mScene));  
  71.         number n = new number();  
  72.         a.mScene.addChild(n);  
  73.     }  
  74.   
  75.     @Override  
  76.     protected void onPause() {  
  77.         super.onPause();  
  78.   
  79.         Director.getInstance().pause();  
  80.     }  
  81.   
  82.     @Override  
  83.     protected void onResume() {  
  84.         super.onResume();  
  85.   
  86.         Director.getInstance().resume();  
  87.     }  
  88.   
  89.     @Override  
  90.     protected void onDestroy() {  
  91.         super.onDestroy();  
  92.         Director.getInstance().end();  
  93.   
  94.     }  
  95.   
  96.     @Override  
  97.     public void onDirectorEnded() {  
  98.         // TODO Auto-generated method stub  
  99.   
  100.     }  
  101.   
  102.     @Override  
  103.     public void onDirectorPaused() {  
  104.         // TODO Auto-generated method stub  
  105.   
  106.     }  
  107.   
  108.     @Override  
  109.     public void onDirectorResumed() {  
  110.         // TODO Auto-generated method stub  
  111.   
  112.     }  
  113.   
  114.     @Override  
  115.     public void onDirectorScreenCaptured(String arg0) {  
  116.         // TODO Auto-generated method stub  
  117.   
  118.     }  
  119.   
  120.     @Override  
  121.     public void onSurfaceChanged(int arg0, int arg1) {  
  122.         // TODO Auto-generated method stub  
  123.   
  124.     }  
  125.   
  126.     @Override  
  127.     public void onSurfaceCreated() {  
  128.         // TODO Auto-generated method stub  
  129.   
  130.     }  
  131.   
  132.     @Override  
  133.     public void onSurfaceDestroyed() {  
  134.         // TODO Auto-generated method stub  
  135.   
  136.     }  
  137. }  



 

菜单栏 menu.java

 

view plain
  1. import com.wiyun.engine.motionwelder.MWSprite;  
  2. import com.wiyun.engine.nodes.Director;  
  3. import com.wiyun.engine.nodes.Layer;  
  4. import com.wiyun.engine.nodes.Menu;  
  5. import com.wiyun.engine.nodes.MenuItemSprite;  
  6. import com.wiyun.engine.nodes.Scene;  
  7. import com.wiyun.engine.nodes.Sprite;  
  8. import com.wiyun.engine.opengl.Texture2D;  
  9. import com.wiyun.engine.particle.ParticleSystem;  
  10. import com.wiyun.engine.types.WYRect;  
  11. import com.wiyun.engine.types.WYSize;  
  12. import com.wiyun.engine.utils.ResolutionIndependent;  
  13.   
  14. public class menu extends Layer {  
  15.     public Scene mScene;  
  16.     WYSize s;  
  17.     MWSprite m_sprite1;  
  18.     ParticleSystem emitter;  
  19.   
  20.     menu() {  
  21.         s = Director.getInstance().getWindowSize();  
  22.         mScene = Scene.make();  
  23.   
  24.         bc();  
  25.         s();  
  26.         m();  
  27.   
  28.     }  
  29.   
  30.     public void bc() {  
  31.         Texture2D bc = Texture2D.makePNG(R.drawable.bc);  
  32.   
  33.         Sprite bc2 = Sprite.make(bc);  
  34.   
  35.         bc.autoRelease();  
  36.         bc2.autoRelease();  
  37.         bc2.setPosition(bc2.getWidth() / 2, bc2.getHeight() / 2);  
  38.         mScene.addChild(bc2);  
  39.     }  
  40.   
  41.     public void s() {  
  42.         emitter = ParticleSnow.make();  
  43.         emitter.setPosition(s.width / 2, s.height);  
  44.         emitter.autoRelease();  
  45.         mScene.addChild(emitter);  
  46.     }  
  47.   
  48.     public void m() {  
  49.   
  50.         Texture2D tex = Texture2D.makePNG(R.drawable.start_normal);  
  51.         Texture2D tex2 = Texture2D.makePNG(R.drawable.start_down);  
  52.         Sprite sprite1Normal = Sprite.make(tex, ResolutionIndependent  
  53.                 .resolve(WYRect.make(009629)));  
  54.         Sprite sprite1Disabled = Sprite.make(tex2, ResolutionIndependent  
  55.                 .resolve(WYRect.make(009629)));  
  56.         MenuItemSprite start = MenuItemSprite.make(sprite1Normal,  
  57.                 sprite1Disabled, sprite1Disabled, this"start");  
  58.   
  59.         tex = Texture2D.makePNG(R.drawable.about_normal);  
  60.         tex2 = Texture2D.makePNG(R.drawable.about_down);  
  61.         sprite1Normal = Sprite.make(tex, ResolutionIndependent.resolve(WYRect  
  62.                 .make(009629)));  
  63.         sprite1Disabled = Sprite.make(tex2, ResolutionIndependent  
  64.                 .resolve(WYRect.make(009629)));  
  65.         MenuItemSprite about = MenuItemSprite.make(sprite1Normal,  
  66.                 sprite1Disabled, sprite1Disabled);  
  67.   
  68.         tex = Texture2D.makePNG(R.drawable.quit_normal);  
  69.         tex2 = Texture2D.makePNG(R.drawable.quit_down);  
  70.         sprite1Normal = Sprite.make(tex, ResolutionIndependent.resolve(WYRect  
  71.                 .make(009629)));  
  72.         sprite1Disabled = Sprite.make(tex2, ResolutionIndependent  
  73.                 .resolve(WYRect.make(009629)));  
  74.         MenuItemSprite quit = MenuItemSprite.make(sprite1Normal,  
  75.                 sprite1Disabled, sprite1Disabled, this"quit");  
  76.   
  77.         Menu menu = Menu.make(start, about, quit);  
  78.         menu.alignItemsVertically(40);  
  79.         menu.setPosition(s.width / 2, s.height / 2);  
  80.         mScene.addChild(menu);  
  81.     }  
  82.   
  83.     public void quit() {  
  84.         GameView.gameview.onDestroy();  
  85.     }  
  86.   
  87.     public void start() {  
  88.         GameView.gameview.change();  
  89.     }  
  90.   
  91. }  


游戏大背景的设置,游戏的大背景由36张图片拼接而成的

BC.java

 

view plain
  1. import com.wiyun.engine.box2d.Box2D;  
  2. import com.wiyun.engine.box2d.Box2DRender;  
  3. import com.wiyun.engine.box2d.collision.PolygonShape;  
  4. import com.wiyun.engine.box2d.dynamics.Body;  
  5. import com.wiyun.engine.box2d.dynamics.BodyDef;  
  6. import com.wiyun.engine.box2d.dynamics.Fixture;  
  7. import com.wiyun.engine.box2d.dynamics.World;  
  8. import com.wiyun.engine.box2d.dynamics.World.IContactListener;  
  9. import com.wiyun.engine.box2d.dynamics.contacts.Contact;  
  10. import com.wiyun.engine.nodes.Director;  
  11. import com.wiyun.engine.nodes.Layer;  
  12. import com.wiyun.engine.opengl.Texture2D;  
  13. import com.wiyun.engine.types.WYPoint;  
  14. import com.wiyun.engine.types.WYSize;  
  15. import com.wiyun.engine.utils.TargetSelector;  
  16.   
  17. public class BC extends Layer implements IContactListener {  
  18.     static World mWorld;  
  19.   
  20.     Fixture bc1, bc2;  
  21.     WYSize s;  
  22.     Box2DRender render;  
  23.     float y1 = +17.8f, y2 = 0;  
  24.     static int chushi = 30;  
  25.     static boolean grass1 = false, grass2 = false, grass3 = false;  
  26.     protected Box2D mBox2D;  
  27.   
  28.     BC() {  
  29.         mBox2D = Box2D.make();  
  30.   
  31.         mBox2D.setDebugDraw(false);  
  32.         render = Box2DRender.make();  
  33.         mBox2D.setBox2DRender(render);  
  34.   
  35.         mWorld = mBox2D.getWorld();  
  36.   
  37.         s = Director.getInstance().getWindowSize();  
  38.         addChild(mBox2D);  
  39.         y1 = s.height / s.width * 10f;  
  40.         // set gravity  
  41.         mWorld.setGravity(04);// 47  
  42.   
  43.         {  
  44.             BodyDef bd = BodyDef.make();  
  45.             bd.setType(Body.TYPE_DYNAMIC);  
  46.             bd.setPosition(0, y1);  
  47.             Body body = mWorld.createBody(bd);  
  48.             bd.destroy();  
  49.             PolygonShape shape = PolygonShape.make();  
  50.             shape.setAsBox(10f, s.height / s.width * 10f);  
  51.             int id = id(chushi);  
  52.             bc1 = body.createFixture(shape, 0.0f);  
  53.             Texture2D tex = Texture2D.makePNG(id);  
  54.             render.bindTexture(bc1, tex);  
  55.             tex.autoRelease();  
  56.         }  
  57.         {  
  58.             BodyDef bd = BodyDef.make();  
  59.             bd.setType(Body.TYPE_DYNAMIC);  
  60.             bd.setPosition(0, y1 + s.height / s.width * 20 - 0.26f);  
  61.             Body body = mWorld.createBody(bd);  
  62.             bd.destroy();  
  63.             PolygonShape shape = PolygonShape.make();  
  64.             shape.setAsBox(10f, s.height / s.width * 10f);  
  65.   
  66.             bc2 = body.createFixture(shape, 0.0f);  
  67.             chushi--;  
  68.             int id = id(chushi);  
  69.             Texture2D tex = Texture2D.makePNG(id);  
  70.             render.bindTexture(bc2, tex);  
  71.             tex.autoRelease();  
  72.         }  
  73.   
  74.         // place box2d to center of bottom edge  
  75.         mBox2D.setPosition(s.width / 20);  
  76.         mWorld.setContactListener(this);  
  77.   
  78.         // add contact listener  
  79.         schedule(new TargetSelector(this"update(float)"new Object[] { 0f }));  
  80.     }  
  81.   
  82.     public void update(float delta) {  
  83.         mWorld.step(1.f / 60.f, 1010);  
  84.         mWorld.clearForces();  
  85.         if (Box2.collision == true) {  
  86.             bc1.getBody().setLinearVelocity(  
  87.                     WYPoint.make(0f, -2.56f * Box2.jump1));  
  88.             bc2.getBody().setLinearVelocity(  
  89.                     WYPoint.make(0f, -2.56f * Box2.jump1));  
  90.             Box2.collision = false;  
  91.         }  
  92.         if (bc1.getBody().getPosition().y < -y1 + 0.26f) {  
  93.             int id = id(chushi);  
  94.             Texture2D tex = Texture2D.makePNG(id);  
  95.             render.bindTexture(bc1, tex);  
  96.             tex.autoRelease();  
  97.   
  98.             chushi--;  
  99.             id = id(chushi);  
  100.             tex = Texture2D.makePNG(id);  
  101.             render.bindTexture(bc2, tex);  
  102.             tex.autoRelease();  
  103.             bc1.getBody().setTransform(0, y1, 0);  
  104.             bc2.getBody().setTransform(0,  
  105.                     y1 + s.height / s.width * 20f - 0.26f, 0);// 35  
  106.             grass1 = true;  
  107.             grass2 = true;  
  108.             grass3 = true;  
  109.         }  
  110.     }  
  111.   
  112.     public int id(int a) {  
  113.         int id = 0;  
  114.         switch (a) {  
  115.         case 1:  
  116.             id = R.drawable.background_1_1;  
  117.             break;  
  118.         case 2:  
  119.             id = R.drawable.background_2_2;  
  120.             break;  
  121.         case 3:  
  122.             id = R.drawable.background_3_3;  
  123.             break;  
  124.         case 4:  
  125.             id = R.drawable.background_4_4;  
  126.             break;  
  127.         case 5:  
  128.             id = R.drawable.background_5_5;  
  129.             break;  
  130.         case 6:  
  131.             id = R.drawable.background_6_6;  
  132.             break;  
  133.         case 7:  
  134.             id = R.drawable.background_7_7;  
  135.             break;  
  136.         case 8:  
  137.             id = R.drawable.background_8_8;  
  138.             break;  
  139.         case 9:  
  140.             id = R.drawable.background_9_9;  
  141.             break;  
  142.         case 10:  
  143.             id = R.drawable.background_10_10;  
  144.             break;  
  145.         case 11:  
  146.             id = R.drawable.background_11_11;  
  147.             break;  
  148.         case 12:  
  149.             id = R.drawable.background_12_12;  
  150.             break;  
  151.         case 13:  
  152.             id = R.drawable.background_13_13;  
  153.             break;  
  154.         case 14:  
  155.             id = R.drawable.background_14_14;  
  156.             break;  
  157.         case 15:  
  158.             id = R.drawable.background_15_15;  
  159.             break;  
  160.         case 16:  
  161.             id = R.drawable.background_16_16;  
  162.             break;  
  163.         case 17:  
  164.             id = R.drawable.background_17_17;  
  165.             break;  
  166.         case 18:  
  167.             id = R.drawable.background_18_18;  
  168.             break;  
  169.         case 19:  
  170.             id = R.drawable.background_19_19;  
  171.             break;  
  172.         case 20:  
  173.             id = R.drawable.background_20_20;  
  174.             break;  
  175.         case 21:  
  176.             id = R.drawable.background_21_21;  
  177.             break;  
  178.         case 22:  
  179.             id = R.drawable.background_22_22;  
  180.             break;  
  181.         case 23:  
  182.             id = R.drawable.background_23_23;  
  183.             break;  
  184.         case 24:  
  185.             id = R.drawable.background_24_24;  
  186.             break;  
  187.         case 25:  
  188.             id = R.drawable.background_25_25;  
  189.             break;  
  190.         case 26:  
  191.             id = R.drawable.background_26_26;  
  192.             break;  
  193.         case 27:  
  194.             id = R.drawable.background_27_27;  
  195.             break;  
  196.         case 28:  
  197.             id = R.drawable.background_28_28;  
  198.             break;  
  199.         case 29:  
  200.             id = R.drawable.background_29_29;  
  201.             break;  
  202.         case 30:  
  203.             id = R.drawable.background_30_30;  
  204.             break;  
  205.         }  
  206.   
  207.         return id;  
  208.   
  209.     }  
  210.   
  211.     @Override  
  212.     public void beginContact(int contactPointer) {  
  213.         // TODO Auto-generated method stub  
  214.         Contact contact = Contact.from(contactPointer);  
  215.         contact.setEnabled(false);  
  216.     }  
  217.   
  218.     @Override  
  219.     public void endContact(int contactPointer) {  
  220.         // TODO Auto-generated method stub  
  221.         Contact contact = Contact.from(contactPointer);  
  222.         contact.setEnabled(false);  
  223.     }  
  224.   
  225.     @Override  
  226.     public void postSolve(int contactPointer, int arg1) {  
  227.         // TODO Auto-generated method stub  
  228.         Contact contact = Contact.from(contactPointer);  
  229.         contact.setEnabled(false);  
  230.     }  
  231.   
  232.     @Override  
  233.     public void preSolve(int contactPointer, int arg1) {  
  234.         // TODO Auto-generated method stub  
  235.         Contact contact = Contact.from(contactPointer);  
  236.         contact.setEnabled(false);  
  237.     }  
  238. }  


第二层的设计,grass.java

 

view plain
  1. import com.wiyun.engine.box2d.Box2D;  
  2. import com.wiyun.engine.box2d.Box2DRender;  
  3. import com.wiyun.engine.box2d.FixtureAnimation;  
  4. import com.wiyun.engine.box2d.collision.PolygonShape;  
  5. import com.wiyun.engine.box2d.dynamics.Body;  
  6. import com.wiyun.engine.box2d.dynamics.BodyDef;  
  7. import com.wiyun.engine.box2d.dynamics.Fixture;  
  8. import com.wiyun.engine.box2d.dynamics.World;  
  9. import com.wiyun.engine.box2d.dynamics.World.IContactListener;  
  10. import com.wiyun.engine.box2d.dynamics.contacts.Contact;  
  11. import com.wiyun.engine.nodes.Director;  
  12. import com.wiyun.engine.nodes.Layer;  
  13. import com.wiyun.engine.opengl.Texture2D;  
  14. import com.wiyun.engine.types.WYPoint;  
  15. import com.wiyun.engine.types.WYSize;  
  16. import com.wiyun.engine.utils.TargetSelector;  
  17.   
  18. public class grass extends Layer implements IContactListener {  
  19.     static World mWorld;  
  20.   
  21.     Fixture grass1, grass2, grass3; // 漂浮物1,2,3(因为一开始只设计了了草,现在更改了)  
  22.     float grassx1, grassx2, grassx3;// 漂浮物1,2,3的横向移动速度,因为有云和草什么的,要具有不同的速度撒(因为一开始只设计了了草,现在更改了)  
  23.     float grassy1, grassy2, grassy3;// 漂浮物1,2,3的纵向移动速度  
  24.   
  25.     Fixture bc1, bc2;  
  26.     WYSize s;  
  27.     Box2DRender render;  
  28.     float y1 = +17.8f, y2 = 0;  
  29.     protected Box2D mBox2D;  
  30.   
  31.     grass() {  
  32.         mBox2D = Box2D.make();  
  33.   
  34.         mBox2D.setDebugDraw(false);  
  35.         render = Box2DRender.make();  
  36.         mBox2D.setBox2DRender(render);  
  37.   
  38.         mWorld = mBox2D.getWorld();  
  39.   
  40.         s = Director.getInstance().getWindowSize();  
  41.         addChild(mBox2D);  
  42.         // set gravity  
  43.         mWorld.setGravity(030);// 47  
  44.   
  45.         first();  
  46.   
  47.         // place box2d to center of bottom edge  
  48.         mBox2D.setPosition(s.width / 20);  
  49.         mWorld.setContactListener(this);  
  50.   
  51.         // add contact listener  
  52.         schedule(new TargetSelector(this"update(float)"new Object[] { 0f }));  
  53.     }  
  54.   
  55.     public void first() {  
  56.         {  
  57.             BodyDef bd = BodyDef.make();  
  58.             bd.setType(Body.TYPE_DYNAMIC);  
  59.             bd.setPosition(-55);  
  60.             Body body = mWorld.createBody(bd);  
  61.             bd.destroy();  
  62.             PolygonShape shape = PolygonShape.make();  
  63.             shape.setAsBox(5f, 10f);  
  64.   
  65.             grass1 = body.createFixture(shape, 0.0f);  
  66.             Texture2D tex = Texture2D.makePNG(R.drawable.branch_upper_flip);  
  67.             tex.autoRelease();  
  68.             render.bindTexture(grass1, tex);  
  69.         }  
  70.         {  
  71.             BodyDef bd = BodyDef.make();  
  72.             bd.setType(Body.TYPE_DYNAMIC);  
  73.             bd.setPosition(065);  
  74.             Body body = mWorld.createBody(bd);  
  75.             bd.destroy();  
  76.             PolygonShape shape = PolygonShape.make();  
  77.             shape.setAsBox(12f, 12f);  
  78.   
  79.             grass2 = body.createFixture(shape, 0.0f);  
  80.             Texture2D tex = Texture2D.makePNG(R.drawable.branch_lower);  
  81.             tex.autoRelease();  
  82.             render.bindTexture(grass2, tex);  
  83.         }  
  84.         {  
  85.             BodyDef bd = BodyDef.make();  
  86.             bd.setType(Body.TYPE_DYNAMIC);  
  87.             bd.setPosition(0120);  
  88.             Body body = mWorld.createBody(bd);  
  89.             bd.destroy();  
  90.             PolygonShape shape = PolygonShape.make();  
  91.             shape.setAsBox(13f, 13f);  
  92.   
  93.             grass3 = body.createFixture(shape, 0.0f);  
  94.             Texture2D tex = Texture2D.makePNG(R.drawable.vines);  
  95.             render.bindTexture(grass3, tex);  
  96.             tex.autoRelease();  
  97.         }  
  98.     }  
  99.   
  100.     public void update(float delta) {  
  101.         mWorld.step(1.f / 60.f, 1010);  
  102.         mWorld.clearForces();  
  103.         if (Box2.collision2 == true) {  
  104.             grass1.getBody().setLinearVelocity(  
  105.                     WYPoint.make(grassx1, Box2.jump1 * (-19.14f + grassy1)));  
  106.             grass2.getBody().setLinearVelocity(  
  107.                     WYPoint.make(grassx2, Box2.jump1 * (-19.14f + grassy2)));  
  108.             grass3.getBody().setLinearVelocity(  
  109.                     WYPoint.make(grassx3, Box2.jump1 * (-19.14f + grassy3)));  
  110.             Box2.collision2 = false;  
  111.         }  
  112.         if (grass1.getBody().getPosition().y < -80) {  
  113.             float a = grass1.getBody().getPosition().x;  
  114.             grass1.getBody().setTransform(a, 800);  
  115.   
  116.             if (BC.grass1 == true) {  
  117.                 BC.grass1 = false;  
  118.                 if (BC.chushi < 29 && BC.chushi > 27) {  
  119.                     Texture2D tex = Texture2D.makePNG(R.drawable.noimage);  
  120.                     render.bindTexture(grass1, tex);  
  121.                     tex.autoRelease();  
  122.                 } else if (BC.chushi == 25) {  
  123.                     Texture2D tex = Texture2D.makePNG(R.drawable.building);  
  124.                     render.bindTexture(grass1, tex);  
  125.                     tex.autoRelease();  
  126.                 } else if (BC.chushi == 23) {  
  127.                     Texture2D tex = Texture2D.makePNG(R.drawable.rock1);  
  128.                     render.bindTexture(grass1, tex);  
  129.                     tex.autoRelease();  
  130.                 } else {  
  131.                     Texture2D tex = Texture2D.makePNG(R.drawable.noimage);  
  132.                     render.bindTexture(grass1, tex);  
  133.                     tex.autoRelease();  
  134.                 }  
  135.             }  
  136.         }  
  137.         if (grass2.getBody().getPosition().y < -80) {  
  138.             float a = grass2.getBody().getPosition().x;  
  139.             grass2.getBody().setTransform(a, 800);  
  140.             if (BC.grass2 == true) {  
  141.                 BC.grass2 = false;  
  142.                 if (BC.chushi == 28) {  
  143.   
  144.                     Texture2D tex = Texture2D.makePNG(R.drawable.branch_blue);  
  145.                     render.bindTexture(grass2, tex);  
  146.                     tex.autoRelease();  
  147.                 } else if (BC.chushi == 26 && BC.chushi == 27) {  
  148.                     Texture2D tex = Texture2D.makePNG(R.drawable.cloud_white_2);  
  149.                     render.bindTexture(grass2, tex);  
  150.                     grassx2 = -3f;  
  151.                     tex.autoRelease();  
  152.                 } else {  
  153.                     Texture2D tex = Texture2D.makePNG(R.drawable.noimage);  
  154.                     render.bindTexture(grass2, tex);  
  155.                     tex.autoRelease();  
  156.                 }  
  157.             }  
  158.   
  159.         }  
  160.         if (grass3.getBody().getPosition().y < -80) {  
  161.             float a = grass3.getBody().getPosition().x;  
  162.             if (BC.chushi != 23) {  
  163.                 grass3.getBody().setTransform(a, 800);  
  164.             } else {  
  165.                 grass3.getBody().setTransform(a, 2800);  
  166.             }  
  167.   
  168.             if (BC.grass3 == true) {  
  169.                 BC.grass3 = false;  
  170.                 if (BC.chushi == 28) {  
  171.   
  172.                     Texture2D tex = Texture2D.makePNG(R.drawable.bridge);  
  173.                     render.bindTexture(grass3, tex);  
  174.                     tex.autoRelease();  
  175.                 } else if (BC.chushi == 25 || BC.chushi == 24  
  176.                         || BC.chushi == 26 || BC.chushi == 27) {  
  177.                     Texture2D tex = Texture2D.makePNG(R.drawable.cloud_city);  
  178.                     render.bindTexture(grass3, tex);  
  179.                     tex.autoRelease();  
  180.                 } else if (BC.chushi == 23) {  
  181.                     Texture2D tex = Texture2D  
  182.                             .makePNG(R.drawable.asteroids_deep);  
  183.                     render.bindTexture(grass3, tex);  
  184.                     grassy3 = -40f;  
  185.                     tex.autoRelease();  
  186.                 } else if (BC.chushi == 22) {  
  187.                     Texture2D tex = Texture2D.makePNG(R.drawable.yellow_clouds);  
  188.                     render.bindTexture(grass3, tex);  
  189.                     grassy3 = -5f;  
  190.                     tex.autoRelease();  
  191.                 } else if (BC.chushi == 19) {  
  192.                     Texture2D tex = Texture2D.makePNG(R.drawable.cloud_white_1);  
  193.                     render.bindTexture(grass3, tex);  
  194.                     grassx3 = 4f;  
  195.                     grassy3 = 0f;  
  196.                     tex.autoRelease();  
  197.                 }  
  198.             }  
  199.         }  
  200.         if (grass3.getBody().getPosition().x < -24) {  
  201.             float a = grass3.getBody().getPosition().y;  
  202.             grass3.getBody().setTransform(24, a, 0);  
  203.         }  
  204.         if (grass3.getBody().getPosition().x > 24) {  
  205.             float a = grass3.getBody().getPosition().y;  
  206.             grass3.getBody().setTransform(-24, a, 0);  
  207.         }  
  208.         if (grass2.getBody().getPosition().x < -24) {  
  209.             float a = grass2.getBody().getPosition().y;  
  210.             grass2.getBody().setTransform(24, a, 0);  
  211.         }  
  212.         if (grass2.getBody().getPosition().x > 24) {  
  213.             float a = grass2.getBody().getPosition().y;  
  214.             grass2.getBody().setTransform(-24, a, 0);  
  215.         }  
  216.         if (grass1.getBody().getPosition().x < -24) {  
  217.             float a = grass1.getBody().getPosition().y;  
  218.             grass1.getBody().setTransform(24, a, 0);  
  219.         }  
  220.         if (grass1.getBody().getPosition().x > 24) {  
  221.             float a = grass1.getBody().getPosition().y;  
  222.             grass1.getBody().setTransform(-24, a, 0);  
  223.         }  
  224.     }  
  225.   
  226.     @Override  
  227.     public void beginContact(int contactPointer) {  
  228.         // TODO Auto-generated method stub  
  229.         Contact contact = Contact.from(contactPointer);  
  230.         contact.setEnabled(false);  
  231.     }  
  232.   
  233.     @Override  
  234.     public void endContact(int contactPointer) {  
  235.         // TODO Auto-generated method stub  
  236.         Contact contact = Contact.from(contactPointer);  
  237.         contact.setEnabled(false);  
  238.     }  
  239.   
  240.     @Override  
  241.     public void postSolve(int contactPointer, int arg1) {  
  242.         // TODO Auto-generated method stub  
  243.         Contact contact = Contact.from(contactPointer);  
  244.         contact.setEnabled(false);  
  245.     }  
  246.   
  247.     @Override  
  248.     public void preSolve(int contactPointer, int arg1) {  
  249.         // TODO Auto-generated method stub  
  250.         Contact contact = Contact.from(contactPointer);  
  251.         contact.setEnabled(false);  
  252.     }  
  253. }  

最上面一层的设计

Box2.java

 

view plain
  1. import java.nio.channels.Selector;  
  2. import java.util.Random;  
  3.   
  4. import com.wiyun.engine.box2d.Box2D;  
  5. import com.wiyun.engine.box2d.Box2DRender;  
  6. import com.wiyun.engine.box2d.FixtureAnimation;  
  7. import com.wiyun.engine.box2d.collision.PolygonShape;  
  8. import com.wiyun.engine.box2d.dynamics.Body;  
  9. import com.wiyun.engine.box2d.dynamics.BodyDef;  
  10. import com.wiyun.engine.box2d.dynamics.Fixture;  
  11. import com.wiyun.engine.box2d.dynamics.World;  
  12. import com.wiyun.engine.box2d.dynamics.World.IContactListener;  
  13. import com.wiyun.engine.box2d.dynamics.contacts.Contact;  
  14. import com.wiyun.engine.nodes.Director;  
  15. import com.wiyun.engine.nodes.Layer;  
  16. import com.wiyun.engine.nodes.Scheduler;  
  17. import com.wiyun.engine.nodes.Timer;  
  18. import com.wiyun.engine.opengl.Texture2D;  
  19. import com.wiyun.engine.particle.ParticleSystem;  
  20. import com.wiyun.engine.particle.QuadParticleSystem;  
  21. import com.wiyun.engine.types.WYPoint;  
  22. import com.wiyun.engine.types.WYSize;  
  23. import com.wiyun.engine.utils.TargetSelector;  
  24.   
  25. public class Box2 extends Layer implements IContactListener {  
  26.     World mWorld;  
  27.     Body m_character;  
  28.   
  29.     float x;  
  30.     float y;  
  31.     float taijiex=1;  
  32.     static float change1 = 1, jump1 = 1,boost1=0;  
  33.     boolean hide=false;  
  34.     int donghua = 0// 娃娃上升的判断条件(在碰撞时被赋值为true)  
  35.     int donghua2 = 1// 娃娃下落的判断条件(在娃娃速度为0时被赋值为true)  
  36.     WYSize s;// 屏幕大小  
  37.     Character character;// 角色类的声明  
  38.     Taijie[] taijie;// 台阶类的声明  
  39.     WYPoint sp;// 整个场景的移动速度(碰撞后台阶都赋予这个速度) ps:grass1,2,3要自己重新赋速度了  
  40.     int mum = 0;  
  41.     int move=-1;  
  42.     Timer t ;  
  43.     static Box2DRender render;// 向一个关联里面加入图片或者动画信息  
  44.     static boolean collision = false, collision2 = false;  
  45.     boolean taijiemove=false;  
  46.     protected Box2D mBox2D;  
  47.     ParticleSystem emitter;  
  48.     Fixture jump, boost, change;  
  49.   
  50.     Box2() {  
  51.         mBox2D = Box2D.make();  
  52.   
  53.         mBox2D.setDebugDraw(false);  
  54.         render = Box2DRender.make();  
  55.         mBox2D.setBox2DRender(render);  
  56.   
  57.         mWorld = mBox2D.getWorld();  
  58.         sp = WYPoint.make(0f, 0f);  
  59.         character = new Character(mWorld, 15f);  
  60.         taijie = new Taijie[10];  
  61.         {  
  62.             for (int i = 0; i < taijie.length; i++) {  
  63.                 taijie[i] = new Taijie(mWorld);  
  64.                 taijie[i].m_platform.getBody().setTransform(0, taijie[i].m_platform.getBody().getPosition().y + 4.6f, 0);  
  65.             }  
  66.         }  
  67.         Random a = new Random();  
  68.         {  
  69.             BodyDef bd = BodyDef.make();  
  70.             bd.setType(Body.TYPE_DYNAMIC);  
  71.             bd.setPosition((float) a.nextInt(13) - 5.5f, 2.1f * 80f);  
  72.             Body body = mWorld.createBody(bd);  
  73.             bd.destroy();  
  74.             PolygonShape shape = PolygonShape.make();  
  75.             shape.setAsBox(1.3f, 1.3f);  
  76.             jump = body.createFixture(shape, 0.0f);  
  77.             Texture2D tex = Texture2D.makePNG(R.drawable.jump);  
  78.             render.bindTexture(jump, tex);  
  79.             tex.autoRelease();  
  80.         }  
  81.         {  
  82.             BodyDef bd = BodyDef.make();  
  83.             bd.setType(Body.TYPE_DYNAMIC);  
  84.             bd.setPosition((float) a.nextInt(13) - 5.5f, 2.1f * 180f);  
  85.             Body body = mWorld.createBody(bd);  
  86.             bd.destroy();  
  87.             PolygonShape shape = PolygonShape.make();  
  88.             shape.setAsBox(1.3f, 1.3f);  
  89.             boost = body.createFixture(shape, 0.0f);  
  90.             Texture2D tex = Texture2D.makePNG(R.drawable.boost);  
  91.             render.bindTexture(boost, tex);  
  92.             tex.autoRelease();  
  93.         }  
  94.         {  
  95.             BodyDef bd = BodyDef.make();  
  96.             bd.setType(Body.TYPE_DYNAMIC);  
  97.             bd.setPosition((float) a.nextInt(13) - 5.5f, 2.1f * 150f);  
  98.             Body body = mWorld.createBody(bd);  
  99.             bd.destroy();  
  100.             PolygonShape shape = PolygonShape.make();  
  101.             shape.setAsBox(1.3f, 1.3f);  
  102.             change = body.createFixture(shape, 0.0f);  
  103.             Texture2D tex = Texture2D.makePNG(R.drawable.changge);  
  104.             render.bindTexture(change, tex);  
  105.             tex.autoRelease();  
  106.         }  
  107.         s = Director.getInstance().getWindowSize();  
  108.           
  109.         emitter = new ParticleSnow();  
  110.         emitter.setPosition(-1000, -1000);  
  111.         main.mScene.addChild(emitter);  
  112.         addChild(mBox2D);  
  113.         // set gravity  
  114.         mWorld.setGravity(047);// 47  
  115.           
  116.         // place box2d to center of bottom edge  
  117.         mBox2D.setPosition(s.width / 20);  
  118.   
  119.         // add contact listener  
  120.         mWorld.setContactListener(this);  
  121.         setAccelerometerEnabled(true);  
  122.         schedule(new TargetSelector(this"update(float)"new Object[] { 0f }));  
  123.           
  124.     }  
  125.   
  126.     public void update(float delta) {  
  127.         mWorld.step(1.f / 60.f, 1010);  
  128.         mWorld.clearForces();  
  129.         float top = 0;  
  130.         for (int i2 = 0; i2 < taijie.length; i2++) {  
  131.   
  132.             if (taijie[i2].m_platform.getBody().getPosition().y > top) {  
  133.                 top = taijie[i2].m_platform.getBody().getPosition().y;  
  134.             }  
  135.         }  
  136.         for (int i2 = 0; i2 < taijie.length; i2++) {  
  137.             if (taijie[i2].m_platform.getBody().getPosition().y < -2) {  
  138.                 Random a= new Random();  
  139.   
  140.                 Random b = new Random();  
  141.                 Random c = new Random();  
  142.                 b.nextInt(30);  
  143.                 if(i2==move)  
  144.                 {  
  145.                     Scheduler.getInstance().unschedule(t);  
  146.                     move=-1;  
  147.                     taijie[i2].m_platform.getBody().setLinearVelocity(WYPoint.make(0, taijie[i2].m_platform.getBody().getLinearVelocity().y));  
  148.                     Texture2D tex = Texture2D.makePNG(R.drawable.bar1);  
  149.                     render.bindTexture(taijie[i2].m_platform, tex);  
  150.                     tex.autoRelease();  
  151.                 }  
  152.                 if(hide==true&&b.nextInt(30)>BC.chushi)  
  153.                 {  
  154.                 taijie[i2].m_platform.getBody().setTransform(-100f, top + 4.6f, 0);  
  155.                 hide=false;  
  156.                 }else  
  157.                 {  
  158.                 taijie[i2].m_platform.getBody().setTransform((float) a.nextInt(16) - 7.5f, top + 4.6f, 0);  
  159.                 hide=true;  
  160.                 if(c.nextInt(30)+1>BC.chushi&&move==-1)  
  161.                 {  
  162.                 Random e = new Random();  
  163.                   
  164.                     taijie[i2].m_platform.getBody().setTransform(-3f, taijie[i2].m_platform.getBody().getPosition().y, 0);  
  165.                       
  166.                     taijie[i2].m_platform.getBody().setLinearVelocity(WYPoint.make(+e.nextInt(2)+4, taijie[i2].m_platform.getBody().getLinearVelocity().y));  
  167.                   
  168.                 Texture2D tex = Texture2D.makePNG(R.drawable.bar3);  
  169.                 render.bindTexture(taijie[i2].m_platform, tex);  
  170.                 tex.autoRelease();  
  171.                 TargetSelector mSelector1 = new TargetSelector(this"updatechange(float,int,int)"new Object[] { 0f, 1 ,i2});  
  172.                 t = new Timer(mSelector1, 2f);  
  173.                 Scheduler.getInstance().schedule(t);  
  174.                 move=i2;  
  175.                 System.out.println("时间已经设置了");  
  176.                 }  
  177.                 }  
  178.                 GameView.fenshu++;  
  179.             }  
  180.         }  
  181.         /* 
  182.         for (int i2 = 0; i2 < taijie.length; i2++) { 
  183.             if (taijie[i2].m_platform.getBody().getPosition().x < -2) { 
  184.                 taijie[i2].m_platform.getBody().setLinearVelocity(WYPoint.make(taijie[i2].m_platform.getBody().getLinearVelocity().x*-1f, 0)); 
  185.             }else if(taijie[i2].m_platform.getBody().getPosition().x >20) 
  186.             { 
  187.                 taijie[i2].m_platform.getBody().setLinearVelocity(WYPoint.make(taijie[i2].m_platform.getBody().getLinearVelocity().x*-1f, 0)); 
  188.             } 
  189.         }*/  
  190.         if (GameView.score < GameView.fenshu * 100) {  
  191.             GameView.score += 9;  
  192.         }  
  193.         if (jump.getBody().getPosition().y < -11.1) {  
  194.             Random a = new Random();  
  195.             jump.getBody().setTransform((float) a.nextInt(13) - 5.5f, 6500);  
  196.         }  
  197.         if (change.getBody().getPosition().y < -11.1) {  
  198.             Random a = new Random();  
  199.             change.getBody().setTransform((float) a.nextInt(13) - 5.5f, 6500);  
  200.         }  
  201.         if (boost.getBody().getPosition().y < -11.1) {  
  202.             Random a = new Random();  
  203.             boost.getBody().setTransform((float) a.nextInt(13) - 5.5f, 6500);  
  204.         }  
  205.         if (character.m_character.getBody().getPosition().x < -11.1) {  
  206.             float a = character.m_character.getBody().getPosition().y;  
  207.             character.m_character.getBody().setTransform(11, a, 0);  
  208.         } else if (character.m_character.getBody().getPosition().x > 11.1) {  
  209.             float a = character.m_character.getBody().getPosition().y;  
  210.             character.m_character.getBody().setTransform(-11, a, 0);  
  211.         }  
  212.   
  213.         if(boost1==0)  
  214.         {  
  215.         if (taijie[0].m_platform.getBody().getLinearVelocity().y >= 0  
  216.                 && donghua2 == 1) {  
  217.             FixtureAnimation anim = FixtureAnimation.make(0.2f,  
  218.                     R.drawable.down, R.drawable.down2, R.drawable.down3);  
  219.             anim.setLoop(true);  
  220.             anim.start(character.m_character);  
  221.             donghua2 = 0;  
  222.         } else if (donghua == 1) {  
  223.             FixtureAnimation anim = FixtureAnimation.make(0.15f,  
  224.                     R.drawable.up1, R.drawable.up2, R.drawable.up3,  
  225.                     R.drawable.up4, R.drawable.up5, R.drawable.up6);  
  226.             anim.setLoop(false);  
  227.             anim.start(character.m_character);  
  228.             donghua = 0;  
  229.         }  
  230.         }else  
  231.         {  
  232.             FixtureAnimation anim = FixtureAnimation.make(0.15f,  
  233.                     R.drawable.up1, R.drawable.up2, R.drawable.up3,  
  234.                     R.drawable.up4, R.drawable.up5, R.drawable.up6);  
  235.             anim.setLoop(false);  
  236.             anim.start(character.m_character);  
  237.         }  
  238.   
  239.     }  
  240.   
  241.     public void updatechange(float a,int c,int n)  
  242.     {  
  243.         taijie[n].m_platform.getBody().setLinearVelocity(WYPoint.make(taijie[move].m_platform.getBody().getLinearVelocity().x*-1f, taijie[move].m_platform.getBody().getLinearVelocity().y));  
  244.     }  
  245.       
  246.     public void updategravityOnce(float a) {  
  247.         BC.mWorld.setGravity(