Cocos2d-x tutorial (16)-Custom Action circular motion

Source: Internet
Author: User

 

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.