游戏引擎cocos2d-android使用大全

做手机游戏需要三个核心的类,即:SurfaceView,SurfaceHolder,Thread。帧数要在30帧左右是最好的。

cocos2d游戏引擎

封装好的框架,可直接使用
cocos2d-android (用java编程)

导演:控制场景的切换,控制开始和暂停
场景:添加图层

图层:添加精灵

精灵

全屏:
application:

android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" 

Activity:
android:screenOrientation="landscape"

图片放到assess目录下

    CCGLSurfaceView surfaceView=new CCGLSurfaceView(this);  
    setContentView(surfaceView);

    director=CCDirector.sharedDirector();//程序只能有一个导演
    director.attachInView(surfaceView); //开启线程
    CCScene ccScene=CCScene.node();//为了api和cocos-phone一致
    ccScene.addChild(new FirstLayer());
    director.runWithScene(ccScene);    

CCDirector

  1. attachInView 开启线程
  2. runWithScene 运行场景
  3. 生命周期的方法 resume pause end
  4. setDisplayFPS 显示帧率
  5. setScreenSize 设置屏幕大小 自动屏幕适配
  6. setDeviceOrientation 设置屏幕的方向

    director.setDeviceOrientation(CCDirector.kCCDeviceOrientationLandscapeLeft);
    director.setDisplayFPS(true);  //是否显示帧率
    director.setAnimationInterval(1.0f/30); //锁定帧率,只能向下锁定
    director.setScreenSize(480, 320); //设置屏幕的大小,自动屏幕适配,必须在开启线程之后调用

CCScene

目前该类没有什么具体的功能,它只是作为一个项目中需要展示内容的根节点存在。

CCLayer

安卓的坐标原点是在左上角,而cocos2d坐标原点在左下角

//按下的事件
@Override
public boolean ccTouchesBegan(MotionEvent event) {
//先把android坐标系中的点转换为cocos2d坐标系中的点
    CGPoint converTouchToNodeSpace=this.convertTouchToNodeSpace(event);
    //  System.out.println("我被按了,呜呜呜!");
    CGRect boundingBox=ccSprite.getBoundingBox();  //获取精灵的矩形
    event.getRawX();
    //判断点是否在矩形之中
    boolean containsPoint=CGRect.containsPoint(boundingBox, converTouchToNodeSpace);
    if(containsPoint){
        ccSprite.setScale(ccSprite.getScale()+0.2);

    }else{
        ccSprite.setScale(ccSprite.getScale()-0.2);
    }

    return super.ccTouchesBegan(event);
}

CCSprite

  1. setAnchorPoint 设置锚点
  2. setPosition 设置坐标
  3. setScale 设置缩放
  4. setFlipX(Y) 设置水平(垂直)翻转
  5. ccSprite.setOpacity(150); 设置不透明度 值越大越不透明
  6. setVisible , 设置是否可见 true可见

  ccSprite = CCSprite.sprite("z_1_attack_01.png");
        ccSprite.setAnchorPoint(0,0);// 设置锚点
        ccSprite.setPosition(100, 100);//设置坐标
        ccSprite.setScale(1); // 设置缩放
        //ccSprite.setFlipY(true);// X水平翻转  Y垂直翻转
        //ccSprite.setOpacity(0);//设置不透明度 值越大 越不透明  0-255
        ccSprite.setVisible(true);// 设置不可显示
        // 把精灵添加到图层上 

CCNode

是 场景 图层 和精灵的基类

  1. ccp(float x,float y) 静态方法 创建一个点
  2. this.addChild(ccSprite);
  3. this.addChild(ccSprite, z); // 2 优先级
  4. this.addChild(ccSprite, 1, 10);// 参数3 标签

动作的处理,CCAction

时间是秒

CCMoveTo ccMoveTo=CCMoveTo.action(2,CCNode.ccp(200, 0));//参数1;移动的时间,单位秒,参数2:移动的距离

