CosCos-android 代码总结

CosCos-android 代码总结

资料:

  Android游戏开发视频教程

  Cocos-android-1代码包下载

 直接上代码:

MainActivity中Cocos常规写法:

CosCos-android 代码总结
//cocos2d 会把图形绘制在 view 上
    private CCGLSurfaceView view = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        view = new CCGLSurfaceView(this);
        //得到CCDirector  一个应用程序只有一个
        CCDirector director = CCDirector.sharedDirector();
        //设置应用程序相关的属性 设置游戏程序中所使用的View对象
        director.attachInView(view);
        //设置游戏是否显示FPS值,看应用程序是否流畅
        director.setDisplayFPS(true);
        //设置游戏FPS值,渲染一帧的时间 
        director.setAnimationInterval(1/30);
        
        //生成一个游戏场景对象
        CCScene scene =CCScene.node();
        //生成布景层对象
        GameLayer gameLayer = new GameLayer();
        
        //将布景层对象添加到游戏场景中
        scene.addChild(gameLayer);
        //运行游戏场景
        director.runWithScene(scene);
        setContentView(view);
View Code

继承了CCLayer的图层类的基本写法:

CosCos-android 代码总结
public class GameLayer extends CCLayer {
    // 声明一个精灵对象
    CCSprite player;

    public GameLayer() {
        // 初始化精灵对象
        player = CCSprite.sprite("tank_a.png");

        // 设置精灵位置的两种方法

        /*
         * //方法一: 设置精灵对象的位置 player.setPosition(100, 100);
         */

        // 方法二:设置对象 常用于表示坐标,或者是表示向量
        CGPoint point = CGPoint.ccp(100, 100);
        player.setPosition(point);
        
        this.addChild(player);
        
        //生成一个JumpTo,该对象表示一个跳跃的动作
        CGPoint target = CGPoint.ccp(400, 100);
        CCJumpTo jumpTo = CCJumpTo.action(3,target,200,10);

        player.runAction(jumpTo);
        
    }
}
View Code

 继承了CCLayer的图层类的几个动作:精灵移动 旋转 缩放到指定倍数 闪烁 隐藏(CCHide) 显示

CosCos-android 代码总结
// 初始化精灵对象
        player = CCSprite.sprite("tank_a.png");
        this.addChild(player);
        player.setPosition(100, 100);
        /*** 例子一 :精灵翻转 ***/
        // 1.生成 动作对象
        /*
         CCFlipX flipX = CCFlipX.action(true); CCFlipY flipY =
         CCFlipY.action(true); CCHide hide = CCHide.action();
         */
        // 2.执行精灵对象执行动作对象
        /*
         player.runAction(flipX); player.runAction(flipY);
         player.runAction(hide);
         */

        /*** 基础延时动作 CCMoveTo和CCScaleTo ***/

        /*** 例子二 :精灵移动 ***/
        /*
         CGPoint mPoint = CGPoint.ccp(400,400); CCMoveTo moveTo =
         CCMoveTo.action(3, mPoint); player.runAction(moveTo);
         */

        /*** 例子三:精灵旋转 ***/
        /*
          CCRotateTo rotateTo = CCRotateTo.action(3,-90);
          player.runAction(rotateTo);
         */

        /*** 例子四:缩放到指定倍数 ***/
        /*** 第一个参数为:倍数,x:y是缩放比例 ***/
        /*CCScaleTo scale = CCScaleTo.action(5, 10, 4);
        player.runAction(scale);*/
        
        /***     例子五:闪烁CCBink    ***/
        /*CCBlink     bink =CCBlink.action(10, 10);
        player.runAction(bink);*/
        
        /***    例子六:CCShow    ***/
        /*CCShow show = CCShow.action();
        player.runAction(show);*/
    }
View Code

继承了CCLayer的图层类的动作执行方式:动作依次执行 多个动作同时执行 以及精灵清理技巧

