問題:從新進入屏幕的時候 方向 變成豎屏 node
加上這無效 android:configChanges="orientation|screenSize|keyboardHidden" android
應該加上這個android:screenOrientation="landscape"ide
在一個九曲十八彎的小路上,一個殭屍冒着大雪前行。最後雪停了,殭屍唱歌this
// DemoLayer : 主Layer // PauseLayer : 暫停Layer public class DemoLayer extends CCLayer { public static final String TAG = "DemoLayer"; private List<CGPoint> roads; private CCTMXTiledMap gameMap; public DemoLayer() { setIsTouchEnabled(true); // 設置屏幕能夠被點擊 init(); } private void init() { loadMap(); loadRoad(); move(); animate(); particleSystem();//使用粒子系統後幀率降低, //自動 地圖的移動 // this.runAction(CCFollow.action(sprite)); //始終保持 //手動地圖移動 } private CCParticleSystem system; private void particleSystem() { system = CCParticleSnow.node(); system.setTexture(CCTextureCache.sharedTextureCache().addImage("f.png")); this.addChild(system); } //添加序列幀以後 座標發生了偏移 緣由是 z_1_attack_01.png 和z_1_01.png 兩張圖片寬度不同 private void animate() { ArrayList<CCSpriteFrame> frames = new ArrayList<CCSpriteFrame>(); String fileName = "z_1_0%d.png"; // 1-9 // String fileName = "z_1_%02d.png"; // 1-99 for (int i = 1; i <= 7; i++) { CCSpriteFrame frame = CCSprite.sprite(String.format(fileName,i)).displayedFrame(); frames.add(frame); } CCAnimation anim = CCAnimation.animation("",0.2f,frames); CCAnimate animate = CCAnimate.action(anim); CCRepeatForever forever = CCRepeatForever.action(animate); sprite.runAction(forever); } private List<CCMoveTo> moveList = new ArrayList<CCMoveTo>(); private CCSprite sprite; private void move() { sprite = getSprite(); //CCMoveTo moveTo = CCMoveTo.action(2, roads.get(1)); // sprite.runAction(moveTo); /* * for (int i = 0; i < roads.size(); i++) { for循環執行很快,沒有停頓,直接從起點到終點了 * CCMoveTo moveTo = CCMoveTo.action(2,roads.get(i)); * sprite.runAction(moveTo); } */ /*方法一: for (int i = 0; i < roads.size(); i++) { CCMoveTo moveTo = CCMoveTo.action(2, roads.get(i)); moveList.add(moveTo); } * CCSequence sequence = CCSequence.actions(moveList.get(0), * moveList.get(1), moveList.get(2), moveList.get(3), moveList.get(4), * moveList.get(5), moveList.get(6), moveList.get(7)); */ // 這樣會很耗時 ,會等集合添加完畢後才執行序列 // 方法二:遞歸 ,執行完第一個動做後再生成第二個動做 // target.getClass().getMethod(selector) moveToNext(); } private int current = 0; private int speed = 50; public void moveToNext(){ // 必定要是public方法 current++; if(current < roads.size()){ float time = CGPointUtil.distance(sprite.getPosition(),roads.get(current))/speed;//這樣就能夠勻速的跑 CCMoveTo moveTo = CCMoveTo.action(time, roads.get(current)); CCSequence sequence = CCSequence.actions(moveTo, CCCallFunc.action(this, "moveToNext")); sprite.runAction(sequence); }else{ // current = 0; // CCPlace place = CCPlace.action(roads.get(current)); // CCSequence sequence = CCSequence.actions(place, CCCallFunc.action(this, "moveToNext")); // sprite.runAction(sequence); //走到終點 讓粒子系統 停下來,同時殭屍也停下來 system.stopSystem(); sprite.stopAllActions(); //播放聲音 聲音播放不了 報錯 錯誤log 刷屏太快 ,抓都都不到 //CCGLSurfaceView surfaceView = new CCGLSurfaceView(this); 點進源碼查看 // SoundEngine.sharedEngine().playSound(CCDirector.theApp, R.raw.ylzs, false); } } //手動移動地圖 要實現 ccTouchesMoved 方法 private void loadMap() { gameMap = CCTMXTiledMap.tiledMap("map.tmx"); // 若是要手動移動地圖,將錨點設置到中心點上,修改地圖的座標(中點) gameMap.setAnchorPoint(0.5f,0.5f); CGSize size = gameMap.getContentSize(); gameMap.setPosition(size.width/2,size.height/2); this.addChild(gameMap); // 設置屏幕能夠被點擊 } private void loadRoad() { roads = new ArrayList<CGPoint>(); CCTMXObjectGroup objectGroup = gameMap.objectGroupNamed("road"); ArrayList<HashMap<String, String>> objects = objectGroup.objects; for (HashMap<String, String> item : objects) { int x = Integer.parseInt(item.get("x")); int y = Integer.parseInt(item.get("y")); roads.add(CGPoint.ccp(x, y)); } } public CCSprite getSprite() { CCSprite sprite = CCSprite.sprite("z_1_attack_01.png"); sprite.setAnchorPoint(0, 0); sprite.setScale(.3); sprite.setFlipX(true); sprite.setPosition(roads.get(0)); // this.addChild(sprite); // 爲了保證殭屍和road不錯位 gameMap.addChild(sprite); return sprite; } @Override public boolean ccTouchesBegan(MotionEvent event) { //顯示暫停Layer PauseLayer layer = new PauseLayer(); //必須添加到場景中去 this.getParent().addChild(layer); // this.addChild(layer); 若是寫成這樣,他會伴隨的主Layer凍結,再也接收不到touch了 //主Layer處於凍結狀態 this.onExit(); //凍結狀態 + setIsTouchEnable(false) return super.ccTouchesBegan(event); } private class PauseLayer extends CCLayer{ CCSprite heart; public PauseLayer() { this.setIsTouchEnabled(true); heart = getHeart(); CGSize winSize = CCDirector.sharedDirector().getWinSize(); heart.setPosition(winSize.width/2,winSize.height/2); } private CCSprite getHeart(){ CCSprite heart = CCSprite.sprite("heart.png"); this.addChild(heart); return heart; } @Override public boolean ccTouchesBegan(MotionEvent event) { CGPoint touchPos = this.convertTouchToNodeSpace(event); if(CGRect.containsPoint(heart.getBoundingBox(),touchPos)){ //遊戲繼續,銷燬當前的Layer this.removeSelf(); // this.setVisible(false); 這樣寫 當前界面上有多個能夠處理touch的layer 會崩潰 DemoLayer.this.onEnter();//touch還原 } return super.ccTouchesBegan(event); } } //用於手動移動地圖 /*@Override public boolean ccTouchesMoved(MotionEvent event) { gameMap.touchMove(event, gameMap); return super.ccTouchesMoved(event); }*/ }