Android游戏开发学习之引擎用法实例详解

2019-12-10 20:00:43于丽
  • import android.app.Activity;   import android.content.pm.ActivityInfo;  
  • import android.os.Bundle;   import android.util.DisplayMetrics;  
  • import android.view.Window;   import android.view.WindowManager;  
  • import static box2d.bheap.Constant.*;   public class MyBox2dActivity extends Activity  
  • {   AABB worldAABB;//创建 一个管理碰撞的世界  
  • World world;   Random random=new Random();  
  • //物体列表   ArrayList<MyBody> bl=new ArrayList<MyBody>();  
  • public void onCreate(Bundle savedInstanceState)   {  
  • super.onCreate(savedInstanceState);   //设置为全屏  
  • requestWindowFeature(Window.FEATURE_NO_TITLE);   getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,  
  • WindowManager.LayoutParams. FLAG_FULLSCREEN);   //设置为横屏模式  
  • setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);   //获取屏幕尺寸  
  • DisplayMetrics dm=new DisplayMetrics();   getWindowManager().getDefaultDisplay().getMetrics(dm);  
  • if(dm.widthPixels<dm.heightPixels)   {  
  • SCREEN_WIDTH=dm.widthPixels;   SCREEN_HEIGHT=dm.heightPixels;  
  • }   else 
  • {   SCREEN_WIDTH=dm.heightPixels;  
  • SCREEN_HEIGHT=dm.widthPixels;   }  
  • worldAABB = new AABB();   //上下界,以屏幕的左上方为 原点,如果创建的刚体到达屏幕的边缘的话,会停止模拟  
  • worldAABB.lowerBound.set(-100.0f,-100.0f);   worldAABB.upperBound.set(100.0f, 100.0f);//注意这里使用的是现实世界的单位  
  • Vec2 gravity = new Vec2(0.0f,10.0f);   boolean doSleep = true;  
  • //创建世界   world = new World(worldAABB, gravity, doSleep);  
  • //创建4边   final int kd=40;//宽度或高度  
  • MyRectColor mrc=Box2DUtil.createBox(kd/4, SCREEN_HEIGHT/2, kd/4, SCREEN_HEIGHT/2, true,world,0xFFe6e4FF);   bl.add(mrc);