cocos2d-html5学习笔记(七)--Action

来源:互联网 发布:excel03查找重复数据 编辑:程序博客网 时间:2024/05/22 05:28

Action实在太多了,有些我没有用过,这里只讲常用的Action。关于Action的其他知识,请参考陈升想的两篇关于action的教程

cocos2d-html5教程之动作CCAction

cocos2d-html5教程之动作CCAction详解2cocos2d-html5教程之动作CCAction详解2

好的,先来一个最简单的Action

[javascript] view plaincopyprint?
  1. var SimpleActon=cc.Layer.extend({ 
  2.     init:function  () { 
  3.         var green=cc.LayerColor.create(cc.c4(0,255,0,255),320,480); 
  4.         var flower= cc.Sprite.create("Resources/flower.png"); 
  5.  
  6.         //菊花太大,缩小一点,并移动一下 
  7.         flower.setScale(0.1); 
  8.         flower.setPosition(cc.ccp(100,100)); 
  9.  
  10.         //一个旋转的Action,2秒转一圈 
  11.         var actionBy = cc.RotateBy.create(2, 360); 
  12.  
  13.         //重复运行Action,不断的转圈 
  14.         flower.runAction(cc.RepeatForever.create(actionBy)); 
  15.  
  16.         this.addChild(green,1,1); 
  17.         this.addChild(flower,2,2); 
  18.  
  19.         this.setTouchEnabled(true); 
  20.         return true
  21.     } 
  22. }); 

动态的效果,图片就看不到了,不过可以看一下菊花:

好的,再来一个复杂一点的。

[javascript] view plaincopyprint?
  1. var ActionTest=cc.Layer.extend({ 
  2.     actionArray:[], 
  3.     sq:null
  4.     init:function  () { 
  5.         var green=cc.LayerColor.create(cc.c4(0,255,0,255),320,480); 
  6.         var flower= cc.Sprite.create("Resources/flower.png"); 
  7.  
  8.         //菊花太大,缩小一点,并移动一下 
  9.         flower.setScale(0.1); 
  10.         flower.setPosition(cc.p(100,100)); 
  11.  
  12.         //一个旋转的Action,2秒转一圈 
  13.         var actionBy = cc.RotateBy.create(2, 360); 
  14.  
  15.         //重复运行Action,不断的转圈 
  16.         flower.runAction(cc.RepeatForever.create(actionBy)); 
  17.  
  18.         this.addChild(green,1,1); 
  19.         this.addChild(flower,2,2); 
  20.  
  21.         this.setTouchEnabled(true); 
  22.         return true
  23.     }, 
  24.     ccTouchesEnded:function (touches,event) { 
  25.  
  26.         //获得点击位置,添加一个MoveTo的Action,菊花1秒内移动到这个位置 
  27.         var position=touches[0].locationInView(); 
  28.         var action=cc.MoveTo.create(1,position); 
  29.         this.actionArray.push(action); 
  30.         //添加一个回调函数,其实也是一个Action 
  31.         this.actionArray.push(cc.CallFunc.create(this,this.callback,this.actionArray.length+1)); 
  32.  
  33.         //是否是第一次 
  34.         if (this.sq==null) { 
  35.             this.sq=cc.Sequence.create(this.actionArray); 
  36.             this.getChildByTag(2).runAction(this.sq); 
  37.             return
  38.         }; 
  39.  
  40.         //上一次是否运行结束,是的话就运行本次点击的Action 
  41.         if (this.sq.isDone()) { 
  42.             this.callback(this,this.actionArray.length-2); 
  43.         }; 
  44.     }, 
  45.     callback:function (sender,next) { 
  46.  
  47.         //是否是最后一个Action,是的话就返回,否则运行下两个个Action(第二个是回调函数Action,cc.CallFunc) 
  48.         if (next==this.actionArray.length)return
  49.  
  50.         this.sq=cc.Sequence.create(this.actionArray[next],this.actionArray[next+1]); 
  51.         this.getChildByTag(2).runAction(this.sq); 
  52.     } 
  53. }); 

这个例子中的init部分与上一个例子是一样的,详细的我不讲了。这个例子的效果是,鼠标点击哪里,菊花就飘到哪里。如果菊花还在飘,你又点击的话,要等到上一次的Action完成之后才运行下一个Action,想一下魔兽的角色是怎么移动的。