Coco2d-x 3.0 game instance learning notes "parkour" 2 game interface-brand new 3.0 physical world

Source: Internet
Author: User

Cocos2d-x3.0 mobile game development C ++ instance Parkour

From here on, there are many differences with those of our predecessors.

In mainscene, in the start button, we will use the callback function to enter our game scenario.

So in the game scene we define: playscene, and is a scenario with a physical world, cocos2d-x 3.0 to create a physical scene is very simple, because it all help us encapsulate.

In this step, we mainly complete the following functions in playscene:

1. Create a physical world

2. Create a physical rigid body on the ground

Personal opinions on related knowledge:

So-called CreationPhysical WorldI personally think it is an abstract stuff. As a newbie, I cannot understand it at first. At first, you can understand it as a game scene. It just has its own features.

The so-calledRigid BodyFirst, you can think that it is an entity that can be seen and touched without being changed by the Force. As we know, game characters and objects are represented by genie, which is something you can see and cannot touch. For example, if there are two genie, they will go face-to-face and overlap, and the backend will be placed and drawn, that is, you will see the backend genie blocking the front genie. In the game cool, both the main character and the gold coin are genie. When the main character's region encounters a gold coin, the gold coin will be "eaten" and the Two elves will overlap, we need to make a series of judgments: "collision detection" to see if the protagonist has eaten gold coins. Then we can use a physical rigid body. The protagonist and the gold coin are displayed with the genie, but they both carry a rigid body. When they come together, we can use them.Physical Collision Detection.


In the physical world, all objects have gravity. In the future, the protagonist will carry a rigid body. To prevent the protagonist from dropping out of the screen, you need to create a ground rigid body, the main character can be placed on a ground rigid body. So you may ask: The floor is also a rigid body, so the floor will not fall? There are two different rigid bodies in Cocos: one is affected by gravity, and the other is a static rigid body. You can place it anywhere, so the floor is a static rigid body.

View the Code directly:

Playscene. h:

# Ifndef _ playscene _ H __# DEFINE _ playscene _ H __# include "cocos2d. H "class playscene: Public cocos2d: layer {public: Virtual bool Init (); static cocos2d: Scene * createscene (); create_func (playscene ); // The role of the two rows is: layer-> setphysicsworld (scene-> getphysicsworld (); cocos2d: physicsworld * m_world; void setphysicsworld (cocos2d :: physicsworld * World) {m_world = World;}; private: // initialize the physical Rigid Body void initphysicworld () ;}; # endif
In the code

cocos2d::PhysicsWorld* m_world;void setPhysicsWorld(cocos2d::PhysicsWorld* world){ m_world = world;};

These two sentences are not used in the subsequent process, but they are necessary.

Playscene. cpp:

# Include "playscene. H "# include" simpleaudioengine. H "# define ground_hight 59using_ns_cc; using namespace cocosdenshion; SCENE * playscene: createscene () {// create a physical sceneauto scene = scene: createwithphysics (); // enable debugging to depict the physical world so that it can see scene-> getphysicsworld ()-> setdebugdrawmask (physicsworld: debugdraw_all); // layer pay attention to use auto here, otherwise, setphysicsworld cannot use auto layer = playscene: Create (); layer-> setphysicsworld (scene-> Getphysicsworld (); scene-> addchild (layer); Return scene;} bool playscene: Init () {If (! Layer: Init () {return false;} simpleaudioengine: getinstance ()-> playbackgroundmusic ("background#", true); initphysicworld (); // return true;} void playscene: initphysicworld () {auto visiblesize = Director: getinstance ()-> getvisiblesize (); Auto origin = Director :: getinstance ()-> getvisibleorigin (); // create a rigid body auto boundbody = physicsbody: createedgesegment (origin, CCP (visiblesize. width, 0), physicsbody_material_default, 1); // associate a node with the physical Rigid Body auto boundnode = node: Create (); boundnode-> setphysicsbody (boundbody ); boundnode-> setposition (0, ground_hight); this-> addchild (boundnode );}
In the createscene function, create a function similar to a common scenario:

auto scene = Scene::create();
We need to create special physical scenarios to do this:

// Create a physical sceneauto scene = scene: createwithphysics ();
Then we need to draw the rigid bodies in the physical world for us to see. We need to enable debugging.

// Enable debugging to depict the physical world so that it can see scene-> getphysicsworld ()-> setdebugdrawmask (physicsworld: debugdraw_all );
Create the play layer and bind the physical world to the layer.

In the initphysicworld function, we initialize the physical world and only create a ground Rigid Body for the physical world. As the rigid body requires an object to be displayed, we use a node to bind the rigid body. There is also a macro definition ground_hight 59, because many things are placed at the height of the ground. I checked the map of the land in the resource, and its height pixel is 59, so ....

Then we can start the preliminary display of the game interface.

In the callback function start of mainscene. cpp,

void MainScene::start(){CCLOG("game is start !");Scene* playScene = TransitionFade::create(1,PlayScene::createScene());Director::getInstance()->replaceScene(playScene);}
Then we can switch to our game scenario through the start button.

Here transitionfade: Create is just a way to switch between scenarios. You can define it, and there are many other ways, such as making your game scene appear like a blind shutter.

When a game is running, the following scenario is displayed:


The red thin line in the figure is our static ground rigid body. Thanks to the enabling mode, we can see all

OK. Now let our hero enter the game scenario and run it.


Here is an entry-level tutorial on 3.0 physical engines:

New physical Engine

Personal ignorance. Thank you for your correction and discussion.

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.