Cocos2d_x_02 _ use the built-in physical Engine

Source: Internet
Author: User

Cocos2d_x_02 _ use the built-in physical Engine
Finally:
1. Create a project to enter the bin directory under the tools of the cocos2d-x:/Users/beyond/Symbol TZ? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> keys =" http://www.2cto.com/uploadfile/Collfiles/20141004/2014100408574179.png "alt =" \ ">
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 );}
































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.