In the previous article we once talked about the use of various actions in the Cocos2d-x, we will first briefly review the content related to the Action 1. files for all the category classes in the Cocos2d-x are located under the Cocos2d-x-2.2.1/cocos2dx/actions directory 2. CCAction is the base class of all category classes. All category classes are derived from CCAction 3. the CCFiniteTimeAction class defines actions that can be completed within a limited period of time. Most implementation classes of CCAction are derived from CCFiniteTimeAction, which provides us with the reverse method in CCFiniteTimeAction, this method can obtain an action opposite to the original action (not all actions have inverse actions. CCFiniteTimeAction is derived from two classes: CCActionInstant and CCActionInterval. in the CCActionInterval action Including CCMoveTo/By CCJumpTo/By CCBezierTo/By CCScaleTo/By CCRotateTo/By, and so on. The example we use today-circular motion is a type of continuous action, next we will encapsulate this action.
First, we need to know the principle of circular motion. Any image can be seen as a polygon, a circle. We can split it into several points, it can be 100 or 200. The so-called circular motion is to find the coordinates of the time points in the next frame during each frame refreshing, it is one of the 200 points. That is to say, the circular motion is actually to divide a circle into several coordinate points, so that nodes can switch constantly on these coordinates. What we need to do is to find the coordinates of these points. Let's take a look at how the coordinates are obtained:
As shown in, the coordinates of any point P on the circle can be obtained through the radius r and angle.
Next we will package a class of circular motion: within the specified time, let a node motion 360 degrees around a center in the specified radius. Create a CCCircleBy class that inherits from CCActionInterval. The. h file code is as follows:
The. cpp file code is as follows:
After completing the above operations, we can use the create () method of CCCircleBy to create an action:
Running the project, we will find that the genie performs a uniform circular motion around the center (480,320), with a radius of 50 and a time of 2 seconds.