10 Android 植物人大战僵尸-生成僵尸
生活随笔
收集整理的這篇文章主要介紹了
10 Android 植物人大战僵尸-生成僵尸
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. 效果
2. 需求
每隔一定的時間在5個跑道中隨機生成僵尸,并且從右往左移動
3. 開發
在onDrawing事件調用,但是要不要生成,根據是否滿足15秒的定時周期
package com.su.botanywarzombies.view;public class GameView extends SurfaceView implements SurfaceHolder.Callback, Runnable {// 僵尸生成器private ZombieManager mZombieManager;private void onDrawing(Canvas mCanvas) {// 定時生成僵尸mZombieManager.drawSelf(mCanvas, mPaint);其中跑道使用隨機函數完成,僵尸的生成坐標的定義
package com.su.botanywarzombies.view;public class GameView extends SurfaceView implements SurfaceHolder.Callback, Runnable {// 生產僵尸public void apply4CreatZombie() {synchronized (mSurfaceHolder) {int raceWay = 0;// 生成 0~4的跑道,向下強轉取整raceWay = (int) (Math.random() * 5);switch (raceWay) {case 0:// Config.screenWidth + Config.zombieFlames[0].getWidth()// 為了僵尸提前走gameLayout4zombie0.add(new Zombie(Config.screenWidth + Config.zombieFlames[0].getWidth(), Config.racWayYpoint[0], raceWay));break;case 1:gameLayout4zombie1.add(new Zombie(Config.screenWidth + Config.zombieFlames[0].getWidth(), Config.racWayYpoint[1], raceWay));break;case 2:gameLayout4zombie2.add(new Zombie(Config.screenWidth + Config.zombieFlames[0].getWidth(), Config.racWayYpoint[2], raceWay));break;case 3:gameLayout4zombie3.add(new Zombie(Config.screenWidth + Config.zombieFlames[0].getWidth(), Config.racWayYpoint[3], raceWay));break;case 4:gameLayout4zombie4.add(new Zombie(Config.screenWidth + Config.zombieFlames[0].getWidth(), Config.racWayYpoint[4], raceWay));break;default:break;}}}這里定義了僵尸的動畫和移動速度
package com.su.botanywarzombies.entity;import android.graphics.Canvas; import android.graphics.Paint;import com.su.botanywarzombies.constant.Config; import com.su.botanywarzombies.model.BaseModel;/*** 僵尸類*/ public class Zombie extends BaseModel {// 跑道,有碰撞監測private int raceWay;// 移動的動畫幀private int farmeIndex;// 僵尸移動的速度private int seepX = 1;public Zombie(int locationX, int locationY, int raceWay) {this.locationX = locationX;this.locationY = locationY;this.raceWay = raceWay;this.isLive = true;}@Overridepublic void drawSelf(Canvas canvas, Paint paint) {if (isLive) {canvas.drawBitmap(Config.zombieFlames[farmeIndex], locationX, locationY, paint);farmeIndex = (++farmeIndex) % 7;locationX = locationX - seepX;}} }4. Demo下載
https://github.com/sufadi/BotanyWarZombies
總結
以上是生活随笔為你收集整理的10 Android 植物人大战僵尸-生成僵尸的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: access查找出生日期年份_acces
- 下一篇: IIS部署,发布网站