cocos2d-android_02_熟悉基本的类

前端之家收集整理的这篇文章主要介绍了cocos2d-android_02_熟悉基本的类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

效果:






主控制器:

  1. public class MainActivity extends ActionBarActivity {
  2. private CCDirector director;
  3.  
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. CCGLSurfaceView surfaceView=new CCGLSurfaceView(this);
  8. setContentView(surfaceView);
  9. // 程序只能有一个导演
  10. director = CCDirector.sharedDirector();
  11. director.attachInView(surfaceView);// 开启线程
  12. director.setDeviceOrientation(CCDirector.kCCDeviceOrientationLandscapeLeft);// 设置游戏方向 水平
  13. director.setDisplayFPS(true);//是否展示帧率
  14. // director.setAnimationInterval(1.0f/30);// 锁定帧率 指定一个帧率 向下锁定
  15. director.setScreenSize(480,320);//设置屏幕的大小 可以自动屏幕适配
  16. CCScene ccScene=CCScene.node();// 为了api 和cocos-iphone 一致
  17. ccScene.addChild(new ActionLayer());//场景添加了图层
  18. director.runWithScene(ccScene);// 运行场景
  19. }
  20. @Override
  21. protected void onResume() {
  22. super.onResume();
  23. director.resume();
  24. }
  25. @Override
  26. protected void onPause() {
  27. super.onPause();
  28. director.onPause();
  29. //director.pause();
  30. }
  31. @Override
  32. protected void onDestroy() {
  33. super.onDestroy();
  34. director.end();// 游戏结束了
  35. }
  36.  
  37. }



场景一:

  1. public class FirstLayer extends CCLayer {
  2. private CCSprite ccSprite;
  3.  
  4.  
  5. public FirstLayer() {
  6. setIsTouchEnabled(true);// 打开触摸事件开关
  7. init();
  8. }
  9. // 按下的事件
  10. @Override
  11. public boolean ccTouchesBegan(MotionEvent event) {
  12. // 先把android坐标系中的点 转换成 cocos2d坐标系中的点
  13. CGPoint convertTouchToNodeSpace = this.convertTouchToNodeSpace(event);
  14. System.out.println("我被按下了");
  15. CGRect boundingBox = ccSprite.getBoundingBox(); // 获取精灵的矩形
  16. // 判断点是否在矩形之中
  17. // 参数1 矩形 参数2 点
  18. boolean containsPoint = CGRect.containsPoint(boundingBox,convertTouchToNodeSpace);
  19. if(containsPoint){
  20. ccSprite.setScale(ccSprite.getScale()+0.2);
  21. }else{
  22. ccSprite.setScale(ccSprite.getScale()-0.2);
  23. }
  24. //this.getChildByTag(10); 根据Tag标签 找对应的孩子
  25. return super.ccTouchesBegan(event);
  26. }
  27. private void init() {
  28. CCSprite bg = CCSprite.sprite("bbg_arena.jpg");
  29. bg.setAnchorPoint(0,0);
  30. this.addChild(bg,0); // 如果第二个参数越大 默认显示的越靠上面,如果一样大 谁先添加显示在下面
  31. ccSprite = CCSprite.sprite("z_1_attack_01.png");
  32. ccSprite.setAnchorPoint(0,0);// 设置锚点
  33. ccSprite.setPosition(100,100);//设置坐标
  34. ccSprite.setScale(1); // 设置缩放
  35. //ccSprite.setFlipY(true);// X水平翻转 Y垂直翻转
  36. //ccSprite.setOpacity(0);//设置不透明度 值越大 越不透明 0-255
  37. ccSprite.setVisible(true);// 设置不可显示
  38. //ccSprite.
  39. // 把精灵添加到图层上
  40. this.addChild(ccSprite);
  41. // this.addChild(ccSprite,z); // 优先级
  42. this.addChild(ccSprite,1,10);// 参数3 标签
  43. }
  44. }






