Cocos2d-x Physics 3-dropped Avatar

Source: Internet
Author: User
Tags addchild

Add a genie and let it fall from the height

BoolHelloWorld: init ()

{

? ? //////////////////////////////

? ? // 1. super init first

? ? If (! Layer: init ())

? ? {

? ? ? ? Return false;

? ? }

?? ?

# If CC_ENABLE_BOX2D_INTEGRATION

? ? Log ("Box2d enabled ");

# Endif

?? ?

? ? Size visibleSize = Director: getInstance ()-> getVisibleSize ();

? ? Vec2 origin = Director: getInstance ()-> getVisibleOrigin ();

?? ?

?? ?

? ?? // Create an Genie

? ? Auto sprite = Sprite: create ("Icon-58.png ");

? ? // Create a rigid body

? ? Auto spriteBody = PhysicsBody: createBox (sprite-> getContentSize (), PHYSICSBODY_MATERIAL_DEFAULT );

? ? // Assign a rigid body to the Genie, and then the genie has the physical characteristics

? ? Sprite-> setPhysicsBody (spriteBody );

? ? // Set the location of the Genie

? ? Sprite-> setPosition (visibleSize. width/2, visibleSize. height/3*2 );

? ? // Add the genie to the layer

? ? This-> addChild (sprite );

?? ?

? ? This-> addChild (sprite );

?

...

}

?

The createBox method has three parameters:

1. Rectangular area size

2. Set Materials

3. Edge width

?

The Sprite material also uses the default PHYSICS_MATERIAL_DEFAUTL instance.

?

The running result shows that there is a sprite falling from the height to the speed, and it will rebound when it hits the bottom.

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

Custom PhysicsMaterial class value

Auto spriteBody = PhysicsBody: createBox (sprite-> getContentSize (), PhysicsMaterial (0.0f, 1.2f, 0.2f ));

?

Density = 0.0f

Recovery Force = 1.2f

Friction = 0.2f

?

Run again, and the Genie is playing higher at the bottom.

?

?

?

Cocos2d-x Physics 3-dropped Avatar

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.