Cocos2d_x_02 _ use the built-in physical engine and cocos2d_x_02 Engine
Finally:
1. Create a project to enter the bin directory under the cocos2d-x tools:/Users/beyond/Desktop/cocos2d-x/cocos2d-x-3.3beta0/tools/cocos2d-console/bin directory
Open the terminal and switch to the bin directory
Use the cocos command to create a project in the following format:
Cocos new project name-p package name-l language-d target folder,
- For example:
Cocos new 01_cocos2d-x-p com. beyond-l cpp-d/Users/beyond/Desktop/cocos2d-x/cocos2d-x Learning
Go to the created project directory and open the project
Ii. BallScene. h, inherited from Layer
//// BallScene. h // 01_cocos2d-x // Created by beyond on 14-10-3. /// # ifndef ___ character cocos2d_x _ BallScene __# define ___ character cocos2d_x _ BallScene __# include "cocos2d. h "USING_NS_CC; // note that the inheritance here is Layerclass BallScene: public cocos2d: Layer {private: // screen Size winSize; public: // c ++ does not have the id type, so the pointer to the Instance Object of the returned class is static cocos2d: Scene * createScene (); // The following are differences: the 'init 'method of the cocos2d-x returns bool // while the cocos2d-iphone returns the 'id' type virtual bool init (); // The macro automatically implements "static create () method "CREATE_FUNC (BallScene); // rewrite a method virtual void onEnter (); // custom method to create an application boundary void addBoundry (); // custom method, add a ball void addBall (float x, float y); // a custom method to add a ball void addBall (Vec2 point );}; # endif/* defined (___ define cocos2d_x _ BallScene __)*/
III,
BallScene. cpp class implementation
//// BallScene. cpp // 01_cocos2d-x // Created by beyond on 14-10-3. # include "BallScene. h "USING_NS_CC; Scene * BallScene: createScene () {// 'Scene 'is automatically released // key. Create a scene auto scene with a physical engine world = scene :: createWithPhysics (); // focus, draw debugging information scene-> getPhysicsWorld ()-> setDebugDrawMask (PhysicsWorld: DEBUGDRAW_ALL); // 'player' Automatically releases auto layer = BallScene :: create (); // Add the layer to the scene-> addChild (layer); // return to fill the image Return scene;} // In the "init" method, instantiate the bool BallScene object: init () {// 1. call the init of the parent class. if (! Layer: init () return false; // screen size winSize = Director: getInstance ()-> getVisibleSize (); // The cocos2d-3.x can quickly create a physical body // setPhysicsBody (<# cocos2d: PhysicsBody * body #> return true;} # pragma mark-override the parent class method void BallScene :: onEnter () {// 1. First Run onEnter Layer: onEnter (); // 2. Call the custom method to create an application boundary addBoundry (); // 3. Call the custom method and add a ball addBall (winSize. width/2, winSize. height/2); // 4. instantiate a touch listener object auto listener = EventListenerTouchOneByOne: create (); // bind a closure function when the touch starts; // [] indicates the external object to be passed in. this // () indicates the parameter listener-> onTouchBegan = [this] (Touch * t, Event * e) {// click it to create a Ball // getLocation and return an OpenGL coordinate. this-> addBall (t-> getLocation (); return false ;}; // 5. Get the event distributor and add an event listener to this. That is, the listener listens to this object [entire Layer] Director: getInstance ()-> getEventDispatcher () -> addEventListenerWithSceneGraphPriority (listener, this);} # pragma mark-custom method // custom method to create an application boundary void BallScene: addBoundry () {// specify the size and create a physical body auto b2Body = PhysicsBody: createEdgeBox (winSize, PHYSICSBODY_MATERIAL_DEFAULT, 5); // create an auto boundry = Node :: create (); boundry-> setPosition (winSize. width/2, winSize. height/2); // set the physical body boundry for the genie-> setPhysicsBody (b2Body); // Add the genie to the current Layer this-> addChild (boundry );} // Add a custom method named ballvoid BallScene: addBall (float x, float y) {auto ball = Sprite: create ("ball.png "); ball-> setPosition (x, y); // specify the size and create a physical body auto b2Body = PhysicsBody: createBox (ball-> getContentSize ()); // set the physical body ball for the genie-> setPhysicsBody (b2Body); // Add the sprite to the current Layer this-> addChild (ball);} // custom method, add a ballvoid BallScene: addBall (Vec2 point) {addBall (point. x, point. y );}
Chipmunk physical engine an example of an object following the mouse movement, preferably under the cocos2d-x-js
I have never studied this physical engine, but I guess it may be that you have a problem calling a function when writing it. According to what you said, It is visually tested whether you have called the function by clicking the left mouse button, didn't I call the left-mouse lift function? If you only call the mouse-pressed function, the physical operation is triggered when you click an object. I'm not sure. I have never used it.
How to Use Xcode tools to add Box2D physical engine to the cocos2d-x framework?
Can't find file error? I have been entangled in this error for a long time. All the settings in the Search Path are set. m files are all changed. mm, that is, the error is always reported. If you have searched for N posts, you can use this setting, but I have not successfully built it. Thank you very much for your help.