• cocos2d-小游戏


    问题:重新进入屏幕的时候 方向 变成竖屏  

    加上这无效 android:configChanges="orientation|screenSize|keyboardHidden" 

    应该加上这个android:screenOrientation="landscape"

    在一个九曲十八弯的小路上,一个僵尸冒着大雪前行。最后雪停了,僵尸唱歌

    // 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);
        }*/
    
    }

     

  • 相关阅读:
    arcims(HtmlView)开发经验总结《转》
    Oracle sequence
    ajax 简介
    PHP:路在何方?
    ArcIMS初级教程(4)
    设计开发必须收藏的资源网站
    Win2008+IIS7.0+VS2008 在测试调试网站时报错,紧急求救!
    动态生成客户端数组
    解决MySQL不允许从远程访问的方法
    MySql中delimiter的作用是什么
  • 原文地址:https://www.cnblogs.com/cherryhimi/p/4104196.html
Copyright © 2020-2023  润新知