场景二:

  1. public class ActionLayer extends CCLayer {
  2. public ActionLayer(){
  3. init();
  4. }
  5.  
  6. private void init() {
  7. // moveTo();
  8. // moveBy();
  9. JumpBy();
  10. //scaleBy();
  11. //rotateBy();
  12. //rotateTo();
  13. //bezierBy();
  14. //fadeIn();
  15. //ease(); //和加速度有关系的动作
  16. //tint();
  17. //blink();
  18. }
  19.  
  20. private void blink() {
  21. // 三秒钟闪烁3次
  22. CCBlink blink=CCBlink.action(3,3);
  23. getSprite().runAction(blink);
  24. }
  25.  
  26. private void tint() {
  27. // 专门显示文字的精灵
  28. // 参数1 显示内容 参数2 字体的样式 3 字体的大小
  29. CCLabel label=CCLabel.labelWithString("那些年,我们在工地上的苦逼日子","hkbd.ttf",24);
  30. label.setColor(ccc3(50,255));
  31. label.setPosition(200,200);
  32. this.addChild(label);
  33. ccColor3B c=ccc3(100,255,-100);
  34. // 参数1 时间 参数2 变化后的颜色
  35. CCTintBy by=CCTintBy.action(1,c);
  36. CCTintBy reverse = by.reverse();
  37. CCSequence actions = CCSequence.actions(by,reverse);
  38. CCRepeatForever forever=CCRepeatForever.action(actions);
  39. label.runAction(forever);
  40. }
  41.  
  42. private void ease() {
  43. CCMoveTo ccMoveTo=CCMoveTo.action(10,CCNode.ccp(200,0));
  44. CCEaseIn eaSEOut=CCEaseIn.action(ccMoveTo,9); // 让移动按照有一定加速度去移动
  45. getSprite().runAction(eaSEOut);
  46. }
  47.  
  48. private void fadeIn() {
  49. CCFadeIn fadeIn=CCFadeIn.action(10);
  50. getSprite().runAction(fadeIn);
  51. }
  52.  
  53. private void bezierBy() {
  54. CCBezierConfig c=new CCBezierConfig();
  55. c.controlPoint_1=ccp(0,0);
  56. c.controlPoint_2=ccp(100,100);
  57. c.endPosition=ccp(200,0);
  58. CCBezierBy bezierBy=CCBezierBy.action(2,c);
  59. getSprite().runAction(bezierBy);
  60. }
  61.  
  62. private void rotateTo() {
  63. CCSprite heart = getHeart();
  64. heart.setPosition(200,100);
  65. CCRotateTo rotateTo=CCRotateTo.action(3,240); //偷懒的做法
  66. heart.runAction(rotateTo);
  67. }
  68.  
  69. private void rotateBy() {
  70. // 参数2 旋转的角度
  71. CCRotateBy by=CCRotateBy.action(3,240);
  72. getHeart().runAction(by);
  73. }
  74.  
  75. private void scaleBy() {
  76. // 参数1 时间 参数2 缩放的比例
  77. CCScaleBy ccScaleBy=CCScaleBy.action(1f,0.65f); //基于锚点进行缩放
  78. CCScaleBy reverse = ccScaleBy.reverse();
  79. CCSequence sequence=CCSequence.actions(ccScaleBy,reverse);
  80. CCRepeatForever forever=CCRepeatForever.action(sequence);
  81. getHeart().runAction(forever);
  82. }
  83.  
  84. private void JumpBy() {
  85. // 1 时间 单位秒 2 目的地 3 高出的高度 4 跳跃的次数
  86. CCJumpBy ccJumpBy=CCJumpBy.action(4,ccp(200,100),100,2);
  87. CCRotateBy ccRotateBy=CCRotateBy.action(2,360);
  88. // 并行动作
  89. CCSpawn ccSpawn=CCSpawn.actions(ccJumpBy,ccRotateBy);//并行起来了 跳跃的过程中伴随着旋转
  90. //CCJumpBy reverse = ccJumpBy.reverse();
  91. CCSequence sequence=CCSequence.actions(ccSpawn,ccSpawn.reverse());// 跳上去 跳回来(伴随着旋转)
  92. CCRepeatForever forever=CCRepeatForever.action(sequence);// 让串行动作 永不停止循环了
  93. CCSprite sprite = getSprite();
  94. sprite.setAnchorPoint(0.5f,0.5f);
  95. sprite.setPosition(50,50);
  96. sprite.runAction(forever);
  97. }
  98.  
  99. private void moveBy() {
  100. CCSprite sprite = getSprite();
  101. sprite.setPosition(0,100);
  102. // 参数1 移动的时间 单位秒 参数2 坐标的改变
  103. CCMoveBy ccMoveBy=CCMoveBy.action(2,0)); //
  104. CCMoveBy reverse = ccMoveBy.reverse();
  105. CCSequence sequence=CCSequence.actions(ccMoveBy,reverse);//CCSequence 串行动作
  106. sprite.runAction(sequence);
  107. }
  108.  
  109. public void moveTo() {
  110. CCSprite sprite = getSprite();
  111. // 参数1 移动的时间 单位秒 参数2 移动的目的地
  112. CCMoveTo ccMoveTo=CCMoveTo.action(2,0));
  113. CCIntervalAction reverse = ccMoveTo.reverse();
  114. CCSequence sequence=CCSequence.actions(ccMoveTo,reverse);//CCSequence 串行动作
  115. sprite.runAction(sequence);
  116. }
  117.  
  118. public CCSprite getSprite() {
  119. CCSprite sprite=CCSprite.sprite("z_1_attack_01.png");
  120. sprite.setAnchorPoint(0,0);
  121.  
  122. this.addChild(sprite);
  123. return sprite;
  124. }
  125. public CCSprite getHeart() {
  126. CCSprite sprite=CCSprite.sprite("heart.png");
  127. sprite.setPosition(100,100);
  128. this.addChild(sprite);
  129. return sprite;
  130. }
  131. }

猜你在找的Cocos2d-x相关文章