[Cocos2d-js official documentation] five, Cocos2d-JS v3.0 New Action API

New method of action

Previously, when we need to repeat an action, we need to:

sprite.runAction(cc.Repeat.create(action, 2)); 

The above code creates a new object is repackaged Repeat action, so that regardless of semantically or the code more difficult to understand. Why can not we be like jQuery as simple and convenient to use the original action it?

So we as action in the new version adds a new method:

sprite.runAction(action.repeat(2));

To increase the circulation action only after the action .repeat (), the same as before without the need to re-generate an action, is not it more convenient? ^. ^

In addition, we also for the relevant class action, adding a more simple way to create, by class name to lowercase first letter will be able to create a new object:

 var action = cc.moveBy(2,cc.p(10,10));

The above code is equivalent to:

var action =  cc.MoveBy.create(2, cc.p(10, 10))

See here, it must be very worried about one thing - compatibility. . .

In fact, we do not have too much to worry about, the old method is still supported drops.

Why add API

Just mentioned how call the new method, but why we added a new method in the new version in the existing set of relatively mature program it?

In fact, it was all for simpler, more in line with people's habits, as well as make the code look more clear.

Chain syntax allows us to just produce an object, then you can achieve a variety of different functions by calling the object's method:

var action = cc.RotateTo.create(0.5, 90).repeat(5).speed(0.5); 

or:

  1.  
    var action = cc.RotateTo.create(0.5, 90);
  2.  
    action. repeat(5);
  3.  
    action.speed( 0.5);

Even no contact cocos2d-html5 user, the code can be read at a first time - a create action, then the execution speed and the number of repetitions for this action is provided.

Let's compare the old method:

  1.  
    var action = cc.RotateTo.create(0.5, 90);
  2.  
    var action1 = cc.Repeat.create(action, 2);
  3.  
    var action2 = cc.Speed.create(action1, 0.5);

The old method of generating three objects, but also caused a slight contamination on the code. In reading and writing are not as efficient new approach to reality.

Moreover, because the object is to regenerate the redundant no longer needed, so the initialization speed is also a little bit of improvement.

So although the new version still support the old way, but we suggest that you follow a new way to write programs.

New API list

In addition to repeat the above-mentioned method and speed, we also added the following methods.

The old call usage Corresponding new method
cc.Repeat.create(action, num) action.repeat(num)
cc.RepeatForever.create(action) action.repeatForever()
cc.Speed.create(action, speed) action.speed(speed)
cc.Speed.setSpeed(speed) action.setSpeed(speed)
cc.Speed.getSpeed() action.getSpeed()
cc.EaseIn.create(action, rate) action.easing(cc.easeIn(rate))
cc.EaseOut.create(action, rate) action.easing(cc.easeOut(rate))
cc.EaseInOut.create(action, rate) action.easing(cc.easeInOut(rate))
cc.EaseExponentialIn.create(action) action.easing(cc.easeExponentialIn())
cc.EaseExponentialOut.create(action) action.easing(cc.easeExponentialOut())
cc.EaseExponentialInOut.create(action) action.easing(cc.easeExponentialInOut())
cc.EaseSineIn.create(action) action.easing(cc.easeSineIn())
cc.EaseSineOut.create(action) action.easing(cc.easeSineOut())
cc.EaseSineInOut.create(action) action.easing(cc.easeSineInOut())
cc.EaseElasticIn.create(action) action.easing(cc.easeElasticIn())
cc.EaseElasticOut.create(action) action.easing(cc.easeElasticOut())
cc.EaseElasticInOut.create(action, rate) action.easing(cc.easeElasticInOut(rate))
cc.EaseBounceIn.create(action) action.easing(cc.easeBounceIn())
cc.EaseBounceOut.create(action) action.easing(cc.easeBounceOut())
cc.EaseBounceInOut.create(action) action.easing(cc.easeBounceInOut())
cc.EaseBackIn.create(action) action.easing(cc.easeBackIn())
cc.EaseBackOut.create(action) action.easing(cc.easeBackOut())
cc.EaseBackInOut.create(action) action.easing(cc.easeBackInOut())

Some examples:

  1.  
    EaseIn:
  2.  
    var move = cc.MoveBy.create(2, cc.p(winSize.width - 80, 0)).easing(cc.easeIn(2.0));
  3.  
    sprite.runAction(move);
  4.  
     
  5.  
    RepeatForever:
  6.  
    var move = cc.MoveBy.create(2, cc.p(winSize.width - 80, 0)).RepeatForever();
  7.  
    sprite.runAction(move);

Precautions

Repeat twice using the repeat / speed action object is a method, the execution result is multiplied by the set value.

  1.  
    var action = cc.RotateTo.create(0.5, 90);
  2.  
     
  3.  
    // speed of 6
  4.  
    action.speed( 2).speed(3);
  5.  
    action.getSpeed() ==> 6;
  6.  
     
  7.  
    // the number of repeat of 6

action.repeat(2).repeat(3);

 

转载请注明:https://blog.csdn.net/qinning199/article/details/40452193

 

Guess you like

Origin www.cnblogs.com/wodehao0808/p/11929570.html
Recommended