private void init() { mPaint = new Paint(); mRect = new Rect(); mBitmaps = Utils.getBigPlane(mContext); WIDTH = Utils.getNewWidth(mBitmaps[0].getWidth()); HEIGHT = (int) (WIDTH * mBitmaps[0].getHeight() / (float) mBitmaps[0].getWidth()); }
@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); }
public BigPlane(Context ctx, EnemyType mType, Handler handler) { super(ctx, mType, handler); // TODO Auto-generated constructor stub setKilledScore(KILLED_SCORE); setKillCount(KILLED_COUNT); mSpeed = SPEED_NORMAL_BIG * Utils.getScale(); init(); }
/** * 绘制死亡动画 * * @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); } }
@Override public void drawKilled(Canvas canvas) { // TODO Auto-generated method stub if (mKilledAnim == null) { Bitmap[] bitmaps = Utils.getBigPlaneKilled(mContext); mKilledAnim = new Animation(bitmaps, false); mKilledAnimSize = bitmaps.length; } if (mKilledAnimIdx < mKilledAnimSize) { mKilledAnim.drawFrame(canvas, mPaint, mRect, mKilledAnimIdx); } if (mDrawNext % 2 == 0) { mKilledAnimIdx++; } mDrawNext++; if (mKilledAnimIdx >= mKilledAnimSize) { mKilledAnimIdx = IDX_KILLED_DEFAULT; reset(); } }
@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; }
private void loadPlane() { mBitmaps = Utils.getMyPlane(mContext); WIDTH = Utils.getNewWidth(mBitmaps[0].getWidth()); HEIGHT = (int) (WIDTH * mBitmaps[0].getHeight() / (float) mBitmaps[0].getWidth()); setPos(Utils.DEVICE_WIDTH / 2, Utils.DEVICE_HEIGHT - HEIGHT / 2); }
/** * 设置死亡状态 * * @param dead */ public void setDead(boolean dead) { isDead = dead; // 通知主界面myplan 挂了 Utils.sendSoundMsg(mHandler, R.raw.use_bomb); }