Create a heart-to-heart picture with physical effects
Create a physical scene first
1Local Gamescene =class("Gamescene", function ()2--Body3--returnDisplay.newscene ("Gamescene")4--create a scene with a physical effect5 returnDisplay.newphysicsscene ("Gamescene")6End
Add physical heart to this physical world
1--Gets the physical world object bound in the scene2Self.world =Self:getphysicsworld ();3--set the acceleration of gravity in a scene4Self.world:setGravity (CC.P (0, -98.0 ) ) ;5--set debug state where physical effects can be seen6 Self.world:setDebugDrawMask (CC. Physicsworld.debugdraw_all);7 8 9--Create a heart and heart with physical effectsTenSelf.heart = Display.newsprite ("Image/pic/heart.png") One--set the location of the heart ASelf.heart:setPosition (display.cx, Display.top-self.heart:getContentSize (). Height) ---add hearts to the scene - self.heart:addTo (self) the ---Set the density, bounce force, and friction of the object, ---Set it all to0to make no physical changes in the event of a collision. -Local material_default = cc. Physicsmaterial (0.0,0.0,0.0 ) +--1 -Local Heartbody =cc. Physicsbody:createcircle ( +Self.heart:getContentSize (). Width/2, Material_default A ) at--adding a physical engine to an object - self.heart:setPhysicsBody (heartbody) ---shielding the body of the physical world from the effects of gravity ---Self.heart:getPhysicsBody (): Setgravityenable (false)
Here the cc.PhysicsBody::createCircle()
method is called to create a circle body;
Createcircle (RADIUS, material, offset),
The Createcircle method has three parameters, namely:
- The parameter 1 is the Cc.size type, which represents the outer radius of the circle body.
- The parameter 2 is CC. The physicsmaterial type, which represents the properties of the physical material,
- CC is the default. Physicsbody_material_default.
- This parameter can also be customized, as follows:
cc.PhysicsMaterial(density, restitution, friction)
- Density: Indicates density
- Restitution: Indicates a rebound force
- Friction: Indicates friction
- The parameter 3 is the CC.P type, and it is an optional parameter that represents the offset of the body from the center point, which is CC.P (0,0) by default.
The 26th line, after the comment
Non-annotated
quick-cocos2d-x3.3 Study (12)---------Create a rounded object with a physical engine effect