Introduction to Cocos2d-x (1) Making a dynamic Sprite

Source: Internet
Author: User
Tags addchild bool

Principle:

Cocos2d has a director to control the whole game process, the director will add the scene to the screen, the scene has a variety of actors.

First by showing a picture to see the cocos2d game process:

AppDelegate.cpp

BOOL Appdelegate::applicationdidfinishlaunching ()  
{  
    //Initialize director  
    Ccdirector *pdirector = ccdirector:: Shareddirector ();  
    Pdirector->setopenglview (Cceglview::sharedopenglview ());  
      
    Open fps  
    pdirector->setdisplaystats (true);  
      
    Set fps the ' default value is 1.0/60 if/don ' t call this  
    pdirector->setanimationinterval (1.0/60);  
      
    Create a scene  
    ccscene *pscene = Gamescence::scene ();  
      
    Run this scene  
    Pdirector->runwithscene (pscene);  
    return true;  
}

The above code adds a scene gamescence, and here's a look at the concrete implementation:

GameScence.h

#include "cocos2d.h"  
#include "box2d/box2d.h"  
      
class Gamescence:public Cocos2d::cclayer  
{public  
:  
    BOOL Init ();  
    You must override the scene ()  
    static cocos2d::ccscene* scene ();  
    Equivalent to the CREATE function, is to rewrite the CREATE function in the Cclayer  
    Create_func (gamescence);  

GameScence.cpp

#include "GameScene.h"  
      
using namespace cocos2d;  
      
ccscene* Gamescence::scene ()  
{  
    ccscene * scene = NULL;  
    Do
    {  
        scene=ccscene::create ();  
      
        gamescence* gamescene=gamescence::create ();  
        Scene->addchild (Gamescene);  
    } while (0);  
      
    return scene;  
};  
      
BOOL Gamescence::init ()  
{  
    bool BRet = false;  
    Do
    {  
        //Create a sprite from the picture  
        ccsprite* psprite = ccsprite::create ("Bg.png");  
        Gets the screen size  
        ccsize size = Ccdirector::shareddirector ()->getwinsize ();  
        Sets the position of the sprite in the scene, coordinates from the lower left corner of the 0,0  
        psprite->setposition (CCP (SIZE.WIDTH/2, SIZE.HEIGHT/2));  
        Add Sprite to Scene  
        This->addchild (psprite, 0);  
    } while (0);  
      
    Bret=true;  
      
    return bRet;  
};

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.