效果:
主控制器:
- public class MainActivity extends ActionBarActivity {
- private CCDirector director;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- CCGLSurfaceView surfaceView=new CCGLSurfaceView(this);
- setContentView(surfaceView);
- // 程序只能有一个导演
- director = CCDirector.sharedDirector();
- director.attachInView(surfaceView);// 开启线程
- director.setDeviceOrientation(CCDirector.kCCDeviceOrientationLandscapeLeft);// 设置游戏方向 水平
- director.setDisplayFPS(true);//是否展示帧率
- // director.setAnimationInterval(1.0f/30);// 锁定帧率 指定一个帧率 向下锁定
- director.setScreenSize(480,320);//设置屏幕的大小 可以自动屏幕适配
- CCScene ccScene=CCScene.node();// 为了api 和cocos-iphone 一致
- ccScene.addChild(new ActionLayer());//场景添加了图层
- director.runWithScene(ccScene);// 运行场景
- }
- @Override
- protected void onResume() {
- super.onResume();
- director.resume();
- }
- @Override
- protected void onPause() {
- super.onPause();
- director.onPause();
- //director.pause();
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- director.end();// 游戏结束了
- }
- }
场景一:
- public class FirstLayer extends CCLayer {
- private CCSprite ccSprite;
- public FirstLayer() {
- setIsTouchEnabled(true);// 打开触摸事件开关
- init();
- }
- // 按下的事件
- @Override
- public boolean ccTouchesBegan(MotionEvent event) {
- // 先把android坐标系中的点 转换成 cocos2d坐标系中的点
- CGPoint convertTouchToNodeSpace = this.convertTouchToNodeSpace(event);
- System.out.println("我被按下了");
- CGRect boundingBox = ccSprite.getBoundingBox(); // 获取精灵的矩形
- // 判断点是否在矩形之中
- // 参数1 矩形 参数2 点
- boolean containsPoint = CGRect.containsPoint(boundingBox,convertTouchToNodeSpace);
- if(containsPoint){
- ccSprite.setScale(ccSprite.getScale()+0.2);
- }else{
- ccSprite.setScale(ccSprite.getScale()-0.2);
- }
- //this.getChildByTag(10); 根据Tag标签 找对应的孩子
- return super.ccTouchesBegan(event);
- }
- private void init() {
- CCSprite bg = CCSprite.sprite("bbg_arena.jpg");
- bg.setAnchorPoint(0,0);
- this.addChild(bg,0); // 如果第二个参数越大 默认显示的越靠上面,如果一样大 谁先添加谁显示在下面
- 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);// 设置不可显示
- //ccSprite.
- // 把精灵添加到图层上
- this.addChild(ccSprite);
- // this.addChild(ccSprite,z); // 优先级
- this.addChild(ccSprite,1,10);// 参数3 标签
- }
- }
场景二:
- public class ActionLayer extends CCLayer {
- public ActionLayer(){
- init();
- }
- private void init() {
- // moveTo();
- // moveBy();
- JumpBy();
- //scaleBy();
- //rotateBy();
- //rotateTo();
- //bezierBy();
- //fadeIn();
- //ease(); //和加速度有关系的动作
- //tint();
- //blink();
- }
- private void blink() {
- // 三秒钟闪烁3次
- CCBlink blink=CCBlink.action(3,3);
- getSprite().runAction(blink);
- }
- private void tint() {
- // 专门显示文字的精灵
- // 参数1 显示的内容 参数2 字体的样式 3 字体的大小
- CCLabel label=CCLabel.labelWithString("那些年,我们在工地上的苦逼日子","hkbd.ttf",24);
- label.setColor(ccc3(50,255));
- label.setPosition(200,200);
- this.addChild(label);
- ccColor3B c=ccc3(100,255,-100);
- // 参数1 时间 参数2 变化后的颜色
- CCTintBy by=CCTintBy.action(1,c);
- CCTintBy reverse = by.reverse();
- CCSequence actions = CCSequence.actions(by,reverse);
- CCRepeatForever forever=CCRepeatForever.action(actions);
- label.runAction(forever);
- }
- private void ease() {
- CCMoveTo ccMoveTo=CCMoveTo.action(10,CCNode.ccp(200,0));
- CCEaseIn eaSEOut=CCEaseIn.action(ccMoveTo,9); // 让移动按照有一定加速度去移动
- getSprite().runAction(eaSEOut);
- }
- private void fadeIn() {
- CCFadeIn fadeIn=CCFadeIn.action(10);
- getSprite().runAction(fadeIn);
- }
- private void bezierBy() {
- CCBezierConfig c=new CCBezierConfig();
- c.controlPoint_1=ccp(0,0);
- c.controlPoint_2=ccp(100,100);
- c.endPosition=ccp(200,0);
- CCBezierBy bezierBy=CCBezierBy.action(2,c);
- getSprite().runAction(bezierBy);
- }
- private void rotateTo() {
- CCSprite heart = getHeart();
- heart.setPosition(200,100);
- CCRotateTo rotateTo=CCRotateTo.action(3,240); //偷懒的做法
- heart.runAction(rotateTo);
- }
- private void rotateBy() {
- // 参数2 旋转的角度
- CCRotateBy by=CCRotateBy.action(3,240);
- getHeart().runAction(by);
- }
- private void scaleBy() {
- // 参数1 时间 参数2 缩放的比例
- CCScaleBy ccScaleBy=CCScaleBy.action(1f,0.65f); //基于锚点进行缩放
- CCScaleBy reverse = ccScaleBy.reverse();
- CCSequence sequence=CCSequence.actions(ccScaleBy,reverse);
- CCRepeatForever forever=CCRepeatForever.action(sequence);
- getHeart().runAction(forever);
- }
- 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);
- }
- private void moveBy() {
- CCSprite sprite = getSprite();
- sprite.setPosition(0,100);
- // 参数1 移动的时间 单位秒 参数2 坐标的改变
- CCMoveBy ccMoveBy=CCMoveBy.action(2,0)); //
- CCMoveBy reverse = ccMoveBy.reverse();
- CCSequence sequence=CCSequence.actions(ccMoveBy,reverse);//CCSequence 串行动作
- sprite.runAction(sequence);
- }
- public void moveTo() {
- CCSprite sprite = getSprite();
- // 参数1 移动的时间 单位秒 参数2 移动的目的地
- CCMoveTo ccMoveTo=CCMoveTo.action(2,0));
- CCIntervalAction reverse = ccMoveTo.reverse();
- CCSequence sequence=CCSequence.actions(ccMoveTo,reverse);//CCSequence 串行动作
- sprite.runAction(sequence);
- }
- public CCSprite getSprite() {
- CCSprite sprite=CCSprite.sprite("z_1_attack_01.png");
- sprite.setAnchorPoint(0,0);
- this.addChild(sprite);
- return sprite;
- }
- public CCSprite getHeart() {
- CCSprite sprite=CCSprite.sprite("heart.png");
- sprite.setPosition(100,100);
- this.addChild(sprite);
- return sprite;
- }
- }