Exemple #1
0
 public int doLogin(int step) {
   switch (step) {
     case INIT:
       // 初始化角色血量
       player.setHp(Player.MAX_HP);
       // 初始化登录界面
       if (loginView == null) {
         loginView = new RelativeLayout(mainContext);
         loginView.setBackgroundResource(R.drawable.game_back);
         // 创建按钮
         Button button = new Button(mainContext);
         // 设置按钮的背景图片
         button.setBackgroundResource(R.drawable.button_selector);
         RelativeLayout.LayoutParams params =
             new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
         params.addRule(RelativeLayout.CENTER_IN_PARENT);
         // 添加按钮
         loginView.addView(button, params);
         button.setOnClickListener(
             new OnClickListener() {
               @Override
               public void onClick(View v) {
                 // 将游戏场景的常量添加到stageList集合中
                 stageList.add(STAGE_GAME);
               }
             });
         // 通过Handler通知主界面加载loginView组件
         setViewHandler.sendMessage(setViewHandler.obtainMessage(0, loginView)); // ①
       }
       break;
     case LOGIC:
       break;
     case CLEAN:
       // 清除登录界面
       if (loginView != null) {
         // 通过Handler通知主界面删除loginView组件
         delViewHandler.sendMessage(delViewHandler.obtainMessage(0, loginView)); // ②
         loginView = null;
       }
       break;
     case PAINT:
       break;
   }
   return STAGE_NO_CHANGE;
 }
Exemple #2
0
 public int doGame(int step) {
   switch (step) {
     case INIT:
       // 初始化游戏界面
       if (gameLayout == null) {
         gameLayout = new RelativeLayout(mainContext);
         // 添加向左移动的按钮
         Button button = new Button(mainContext);
         button.setId(ID_LEFT);
         // 设置按钮的背景图片
         button.setBackground(getResources().getDrawable(R.drawable.left));
         RelativeLayout.LayoutParams params =
             new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
         params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
         params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
         params.setMargins((int) (ViewManager.scale * 20), 0, 0, (int) (ViewManager.scale * 10));
         // 向游戏界面上添加向左的按钮
         gameLayout.addView(button, params);
         // 为按钮添加事件监听器
         button.setOnTouchListener(
             new OnTouchListener() {
               @Override
               public boolean onTouch(View v, MotionEvent event) {
                 switch (event.getAction()) {
                   case MotionEvent.ACTION_DOWN:
                     player.setMove(Player.MOVE_LEFT);
                     break;
                   case MotionEvent.ACTION_UP:
                     player.setMove(Player.MOVE_STAND);
                     break;
                   case MotionEvent.ACTION_MOVE:
                     break;
                 }
                 return false;
               }
             });
         // 添加向右移动的按钮
         button = new Button(mainContext);
         // 设置按钮的背景图片
         button.setBackground(getResources().getDrawable(R.drawable.right));
         params =
             new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
         params.addRule(RelativeLayout.RIGHT_OF, ID_LEFT);
         params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
         params.setMargins((int) (ViewManager.scale * 20), 0, 0, (int) (ViewManager.scale * 10));
         // 向游戏界面上添加向右的按钮
         gameLayout.addView(button, params);
         // 为按钮添加事件监听器
         button.setOnTouchListener(
             new OnTouchListener() {
               public boolean onTouch(View v, MotionEvent event) {
                 switch (event.getAction()) {
                   case MotionEvent.ACTION_DOWN:
                     player.setMove(Player.MOVE_RIGHT);
                     break;
                   case MotionEvent.ACTION_UP:
                     player.setMove(Player.MOVE_STAND);
                     break;
                   case MotionEvent.ACTION_MOVE:
                     break;
                 }
                 return false;
               }
             });
         // 添加射击按钮
         button = new Button(mainContext);
         button.setId(ID_FIRE);
         // 设置按钮的背景图片
         button.setBackground(getResources().getDrawable(R.drawable.fire));
         params =
             new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
         params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
         params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
         params.setMargins(0, 0, (int) (ViewManager.scale * 20), (int) (ViewManager.scale * 10));
         // 向游戏界面上添加射击的按钮
         gameLayout.addView(button, params);
         // 为按钮添加事件监听器
         button.setOnClickListener(
             new OnClickListener() {
               @Override
               public void onClick(View v) {
                 // 当角色的leftShootTime为0时(上一枪发射结束),角色才能发射下一枪。
                 if (player.getLeftShootTime() <= 0) {
                   player.addBullet();
                 }
               }
             });
         // 添加跳的按钮
         button = new Button(mainContext);
         // 设置按钮的背景图片
         button.setBackground(getResources().getDrawable(R.drawable.jump));
         params =
             new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
         params.addRule(RelativeLayout.LEFT_OF, ID_FIRE);
         params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
         params.setMargins(0, 0, (int) (ViewManager.scale * 20), (int) (ViewManager.scale * 10));
         // 向游戏界面上添加跳的按钮
         gameLayout.addView(button, params);
         // 为按钮添加事件监听器
         button.setOnClickListener(
             new OnClickListener() {
               @Override
               public void onClick(View v) {
                 player.setJump(true);
               }
             });
         setViewHandler.sendMessage(setViewHandler.obtainMessage(0, gameLayout)); // ③
       }
       break;
     case LOGIC:
       // 随机生成怪物
       MonsterManager.generateMonster();
       // 检查碰撞
       MonsterManager.checkMonster();
       // 角色跳与移动
       player.logic();
       // 角色死亡
       if (player.isDie()) {
         stageList.add(STAGE_LOSE);
       }
       break;
     case CLEAN:
       // 清除游戏界面
       if (gameLayout != null) {
         delViewHandler.sendMessage(delViewHandler.obtainMessage(0, gameLayout)); // ④
         gameLayout = null;
       }
       break;
     case PAINT:
       // 画游戏元素
       ViewManager.clearScreen(canvas);
       ViewManager.drawGame(canvas);
       break;
   }
   return STAGE_NO_CHANGE;
 }