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