CCSprite.sprite()加載進來的圖片默認錨點是(0.5,0.5),即圖片中間java
CCTMXTiledMap.tiledMap()加載進來的地圖默認錨點(0,0)node
這段代碼在android
SystemClock.sleep(2000);會報錯,
SystemClock.sleep(5000)不會報錯。
private void consumeTime() { new AsyncTask<Void, Void, Void>(){ @Override protected Void doInBackground(Void... params) { SystemClock.sleep(5000); return null; } protected void onPostExecute(Void result) { setIsTouchEnabled(true); loading.removeSelf(); WelcomeLayer.this.getChildByTag(TAG_START).setVisible(true); }; }.execute(); }
錯誤logapp
11-22 18:36:44.818: W/System.err(19995): at org.zqt.cocos2d.layer.WelcomeLayer$1.onPostExecute(WelcomeLayer.java:42)
11-22 18:36:44.819: W/System.err(19995): at org.zqt.cocos2d.layer.WelcomeLayer$1.onPostExecute(WelcomeLayer.java:1)
11-22 18:36:44.819: W/System.err(19995): at android.os.AsyncTask.finish(AsyncTask.java:632)
11-22 18:36:44.819: W/System.err(19995): at android.os.AsyncTask.access$500(AsyncTask.java:177)
11-22 18:36:44.819: W/System.err(19995): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:648)
11-22 18:36:44.819: W/System.err(19995): at android.os.Handler.dispatchMessage(Handler.java:117)
11-22 18:36:44.819: W/System.err(19995): at android.os.Looper.loop(Looper.java:194)
11-22 18:36:44.819: W/System.err(19995): at android.app.ActivityThread.main(ActivityThread.java:6097)
11-22 18:36:44.819: W/System.err(19995): at java.lang.reflect.Method.invokeNative(Native Method)
11-22 18:36:44.820: W/System.err(19995): at java.lang.reflect.Method.invoke(Method.java:525)
11-22 18:36:44.820: W/System.err(19995): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:838)
11-22 18:36:44.820: W/System.err(19995): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)dom
MainActivity.javaide
public class MainActivity extends Activity { private CCDirector director; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); CCGLSurfaceView surfaceView = new CCGLSurfaceView(this); director = CCDirector.sharedDirector(); director.attachInView(surfaceView); // director.setDisplayFPS(true); director.setDeviceOrientation(CCDirector.kCCDeviceOrientationLandscapeLeft); director.setScreenSize(480, 320); CCScene scene = createScene(); director.runWithScene(scene); setContentView(surfaceView); } private CCScene createScene() { CCScene scene = CCScene.node(); // WelcomeLayer layer = new WelcomeLayer(); FightLayer layer = new FightLayer(); scene.addChild(layer); return scene; } }
ShowZombie.javaoop
public class ShowZombie extends CCSprite{ private static ArrayList<CCSpriteFrame> shakeFrames; public ShowZombie() { super("image/zombies/zombies_1/shake/z_1_01.png"); this.setAnchorPoint(0.5f,0); this.setScale(0.5f); shake(); } public void shake(){ CCAction repeatForeverAction = CommonUtil.getRepeatForeverAction(shakeFrames, 2, "image/zombies/zombies_1/shake/z_1_%02d.png", 0.2f); this.runAction(repeatForeverAction); } }
ShowPlant.javaui
public class ShowPlant { private static HashMap<Integer,HashMap> data; private int id; private int sumNum = 25; private CCSprite defaultImg; private CCSprite bgImg; static{ data = new HashMap<Integer, HashMap>(); HashMap<String,String> map ; for(int i=1;i<=9;i++){ map = new HashMap<String,String>(); map.put("fileName", String.format("image/fight/chose/choose_default%02d.png", i)); map.put("sunNum", String.valueOf(i*25)); data.put(i, map); } } public ShowPlant(int id) { this.id = id; HashMap<String,String> item = data.get(id); defaultImg = CCSprite.sprite(item.get("fileName")); bgImg = CCSprite.sprite(item.get("fileName")); defaultImg.setAnchorPoint(0,0); bgImg.setAnchorPoint(0,0); bgImg.setOpacity(150); } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getSumNum() { return sumNum; } public void setSumNum(int sumNum) { this.sumNum = sumNum; } public CCSprite getDefaultImg() { return defaultImg; } public void setDefaultImg(CCSprite defaultImg) { this.defaultImg = defaultImg; } public CCSprite getBgImg() { return bgImg; } public void setBgImg(CCSprite bgImg) { this.bgImg = bgImg; } }
BaseElement.javathis
public abstract class BaseElement extends CCSprite{ private DieListener dieListener; public BaseElement(String filePath) { super(filePath); } public void setDieListener(DieListener dieListener) { this.dieListener = dieListener; } public abstract void baseAction(); public void destroy(){ if(dieListener!=null){ dieListener.onDie(this); } this.removeSelf(); } }
Bullet.javaspa
public abstract class Bullet extends Plant{ protected int attack = 10; protected int speed = 60; public Bullet(String filePath) { super(filePath); } public abstract void move(); public int getAttack() { return attack; } }
Product.java
public abstract class Product extends BaseElement{ public Product(String filePath) { super(filePath); } }
ProductPlant.java
public abstract class ProductPlant extends Plant{ public ProductPlant(String filePath) { super(filePath); } public abstract void create(); }
Zombie
public abstract class Zombie extends BaseElement{ protected CGPoint startPoint; protected CGPoint endPoint; protected int life = 50; protected int attack = 10; protected int speed = 10; public Zombie(String filePath) { super(filePath); setScale(0.5); setAnchorPoint(0.5f,0); } public abstract void move(); public abstract void attack(BaseElement element); public abstract void attacked(int attack); }
Plant.java
public abstract class Plant extends BaseElement{ protected int life = 100; protected int row ; // 行號 line protected int column; // 列號 row public Plant(String filePath) { super(filePath); setScale(0.65); setAnchorPoint(0.5f,0); } public void attacked(int attack){ life -= attack; if(life<0){ destroy(); } } public int getRow() { return row; } public void setRow(int row) { this.row = row; } public int getColumn() { return column; } public void setColumn(int column) { this.column = column; } public int getLife() { return life; } public void setLife(int life) { this.life = life; } }
AttackPlant.java
public abstract class AttackPlant extends Plant implements DieListener{ protected ArrayList<Bullet> bullets = new ArrayList<Bullet>(); public AttackPlant(String filePath) { super(filePath); } public ArrayList<Bullet> getBullets() { return bullets; } public abstract void createBullet(); @Override public void onDie(BaseElement element) { if(element instanceof Bullet){ bullets.remove(element); } } }
DefencePlant.java
public abstract class DefencePlant extends Plant { public DefencePlant(String filePath) { super(filePath); life = 200; } }
baseLayer
public class BaseLayer extends CCLayer{ protected CGSize winSize; public BaseLayer() { winSize = CCDirector.sharedDirector().getWinSize(); } }
welcomelayer
public class WelcomeLayer extends BaseLayer{ private static final int TAG_START = 10; private static final String TAG = "WelcomeLayer"; private CCSprite loading; private CCSprite start; public WelcomeLayer() { consumeTime(); init(); } public void consumeTime() { new AsyncTask<Void,Void,Void>(){ @Override protected Void doInBackground(Void... params) { SystemClock.sleep(5000); // 若是設置成2000,就會報空指針異常 return null; } protected void onPostExecute(Void result) { setIsTouchEnabled(true); loading.removeSelf(); WelcomeLayer.this.getChildByTag(TAG_START).setVisible(true); super.onPostExecute(result); }; }.execute(); } private void init() { CCSprite logo = CCSprite.sprite("image/popcap_logo.png"); logo.setPosition(winSize.width/2,winSize.height/2); this.addChild(logo); CCSequence sequence = CCSequence.actions(CCDelayTime.action(1), CCHide.action(), CCDelayTime.action(1),CCCallFunc.action(this, "loadInfo")); logo.runAction(sequence); } public void loadInfo(){ CCSprite bg = CCSprite.sprite("image/welcome.jpg"); bg.setAnchorPoint(0,0); this.addChild(bg); ArrayList<CCSpriteFrame> frames = null; loading = CCSprite.sprite("image/loading/loading_01.png"); loading.setPosition(winSize.width/2,25); this.addChild(loading); String filePath = "image/loading/loading_%02d.png"; CCAnimate animate = CommonUtil.getAnimate(frames , 9, filePath, 0.2f); loading.runAction(animate); start = CCSprite.sprite("image/loading/loading_start.png"); start.setPosition(winSize.width/2,25); this.addChild(start,0,TAG_START);// 在什麼狀況下須要增長TAG_START?在其餘類中訪問到 start.setVisible(false); } @Override public boolean ccTouchesEnded(MotionEvent event) { if(CommonUtil.isClick(event,this,start)){ CommonUtil.replaceScene(new MenuLayer()); } return super.ccTouchesEnded(event); } }
MenuLayer
public class MenuLayer extends BaseLayer { public MenuLayer() { init(); } private void init() { CCSprite bg = CCSprite.sprite("image/menu/main_menu_bg.jpg"); bg.setAnchorPoint(0,0); this.addChild(bg); CCMenu menu = CCMenu.menu(); CCMenuItemSprite item = CCMenuItemSprite.item(CCSprite.sprite("image/menu/start_adventure_default.png"), CCSprite.sprite("image/menu/start_adventure_press.png"), this,"onClick"); menu.addChild(item); menu.setScale(0.5f); menu.setRotation(5); menu.setPosition(winSize.width/2-25,winSize.height/2-110);// 在使用絕對像素的時候,對屏幕適配是否有影響 this.addChild(menu); } public void onClick(Object o){ CommonUtil.replaceScene(new FightLayer()); } }
FightLayer
public class FightLayer extends BaseLayer{ private static final String TAG = "FightLayer"; public static final int TAG_CHOSE = 10; private CCTMXTiledMap gameMap; private ArrayList<ShowZombie> showZombieList; private ArrayList<ShowPlant> showPlantList; private CopyOnWriteArrayList<ShowPlant> chosePlantList; private CCSprite choseContainer; private CCSprite chooseContainer; private HashMap<Integer,HashMap<String,String>> data; private CCSprite start; private CCSprite ready; public FightLayer() { init(); } private void init() { loadMap(); loadZombie(); moveMap(); } private void loadZombie() { ArrayList<CGPoint> loadPoint = CommonUtil.loadPoint(gameMap, "zombies"); showZombieList = new ArrayList<ShowZombie>(); for (CGPoint cgPoint : loadPoint) { ShowZombie showZombie = new ShowZombie(); showZombie.setPosition(cgPoint); gameMap.addChild(showZombie); showZombieList.add(showZombie); } } private void moveMap() { CGSize contentSize = gameMap.getContentSize(); CGPoint pos = CGPoint.ccp(winSize.width-contentSize.width, 0); CCMoveTo moveTo = CCMoveTo.action(1, pos); CCSequence sequence = CCSequence.actions(moveTo, CCCallFunc.action(this, "loadContainer")); gameMap.runAction(sequence); } public void loadContainer(){ choseContainer = CCSprite.sprite("image/fight/chose/fight_chose.png"); chooseContainer = CCSprite.sprite("image/fight/chose/fight_choose.png"); choseContainer.setPosition(0,winSize.height); choseContainer.setAnchorPoint(0,1); this.addChild(choseContainer,0,TAG_CHOSE);//TAG_CHOSE 方便在另一個類中訪問到 chooseContainer.setAnchorPoint(0,0); this.addChild(chooseContainer); loadPlant(); start = CCSprite.sprite("image/fight/chose/fight_start.png"); start.setPosition(chooseContainer.getContentSize().width/2,25); chooseContainer.addChild(start); } private void loadPlant() { //the picture width and height is 42*56 showPlantList = new ArrayList<ShowPlant>(); chosePlantList = new CopyOnWriteArrayList<ShowPlant>(); for (int i = 1; i <=9 ; i++) { ShowPlant showPlant = new ShowPlant(i); CCSprite defaultImg = showPlant.getDefaultImg(); CCSprite bgImg = showPlant.getBgImg(); defaultImg.setPosition(52*((i-1)%4)+25,175-((i-1)/4)*58); // from up to down ,so more and more low bgImg.setPosition(52*((i-1)%4)+25,175-((i-1)/4)*58); chooseContainer.addChild(defaultImg); chooseContainer.addChild(bgImg); showPlantList.add(showPlant); } setIsTouchEnabled(true); } @Override public boolean ccTouchesBegan(MotionEvent event) { //CGPoint touchPos = convertTouchToNodeSpace(event); // 肯定座標才須要轉換 //remove if(GameController.getInstance().isStart()){ GameController.getInstance().handlerTouch(event); }else{ if(CommonUtil.isClick(event, this, choseContainer)){ boolean isDel = false; for (ShowPlant item : chosePlantList) { if(CommonUtil.isClick(event, this, item.getDefaultImg())){ CGPoint pos = item.getBgImg().getPosition(); CCMoveTo moveTo = CCMoveTo.action(0.2f, pos); item.getDefaultImg().runAction(moveTo); chosePlantList.remove(item); isDel = true; } if(isDel){ //當前點擊的元素也會前移,可是接下來有moveTo動做 看到的效果是 moveTo後結果 item.getDefaultImg().setPosition(item.getDefaultImg().getPosition().x-52, item.getDefaultImg().getPosition().y); } } }else{ //add if(CommonUtil.isClick(event, this, start)){ setIsTouchEnabled(false); // Log.i(TAG, "start"); preGame(); }else{ if(chosePlantList.size()<5){ for (ShowPlant item : showPlantList) { if(CommonUtil.isClick(event, this, item.getDefaultImg())){ CGPoint pos = CGPoint.ccp(75+chosePlantList.size()*52, winSize.height-62); CCMoveTo moveTo = CCMoveTo.action(0.2f, pos); item.getDefaultImg().runAction(moveTo); chosePlantList.add(item); Log.i(TAG, chosePlantList.size()+""); } } } } } } return super.ccTouchesBegan(event); } private void preGame() { // ①回收掉玩家已有植物容器 chooseContainer.removeSelf(); choseContainer.setScale(0.65); // 父node改變大小,child的大小並不會隨之改變 for (ShowPlant item : chosePlantList){ CCSprite plant = item.getDefaultImg(); CGPoint pos = CGPoint.ccp(plant.getPosition().x*0.65, winSize.height-42); plant.setPosition(pos); plant.setScale(0.65); this.addChild(plant); } CGPoint pos = CGPoint.ccp(0, 0); CCMoveTo moveTo = CCMoveTo.action(1, pos); CCSequence sequence = CCSequence.actions(moveTo, CCCallFunc.action(this, "clearShowZombies")); gameMap.runAction(sequence); // ④序列幀播放:準備……安放……植物…… } // ②移動地圖 以後 clearShowZombies // ③回收掉展現用殭屍 public void clearShowZombies() { for (ShowZombie item : showZombieList) { item.removeSelf(); } showZombieList.clear(); showZombieList = null; ready(); } public void ready() { ready = CCSprite.sprite("image/fight/startready_01.png"); ready.setPosition(winSize.width/2,winSize.height/2); this.addChild(ready); ArrayList<CCSpriteFrame> frames = null; CCAnimate animate = CommonUtil.getAnimate(frames, 3, "image/fight/startready_%02d.png", 1f); CCSequence sequence = CCSequence.actions(animate, CCCallFunc.action(this, "go")); ready.runAction(sequence); } public void go(){ ready.removeSelf(); setIsTouchEnabled(true); GameController.getInstance().start(gameMap, chosePlantList); } private void loadMap() { gameMap = CommonUtil.loadMap("image/fight/map_day.tmx"); this.setPosition(0,0); this.addChild(gameMap); } }
CommonUtil.java
public class CommonUtil { //只播放一次 public static CCAnimate getAnimate(ArrayList<CCSpriteFrame> frames,int num,String filePath,float interval){ if(frames == null){ frames = new ArrayList<CCSpriteFrame>(); for (int i = 1; i <= num; i++) { CCSpriteFrame frame = CCSprite.sprite(String.format(filePath, i)).displayedFrame(); frames.add(frame); } } CCAnimation animation = CCAnimation.animation("",interval,frames); CCAnimate animate = CCAnimate.action(animation,false); return animate; } //永久播放 public static CCAction getRepeatForeverAction(ArrayList<CCSpriteFrame> frames,int num,String filePath,float interval){ if(frames == null){ frames = new ArrayList<CCSpriteFrame>(); for (int i = 1; i <= num; i++) { CCSpriteFrame frame = CCSprite.sprite(String.format(filePath, i)).displayedFrame(); frames.add(frame); } } CCAnimation animation = CCAnimation.animation("",interval,frames); CCAnimate animate = CCAnimate.action(animation); return CCRepeatForever.action(animate); } public static boolean isClick(MotionEvent event,CCLayer layer,CCNode node) { CGPoint touchPos = layer.convertTouchToNodeSpace(event); if(CGRect.containsPoint(node.getBoundingBox(), touchPos)){ return true; } return false; } //replace scene public static void replaceScene(CCLayer layer){ CCScene scene = CCScene.node(); scene.addChild(layer); CCFadeTransition transition = CCFadeTransition.transition(1, scene); CCDirector.sharedDirector().replaceScene(transition); } public static CCTMXTiledMap loadMap(String map){ CCTMXTiledMap tiledMap = CCTMXTiledMap.tiledMap(map); return tiledMap; } public static ArrayList<CGPoint> loadPoint(CCTMXTiledMap map,String name){ CCTMXObjectGroup objectGroupNamed = map.objectGroupNamed(name); ArrayList<HashMap<String, String>> objects = objectGroupNamed.objects; ArrayList<CGPoint> list = new ArrayList<CGPoint>(); for (HashMap<String, String> item : objects) { Float x = Float.parseFloat(item.get("x")); Float y = Float.parseFloat(item.get("y")); list.add(CGPoint.ccp(x, y)); } return list; } }
GameController.java
public class GameController { private static final String TAG = "GameController"; private static GameController instance = new GameController(); private boolean isStart = false; private FightLayer layer; private CCTMXTiledMap gameMap; private CGPoint[][] towers = new CGPoint[5][9]; private static ArrayList<FightLine> fightLines; private List<ShowPlant> chosePlantList; static { fightLines = new ArrayList<FightLine>(); for (int i = 0; i <= 4; i++) { FightLine fightLine = new FightLine(i); fightLines.add(fightLine); } } public static GameController getInstance() { return instance; } public boolean isStart() { return isStart; } public void setStart(boolean isStart) { this.isStart = isStart; } public void gameOver(){ // isStart = false; //切換界面 } public void start(CCTMXTiledMap gameMap,List<ShowPlant> chosePlantList) { isStart = true; this.gameMap = gameMap; this.chosePlantList = chosePlantList; this.layer = (FightLayer) gameMap.getParent(); loadPlantPos(); // false 表示不中止 addZombies 是 this 中的方法 CCScheduler.sharedScheduler().schedule("addZombies", this, 5, false); } private void loadPlantPos() { for (int i = 1; i <= 5; i++) { CCTMXObjectGroup objectGroupNamed = gameMap.objectGroupNamed(String .format("tower%02d", i)); for (int j = 0; j < objectGroupNamed.objects.size(); j++) { HashMap<String, String> item = objectGroupNamed.objects.get(j); towers[i - 1][j] = CGPoint.ccp(Integer.parseInt(item.get("x")), Integer.parseInt(item.get("y"))); } } } public void addZombies(float t) { Random random = new Random(); int lineNum = random.nextInt(5); int startIndex = lineNum * 2; int endIndex = lineNum * 2 + 1; ArrayList<CGPoint> pos = CommonUtil.loadPoint(gameMap, "road"); PrimaryZombies zombie = new PrimaryZombies(pos.get(startIndex), pos.get(endIndex)); this.layer.addChild(zombie,1); fightLines.get(lineNum).addZombies(zombie); } private ShowPlant currentShowPlant ; private Plant currentPlant ; public void handlerTouch(MotionEvent event) { //選擇植物 if(CommonUtil.isClick(event, layer, (CCSprite) layer.getChildByTag(FightLayer.TAG_CHOSE))){ for(ShowPlant item : chosePlantList){ if(CommonUtil.isClick(event, layer, item.getDefaultImg())){ currentShowPlant = item; currentShowPlant.getDefaultImg().setOpacity(100); switch (item.getId()) { case 4: currentPlant = new PlantNut(); break; case 2: currentPlant = new PlantSun(); break; case 1: currentPlant = new PlantPease(); break; } } } }else{ //安放植物 if(currentShowPlant!=null){ // 第一層判斷:安放區域(水平方向1-9;垂直方向1-5) if(isBuild(layer.convertTouchToNodeSpace(event))){ addPlant(); } } } //收集陽光 } private boolean isBuild(CGPoint touchPos) { int blockColumn = (int) (touchPos.x/46); int blockRow = (int) ((CCDirector.sharedDirector().getWinSize().height-touchPos.y)/54); Log.i(TAG, blockRow+"--"+blockColumn); if(blockRow>=1&&blockRow<=5){ if(blockColumn>=1&&blockColumn<=9){ int row = blockRow - 1; int column = blockColumn -1; currentPlant.setRow(row); currentPlant.setColumn(column); currentPlant.setPosition(towers[row][column]); return true; } } return false; } private void addPlant() { int row = currentPlant.getRow(); FightLine fightLine = fightLines.get(row); //第二層判斷 重疊植物 if(fightLine.isAdd(currentPlant)){ fightLine.addPlant(currentPlant); this.layer.addChild(currentPlant); currentShowPlant.getDefaultImg().setOpacity(255); currentShowPlant = null; currentPlant = null; } } }
FightLine.java
public class FightLine implements DieListener { private static final String TAG = "FightLine"; private int lineNum; private List<Zombie> zombieList; private List<AttackPlant> attackPlantList; private HashMap<Integer, Plant> plants; public FightLine(int lineNum) { this.lineNum = lineNum; zombieList = new ArrayList<Zombie>(); plants = new HashMap<Integer, Plant>(); attackPlantList = new ArrayList<AttackPlant>(); CCScheduler.sharedScheduler() .schedule("attackPlant", this, 0.5f, false); CCScheduler.sharedScheduler().schedule("attackZombie", this, 0.5f, false); CCScheduler.sharedScheduler().schedule("checkBullet", this,0.2f, false); } public void checkBullet(float t){ if(zombieList.size()>0&&attackPlantList.size()>0){ for (AttackPlant item : attackPlantList) { ArrayList<Bullet> bullets = item.getBullets(); for (Bullet bullet : bullets) { for (Zombie zombie : zombieList) { float left = zombie.getPosition().x-20 ; float right = zombie.getPosition().x+20 ; float x = bullet.getPosition().x; if(x>=left && x<=right){ bullet.destroy(); zombie.attacked(bullet.getAttack()); } } } } } } public void addZombies(PrimaryZombies zombie) { zombie.setDieListener(this); zombieList.add(zombie); } public void addPlant(Plant plant) { plant.setDieListener(this); Log.i(TAG, "" + plant.getColumn()); plants.put(plant.getColumn(), plant); if (plant instanceof AttackPlant) { attackPlantList.add((AttackPlant) plant); } } public boolean isAdd(Plant plant) { return !plants.containsKey(plant.getColumn()); } public void attackZombie(float t){ if(zombieList.size()>0&&attackPlantList.size()>0){ for(AttackPlant item:attackPlantList){ item.createBullet(); } } } // 殭屍攻擊植物 public void attackPlant(float t){ if(plants.size()>0&&zombieList.size()>0){ for (Zombie item : zombieList) { int key = (int) item.getPosition().x/46 -1 ; Plant plant = plants.get(key); if(plant != null){ item.attack(plant); } } } } @Override public void onDie(BaseElement element) { if(element instanceof Plant){ int key =((Plant) element).getColumn(); plants.remove(key); if(element instanceof AttackPlant){ attackPlantList.remove(element); //爲何上面不能直接 remove(element) } }else{ zombieList.remove(element); } } }
DieListener.java
public interface DieListener { void onDie(BaseElement baseElement); }
PrimaryZombies.java
public class PrimaryZombies extends Zombie { private static final String TAG = "PrimaryZombies"; private static ArrayList<CCSpriteFrame> shakeFrame; private static ArrayList<CCSpriteFrame> moveFrame; private static ArrayList<CCSpriteFrame> attackFrame; public PrimaryZombies() { super("image/zombies/zombies_1/walk/z_1_01.png"); baseAction(); } public PrimaryZombies(CGPoint startIndex, CGPoint endIndex) { super("image/zombies/zombies_1/walk/z_1_01.png"); startPoint = startIndex; endPoint = endIndex; this.setPosition(startIndex); move(); } @Override public void baseAction() { CCAction repeatForeverAction = CommonUtil.getRepeatForeverAction( shakeFrame, 2, "image/zombies/zombies_1/shake/z_1_%02d.png", 0.2f); this.runAction(repeatForeverAction); } @Override public void move() { float t = CGPointUtil.distance(getPosition(), this.endPoint) / speed; Log.i(TAG, t + ""); CCMoveTo moveTo = CCMoveTo.action(t, this.endPoint); CCSequence sequence = CCSequence.actions(moveTo, CCCallFunc.action(this, "gameOver")); this.runAction(sequence); this.runAction(CommonUtil.getRepeatForeverAction(moveFrame, 7, "image/zombies/zombies_1/walk/z_1_%02d.png", 0.2f)); } public void gameOver() { destroy(); GameController.getInstance().gameOver(); } private boolean isAttack = false; private BaseElement target; @Override public void attack(BaseElement element) { isAttack = true; target = element; this.stopAllActions(); CCAction repeatForeverAction = CommonUtil.getRepeatForeverAction( attackFrame, 10, "image/zombies/zombies_1/attack/z_1_attack_%02d.png", 0.2f); this.runAction(repeatForeverAction); CCScheduler.sharedScheduler() .schedule("attackPlant", this, 0.5f, false); } public void attackPlant(float t) { if (target instanceof Plant) { Plant plant = (Plant) target; plant.attacked(attack); if (plant.getLife() < 0) { CCScheduler.sharedScheduler().unschedule("attackPlant", this); this.stopAllActions(); move(); } } } private boolean isDie = false; private static ArrayList<CCSpriteFrame> headFrame;// 頭掉下來 private static ArrayList<CCSpriteFrame> dieFrame;// 趴着地上 private static String headRes = "image/zombies/zombies_1/head/z_1_head_%02d.png"; private static String dieRes = "image/zombies/zombies_1/die/z_1_die_%02d.png"; @Override public void attacked(int attack) { life-= attack; if(life<0&&!isDie){ isDie = true; this.stopAllActions(); CCAnimate head = CommonUtil.getAnimate(headFrame, 6, headRes, 0.2f); CCAnimate die = CommonUtil.getAnimate(headFrame, 6, dieRes, 0.2f); CCSequence sequence = CCSequence.actions(head, die,CCCallFunc.action(this, "destroy")); this.runAction(sequence); } } }
PlantPease
public class PlantPease extends AttackPlant { private ArrayList<CCSpriteFrame> shakeFrames; public PlantPease() { super("image/plant/pease/p_2_01.png"); baseAction(); } @Override public void baseAction() { CCAction repeatForeverAction = CommonUtil.getRepeatForeverAction(shakeFrames, 8, "image/plant/pease/p_2_%02d.png", 0.2f); this.runAction(repeatForeverAction); } @Override public void createBullet() { if(bullets.size()<1){ CommonPease pease = new CommonPease(); pease.setPosition(getPosition().x+20,getPosition().y+35); pease.setDieListener(this); this.getParent().addChild(pease); bullets.add(pease); pease.move(); } } }
PlantNut.java
public class PlantNut extends DefencePlant { private ArrayList<CCSpriteFrame> shakeFrame; public PlantNut() { super("image/plant/nut/p_3_01.png"); } @Override public void baseAction() { this.runAction(CommonUtil.getRepeatForeverAction(shakeFrame, 11, "image/plant/nut/p_3_%02d.png", 0.2f)); } }