Example #1
0
  @Override
  public void draw(Canvas canvas) {
    // 如果本机已经死亡
    if (isDead) {
      // 如果死亡动画为空,先初始化
      if (mKilledAnim == null) {
        Bitmap[] bitmaps = Utils.getMyPlaneKilled(mContext);
        mKilledAnim = new Animation(bitmaps, false);
        mKilledAnimSize = bitmaps.length;
      }
      if (mKilledAnimIdx < mKilledAnimSize) {
        drawKilled(canvas);
      }
      return;
    }
    // TODO Auto-generated method stub
    calDrawRectByPos();
    canvas.drawBitmap(mBitmaps[mDrawWhich], null, mRect, mPaint);
    if (mDrawNext % 2 == 0) {
      mDrawWhich++;
    }
    if (mDrawWhich >= mBitmaps.length) {
      mDrawWhich = 0;
    }
    mDrawNext++;

    long now = System.currentTimeMillis();
    if (now - mSendTime > SHOOT_TIME) {
      if (isDoubleBullet) {
        BulletFactory.createBullet(
                mContext,
                Bullet.TYPE_BULLET_DOUBLE,
                mPos.x + WIDTH / 4,
                mRect.top - Bullet.HEIGHT / 2)
            .draw(canvas);
        BulletFactory.createBullet(
                mContext,
                Bullet.TYPE_BULLET_DOUBLE,
                mPos.x - WIDTH / 4,
                mRect.top - Bullet.HEIGHT / 2)
            .draw(canvas);
        if (now - mDoubleBulletTime > DOUBLE_BULLET_TIME) {
          cancelDoubleBullet();
        }
      } else {
        BulletFactory.createBullet(
                mContext, Bullet.TYPE_BULLET_SINGLE, mPos.x, mRect.top - Bullet.HEIGHT / 2)
            .draw(canvas);
      }
      // 产生子弹发出的声音
      Utils.sendSoundMsg(mHandler, R.raw.bullet);

      mSendTime = now;
    }
    BulletFactory.drawAllBullet(canvas);
  }
Example #2
0
  /**
   * 绘制死亡动画
   *
   * @param canvas
   */
  public void drawKilled(Canvas canvas) {
    // TODO Auto-generated method stub
    if (mKilledAnimIdx < mKilledAnimSize) {
      mKilledAnim.drawFrame(canvas, mPaint, mRect, mKilledAnimIdx);
    }
    if (mDrawNext % 5 == 0) {
      mKilledAnimIdx++;
    }
    mDrawNext++;
    if (mKilledAnimIdx >= mKilledAnimSize) {
      // 通知主界面game over
      Message msg = mHandler.obtainMessage();
      msg.what = MainActivity.MSG_GAME_OVER;
      mHandler.sendMessage(msg);

      // 播放 game over 声音
      Utils.sendSoundMsg(mHandler, R.raw.game_over);
    }
  }
Example #3
0
  @Override
  public boolean checkCollision(Canvas canvas, Sprite s) {
    // TODO Auto-generated method stub
    if (s instanceof Bullet) { // 子弹与小飞机碰撞检测
      if (Collision.isRectCls(mRect, ((Bullet) s).getRect())) {
        // 被打击的数目加1
        addHitted();
        // 播放被打击的声音
        Utils.sendSoundMsg(mHandler, R.raw.button);
        if (isKilled()) { // 如果被击落
          bomb(canvas);
        }
        s.reset();
        return true;
      }
    } else if (s instanceof Plane) {
      if (!((Plane) s).isDead() && Collision.isRectCls(mRect, ((Plane) s).getCollisionRect())) {
        ((Plane) s).setDead(true);
        return true;
      }
    }

    return false;
  }
Example #4
0
 /**
  * 设置死亡状态
  *
  * @param dead
  */
 public void setDead(boolean dead) {
   isDead = dead;
   // 通知主界面myplan 挂了
   Utils.sendSoundMsg(mHandler, R.raw.use_bomb);
 }