CosCos-android 代码总结
    // 初始化精灵对象
        playerA = CCSprite.sprite("tank_a.png");
        playerB = CCSprite.sprite("tank_b.png");
        
        this.addChild(playerA);
        this.addChild(playerB);
        
        CGPoint point = CGPoint.ccp(100,100);
        playerA.setPosition(point);
        playerB.setPosition(point);
        
        CGPoint detaPoint = CGPoint.ccp(300, 300);
        
        CCMoveTo moveTo = CCMoveTo.action(2, detaPoint);
        CCRotateTo rotateTo = CCRotateTo.action(2,90);
        CCScaleTo scaleTo = CCScaleTo.action(2, 2);        
        
        /***    例一:多少动作依次执行     ***/
        /*CCSequence seq = CCSequence.actions(moveTo, rotateTo,scaleTo);
        playerA.runAction(seq);*/
        
        /***    例二:多个动作同时执行    ***/
        /*CCSpawn spawn  = CCSpawn.actions(moveTo, rotateTo,scaleTo);
        playerA.runAction(spawn);*/
        
        /***    例三:常用于清理精灵,与CCSequence联用    ***/
        /*CCCallFuncN    func = CCCallFuncN.action(this, "onActionFinished");
        CCSequence seq = CCSequence.actions(moveTo, func);
        playerA.runAction(seq);*/
        
        /***    例四:CCFollow    :***/
        
        
    }
    public void onActionFinished(Object sender)
    {
        System.out.println("onActionFinished called");
    }
View Code

继承了CCLayer的图层类的向量的加、减、乘:

CosCos-android 代码总结
// 声明一个精灵对象
    CCSprite playerA;
    CCSprite playerB;
    public GameLayer() {
        // 初始化精灵对象
        playerA = CCSprite.sprite("tank_a.png");
        playerB = CCSprite.sprite("tank_b.png");
        
        this.addChild(playerA);
        this.addChild(playerB);
        
        CGPoint point = CGPoint.ccp(0, 400);
        playerA.setPosition(point);
        playerB.setPosition(point);
        
        CGPoint detaPoint = CGPoint.ccp(200, 300);
        /*//向量加法
        CGPoint targetPoint1  = CGPoint.ccpAdd(point, detaPoint);
        playerB.setPosition(targetPoint1);
        
        //向量减法
        CGPoint targetPoint2  = CGPoint.ccpSub(point, detaPoint);
        playerB.setPosition(targetPoint2);
        
        //向量乘法
        CGPoint targetPoint3  = CGPoint.ccpMult(point, (float) 1.3);
        playerB.setPosition(targetPoint3);
        
        //计算单位向量
        CGPoint targetPoint4  = CGPoint.ccpNormalize(point);
        playerB.setPosition(targetPoint4);*/
        
        //MoveBy 第二个参数为增量
        /*CCMoveBy moveBy  = CCMoveBy.action(3,detaPoint);
        playerA.runAction(moveBy);*/
        
        //JumpBy 第二个参数为增量
        /*CCJumpBy    jumpBy = CCJumpBy.action(3, detaPoint, 200, 3);
        playerA.runAction(jumpBy);*/
    }
View Code

继承了CCLayer的图层类的 精灵淡入(颜色变化到指定值、颜色变化到相对值) 精灵淡出 重复指定动作N次数 指定动作,重复次数无限

