1. 效果
2. 需求
安置向日葵完成,等待 10 秒 產(chǎn)生一個太陽太陽 5 秒鐘如果沒有被收集,則自動消失太陽如果被點擊,即收集,則執(zhí)行位移操作,回到卡片狀態(tài)欄的太陽卡槽里
3. 思路
向日葵的 10 秒產(chǎn)生太陽事件
package com.su.botanywarzombies.entity;
public class Flower extends BaseModel {@Overridepublic void drawSelf(Canvas canvas, Paint paint) {
super.drawSelf(canvas, paint);
if (isLive) {canvas.drawBitmap(Config.flowerFlames[farmeIndex], locationX, locationY, paint);farmeIndex = (++farmeIndex) %
8;
if (System.currentTimeMillis() - lastBrithTime > TIME) {lastBrithTime = System.currentTimeMillis();creatSun(locationX, locationY);}}}
private void creatSun(
int locationX,
int locationY) {GameView.getInstance().creatSun(locationX, locationY);}
游戲圖層畫出小太陽
package com.su.botanywarzombies.view;
public class GameView extends SurfaceView implements SurfaceHolder.Callback, Runnable {private ArrayList<BaseModel> gameLayout3;
public void creatSun(
int locationX,
int locationY) {
synchronized (mSurfaceHolder) {gameLayout3.add(
new Sun(locationX, locationY));}}
private void onDrawing(Canvas mCanvas) {...
for (BaseModel model : gameLayout3) {model.drawSelf(mCanvas, mPaint);}...}
超時自動回收小太陽
package com.su.botanywarzombies.entity;
public class Sun extends BaseModel implements TouchAble {/*** SHOW 生命周期TIME有效* * MOVE 生命周期無效* */public enum SunState {SHOW, MOVE}
@Overridepublic void drawSelf(Canvas canvas, Paint paint) {
if (isLive) {}
else if (mSunState == SunState.SHOW) {
if (System.currentTimeMillis() - brithTime > TIME) {isLive =
false;}}canvas.drawBitmap(Config.sun, locationX, locationY, paint);}
對于游戲圖層
package com.su.botanywarzombies.view;
public class GameView extends SurfaceView implements SurfaceHolder.Callback, Runnable {private void updateData() {deadList.clear();
for (BaseModel model : gameLayout3) {
if (!model.isLive) {deadList.add(model);}}
for (BaseModel model : deadList) {
if (!model.isLive) {gameLayout1.remove(model);gameLayout2.remove(model);gameLayout3.remove(model);}}
點擊收取小太陽,并為位移運動做準備
package com.su.botanywarzombies.entity;
public class Sun extends BaseModel implements TouchAble {@Overridepublic boolean onTouch(MotionEvent event) {
int x = (
int) event.getX();
int y = (
int) event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (touchArea.contains(x, y)) {mSunState = SunState.MOVE;xDirection = locationX - Config.sunDeadLocationX;yDirection = locationY - Config.sunDeadLocationY;xSpeed = xDirection /
10f;ySpeed = yDirection /
10f;
return true;}
break;
default:
break;}
return false;}
小太陽移動到卡片槽動畫
package com.su.botanywarzombies.entity;
public class Sun extends BaseModel implements TouchAble {@Overridepublic void drawSelf(Canvas canvas, Paint paint) {
if (isLive) {
if (mSunState == SunState.MOVE) {locationX = (
int) (locationX - xSpeed);locationY = (
int) (locationY - ySpeed);
if (locationX <
0 || locationY <
0) {locationX = Config.sunDeadLocationX;locationY = Config.sunDeadLocationY;isLive =
false;}.... canvas.drawBitmap(Config.sun, locationX, locationY, paint);
4. Demo 下載
https://github.com/sufadi/BotanyWarZombies
總結(jié)
以上是生活随笔為你收集整理的09 Android 植物人大战僵尸-生成小太阳的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。