1.CCFiniteTimeAction 和时间相关的动作

CCInstantAction   瞬时动作    闪现 
CCIntervalAction  延迟动作  疾跑

2.CCFollow 跟随动作
3.CCRepeatForever 永不停止的循环

    CCRepeatForever forver=CCRepeatForever.action(sequence);

4.CCSpeed 和速度相关的动作

各种动作

moveTo和moveBy的区别:

1.to 移动到指定目的地;2.by 移动的坐标的改变

1 to没有相反的动作;2.by 有相反动作

CCSequence 串行动作

CCMoveBy ccMoveBy=CCMoveBy.action(2,CCNode.ccp(200, 0));//参数1;移动的时间,单位秒,参数2:移动的距离
    CCMoveBy reverse=ccMoveBy.reverse();
    CCSequence sequence=CCSequence.actions(ccMoveBy,reverse);
    //串行动作,
    sprite.runAction(sequence);

跳跃动作

    CCJumpBy ccJumpBy=CCJumpBy.action(2,ccp(200,200),50,1);
    sprite.runAction(ccJumpBy);

旋转和缩放
>
缩放:
CCScaleBy ccScaleBy=CCScaleBy.action(1,0.5f);//基于锚点进行缩放


旋转:CCRotateBy和CCRotateTo都可以,同时CCRotateTo更实用(翻转120度)

贝赛尔曲线运动

CCBezierConfig cc=new CCBezierConfig();
    cc.controlPoint_1=ccp(0,0);
    cc.controlPoint_2=ccp(100,100);
    cc.endPosition=ccp(200,0);

    CCBezierBy bezierBy=CCBezierBy.action(2,cc);
    getSprite().runAction(bezierBy);

淡入淡出

CCFadeIn  fadeIn=CCFadeIn.action(10);
    getSprite().runAction(fadeIn);

加速度

CCMoveTo cmt=CCMoveTo.action(10,CCNode.ccp(200,0));
    CCEaseOut easeOut=CCEaseOut.action(cmt,10);//让移动按照一定加速度去移动
    getSprite().runAction(easeOut);

颜色渐变
CCLabel,专门用来显示文字的精灵

CCLabel label=CCLabel.labelWithString("指令汇最棒", "hkbd.ttf", 24);
    label.setColor(ccc3(50,0,255));
    label.setPosdition(200,200);
    this.adChild(label);

    ccColor3B c=ccc3(100,255,-100);

    CCTintBy cb=CCTintBy.action(1, c);
    CCTintBy reverse=cb.reverse();
    CCSequence seq=CCSequence.actions(cb, reverse);
    CCRepeatForever forver=CCRepeatForever.action(seq);
    label.runAction(forver);

闪硕

CCBlink blink=CCBlink.action(3, 3);
    getSprite().runAction(blink);   

复杂的动作

  1. CCSpawn 并行动作
  2. CCSequence 串行动作
  3. CCRepeatForever 永不停止的循环

private void JumpBy() {
        // 1 时间  单位秒 2 目的地   3 高出的高度  4 跳跃的次数
        CCJumpBy ccJumpBy=CCJumpBy.action(4, ccp(200, 100), 100, 2);
        CCRotateBy ccRotateBy=CCRotateBy.action(2, 360);
        // 并行动作
        CCSpawn ccSpawn=CCSpawn.actions(ccJumpBy, ccRotateBy);//并行起来了   跳跃的过程中伴随着旋转

        //CCJumpBy reverse = ccJumpBy.reverse();
        CCSequence sequence=CCSequence.actions(ccSpawn, ccSpawn.reverse());// 跳上去 跳回来(伴随着旋转) 
        CCRepeatForever forever=CCRepeatForever.action(sequence);// 让串行动作 永不停止循环了
        CCSprite sprite = getSprite();
        sprite.setAnchorPoint(0.5f, 0.5f);  
        sprite.setPosition(50, 50);
        sprite.runAction(forever);
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值