Setting up the physics world
In order to enable physics in our game, we need to add the following lines to our HelloWorldScene.h
header file:
cocos2d::Sprite* _sprBomb; void initPhysics(); bool onCollision(cocos2d::PhysicsContact& contact); void setPhysicsBody(cocos2d::Sprite* sprite);
Here we have created an instance variable for the _sprBomb
variable, so that it can be accessed from within all the instance methods. In this particular case, we would like to have access to the bomb instance in the onCollision
method that will be called each time the collisions between the physics bodies are detected, so that we can make the bomb disappear by simply setting its visible attribute to false.
Now let us move to our HelloWorld.cpp
implementation file, and make a few changes in order to set up our physics world.
First, let us modify our createScene
method, so that it will now look like this:
Scene* HelloWorld::createScene() { auto scene = Scene::createWithPhysics(); scene->getPhysicsWorld...