Cocos2d-x 3.0 game instance learning notes the sixth step -- physical collision detection (2) -- the protagonist eat gold coins, cocos2d-x the Parkour

Source: Internet
Author: User

Cocos2d-x 3.0 game instance learning notes the sixth step -- physical collision detection (2) -- the protagonist eat gold coins, cocos2d-x the Parkour

(Here is the reference: Xiaofeng residual month predecessors blog, he is the teiran network of the parkour tutorial, with the cocos2d-x 2.X version of rewriting, I am learning cocos2d-X3.0 so I will use cocos2d-X 3.0 rewrite, take notes

Well, based on a brief understanding of the physical collision detection in the previous step, we also need to complete the last step of this game instance note-let the protagonist eat gold coins. if it hits the rock, it will be GameOver.

After the game is over, all source code and resources will be released, or I will package and share the entire project so that you can run it directly. After all, it was something of others at the beginning, supporting open source

Here, we need to first add the collision detection conditions for PlayScene:

<Span style = "font-size: 18px;"> </span> <span style = "font-size: 14px;"> // collision detection cocos2d :: eventDispatcher * dispatcher; void onEnter (); void onExit (); bool onContactBegin (cocos2d: PhysicsContact & contact); </span>
Then implement:

Void PlayScene: onEnter () {Layer: onEnter (); auto contactListenner = EventListenerPhysicsContact: create (); // CC_CALLBACK indicates the number of callback function parameters contactListenner-> onContactBegin = CC_CALLBACK_1 (PlayScene: onContactBegin, this); dispatcher = Director: getInstance ()-> getEventDispatcher (); dispatcher-> cancel (contactListenner, this);} void PlayScene: onExit () {Layer: onExit (); // cancel the event dispatch mechanism dispatcher-> removeAllEventListeners ();} bool PlayScene: onContactBegin (PhysicsContact & contact) {auto body_1 = (Sprite *) contact. getShapeA ()-> getBody ()-> getNode (); auto body_2 = (Sprite *) contact. getShapeB ()-> getBody ()-> getNode (); CCLOG ("ssssss"); // if (body_1-> getTag () = 3 & body_2-> getTag () = 1) | (body_1-> getTag () = 1 & body_2-> getTag () = 3 )) {Director: getInstance ()-> replaceScene (GameOver: scene ();} if (body_1-> getTag () = 2) {body_1-> setVisible (false);} if (body_2-> getTag () = 2) {body_2-> setVisible (false);} return false ;}
So here we only need to do this step, and cocos will automatically detect it, so here we will also be in the Runner init Function

This-> setTag (1); here we can know that the Tag of the protagonist is 1, the Tag of the gold coin is 2, and the rock is 3;

Then add the code in the initBody so that it can be detected by collision:

Void Runner: initBody () {// set bodySize for different rigid bodies according to different States; if (m_state = crouch) {bodySize = crouchSize;} else {bodySize = run_jumpSize ;} // create the runner's rigid body auto runerBody = PhysicsBody: createBox (bodySize, PHYSICSBODY_MATERIAL_DEFAULT); // set the runerBody for Collision Detection-> setCategoryBitmask (1 ); runerBody-> setCollisionBitmask (1); runerBody-> setContactTestBitmask (1); // bind the rigid body this-> setPhysicsBody (runerBody );}
Okay, we switched the GameOver scene when the rock collided with the main character. The scene is actually similar to that of MainScene. The Code is as follows:

#ifndef __GameOver__H__#define __GameOver__H__#include "cocos2d.h"class GameOver : cocos2d::Layer{public:virtual bool init();static cocos2d::Scene* scene();CREATE_FUNC(GameOver);void createBG();private:void createButton(cocos2d::Point centerPoint);void reStart();};/**/#endif-------------------------.cpp--------------------------#include "GameOver.h"#include "PlayScene.h"USING_NS_CC;Scene* GameOver::scene(){Scene* scene = Scene::create();Layer* layer = GameOver::create();scene->addChild(layer);return scene;}bool GameOver::init(){if(!Layer::init()){return false;}//create backgroundcreateBG();return true;}void GameOver::createBG(){auto winSize = Director::getInstance()->getWinSize();auto centerPoint = ccp(winSize.width / 2, winSize.height / 2);//BGSprite* spriteBg = Sprite::create("MainBG.png");spriteBg->setPosition(centerPoint);this->addChild(spriteBg);//create buttoncreateButton(centerPoint);}void GameOver::createButton(cocos2d::Point centerPoint){//Start muttonMenuItemImage* menuItem = MenuItemImage::create("restart_n.png","restart_s.png",CC_CALLBACK_0(GameOver::reStart,this));Menu* menu = Menu::create(menuItem,NULL);menu->setPosition(centerPoint);this->addChild(menu);}void GameOver::reStart(){CCLOG("game is restart !");Scene* playScene = TransitionFade::create(1,PlayScene::createScene());Director::getInstance()->replaceScene(playScene);}

OK, now, and all. But... There is still a small problem. I will stay in the next article, that is, if the game ends completely according to my code here and switches to the end scene. However, it seems useless to start again in the end scene .... Wait for me to solve the problem first, or please help me here !! Thank you. The solution, complete code, and packaged project are provided in the next article !!!

Personal ignorance. Thank you for your correction and discussion !!!




Using cocos2d-x developed a small game, _ bullet and bullet collision detection, get sprite texture size occasionally reported error, as shown below

This is the access to invalid memory... If you're sure it was a crash in your lap .. Check whether the memory pointed to by your tex is valid ....


Related Article

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.