10 Android 植物人大战僵尸-生成僵尸
生活随笔
收集整理的這篇文章主要介紹了
10 Android 植物人大战僵尸-生成僵尸
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1. 效果
2. 需求
每隔一定的時(shí)間在5個(gè)跑道中隨機(jī)生成僵尸,并且從右往左移動(dòng)
3. 開發(fā)
在onDrawing事件調(diào)用,但是要不要生成,根據(jù)是否滿足15秒的定時(shí)周期
package com.su.botanywarzombies.view;public class GameView extends SurfaceView implements SurfaceHolder.Callback, Runnable {// 僵尸生成器private ZombieManager mZombieManager;private void onDrawing(Canvas mCanvas) {// 定時(shí)生成僵尸mZombieManager.drawSelf(mCanvas, mPaint);其中跑道使用隨機(jī)函數(shù)完成,僵尸的生成坐標(biāo)的定義
package com.su.botanywarzombies.view;public class GameView extends SurfaceView implements SurfaceHolder.Callback, Runnable {// 生產(chǎn)僵尸public void apply4CreatZombie() {synchronized (mSurfaceHolder) {int raceWay = 0;// 生成 0~4的跑道,向下強(qiáng)轉(zhuǎn)取整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;}}}這里定義了僵尸的動(dòng)畫和移動(dòng)速度
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 {// 跑道,有碰撞監(jiān)測private int raceWay;// 移動(dòng)的動(dòng)畫幀private int farmeIndex;// 僵尸移動(dòng)的速度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
總結(jié)
以上是生活随笔為你收集整理的10 Android 植物人大战僵尸-生成僵尸的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: access查找出生日期年份_acces
- 下一篇: 01 Android 植物人大战僵尸-画