CosCos-android 代码总结
// 声明一个精灵对象
    CCSprite playerA;
    CCSprite playerB;
    public GameLayer_Animale() {
        // 初始化精灵对象
        playerA = CCSprite.sprite("tank_a.png");
        this.addChild(playerA);
        
        CGPoint point = CGPoint.ccp(100,300);
        playerA.setPosition(point);

        //使精灵淡入
        //ccColor3B color3b = ccColor3B.ccc3(0,-300,-300);
        //例一:使精灵的颜色变化到指定值
        /*CCTintTo tintTo = CCTintTo.action(3, color3b);
        playerA.runAction(tintTo);*/

        //例二:使精灵的颜色变化到相对值:在当前颜色上加值
        /*CCTintBy tintBy = CCTintBy.action(3,color3b);
        playerA.runAction(tintBy);*/
        
        //例三:使精灵淡入CCFadenIn
        /*CCFadeIn fadeIn = CCFadeIn.action(3);
        playerA.runAction(fadeIn);*/
        
        //例三:使精灵淡出CCFadenOut
        /*    CCFadeOut fadeOut = CCFadeOut.action(3);
                playerA.runAction(fadeOut);*/
        
        //例四:
        /*CGPoint targetPoint1 = CGPoint.ccp(400, 300);
        CGPoint targetPoint2 = CGPoint.ccp(200,300);
        CCMoveTo    moveTo1 = CCMoveTo.action(2,targetPoint1);
        CCMoveTo    moveTo2 = CCMoveTo.action(2,targetPoint2);
        CCSequence seq = CCSequence.actions(moveTo1,moveTo2);*/
        //用一重复指定动作,重复次数
    /*CCRepeat repeat = CCRepeat.action(seq, 5);
        playerA.runAction(repeat);*/
        
        //用一重复指定动作,重复次数无限
        /*CCRepeatForever repeatForever = CCRepeatForever.action(seq);
        playerA.runAction(repeatForever);*/
View Code

继承了CCLayer的图层类的 触摸事件

CosCos-android 代码总结
// 声明一个精灵对象
        CCSprite playerA;
        
        //当用户开始触摸屏幕时,执行此方法
        @Override
        public boolean ccTouchesBegan(MotionEvent event) {
            // TODO Auto-generated method stub
            System.out.println("began");
            return super.ccTouchesBegan(event);
        }
    
        //当用户手指离开时,执行该方法
        @Override
        public boolean ccTouchesEnded(MotionEvent event) {
            // TODO Auto-generated method stub
            float x  = event.getX();
            float y = event.getY();
            
            CGPoint p1 = CGPoint.ccp(x, y);
            
            //左上角的坐标,转换成右下角的坐标
            CGPoint p3=CCDirector.sharedDirector().convertToGL(p1);
            
            System.out.println("转换后:"+p3.x+","+p3.y);
            System.out.println("转换前:("+x+","+y+") = end");
            return super.ccTouchesEnded(event);
        }
    
        //当用户在屏幕上移动时,执行该方法
        @Override
        public boolean ccTouchesMoved(MotionEvent event) {
            // TODO Auto-generated method stub
            System.out.println("move");
            return super.ccTouchesMoved(event);
        }
        
    //接收触摸事件,首先必须对当前图层进行设置
    public GamerLayer_Touch()
    {
        //对当前图层进行设置处理触摸事件
        this.setIsTouchEnabled(true);
        playerA = CCSprite.sprite("tank_a.png");
    
        this.addChild(playerA);
    }
View Code

继承了CCLayer的图层类的 按时间执行指定函数

CosCos-android 代码总结
/**
 * @author FDAorangebook 日期:2014-4-11 功能:
 */
public class GameLayer_Time extends CCLayer {
    public GameLayer_Time() {
        this.setIsTouchEnabled(true);
        // 调用schedule方法,传输函数名,以及间隔时间
        this.schedule("fun", 2);
    }

    public void fun(float delta) {
        System.out.println("fun is called"+delta);
    }

    /* (non-Javadoc)
     * @see org.cocos2d.layers.CCLayer#ccTouchesBegan(android.view.MotionEvent)
     */
    @Override
    public boolean ccTouchesBegan(MotionEvent event) {
        // TODO Auto-generated method stub
        this.unschedule("fun");
        System.out.println("unschedule");
        return super.ccTouchesBegan(event);
    }

}
View Code

注:其中图片资源要放在这此只是基本的用法,其它的用法 还要看源码。

要源示例代码的请顶起!

 

 

CosCos-android 代码总结,布布扣,bubuko.com

CosCos-android 代码总结

上一篇:android 显示大图片


下一篇:VUE笔记三 Axios异步通信