About the relationship of nodes and sprites in cocos2d-x and initialization

Source: Internet
Author: User

1. Each object class has a CREATE function of its own public (equivalent to Create_func), and the Init function.

The 2.create function returns its own type, and the Init function is called automatically when the Ceate function is called.

3. If it is a scene object class, also public a static cocos2d::scene* Createscene (), the function, the return value is the scene type

If we're going to present a moving man,

1. Define an object class that inherits from node

2. Define a private Sprite * Sprite Property

3. In the INIT function

Node::init (); Executing the parent class method

Set the size, scale, body size of the node
Size S = size (44, 52);
Setcontentsize (s);
Setphysicsbody (Physicsbody::createbox (s));
Setscale (0.5);

Set the sprite's texture, size, zoom, Position
Sprite = Sprite::create ("S_1.png");
Sprite->setcontentsize (s);
Sprite->setscale (0.5);
Sprite->setposition (22, 52); Set to coincide with node

Bind the Genie to the node, and the genie becomes the child node of the node.

This->addchild (sprite);

Performing animations

Animation * Animation = Animation::create ();
Animation->addspriteframewithfile ("S_1.png");
Animation->addspriteframewithfile ("S_2.png");
Animation->addspriteframewithfile ("S_3.png");
Animation->addspriteframewithfile ("S_4.png");
Animation->addspriteframewithfile ("S_5.png");
Animation->addspriteframewithfile ("S_6.png");
Animation->setdelayperunit (0.1f);
Animation->setrestoreoriginalframe (TRUE);
Animating an action
Auto animate = animate::create (animation);
Sprite->runaction (Repeatforever::create (animate));

Set Rigid Body Properties

Getphysicsbody ()->setrotationenable (false);
Getphysicsbody ()->setcontacttestbitmask (1);
return true;

4. When we create a node, call the initialization method, and return a node that contains the sprite child node that performs the sprite animation, which achieves the effect we want, and we operate on the node, it contains the child node sprite will follow the move

About the relationship of nodes and sprites in cocos2d-x and initialization

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.