第一个cocos2d-x程序

目的:在界面上显示3个精灵。

做法:创建一个cocos2dx项目之后重写 init( ) 方法。代码段如下:

// on "init" you need to initialize your instance

bool HelloWorld::init()

{

    //////////////////////////////

    // 1. super init first

    if ( !CCLayer::init() )

    {

        return false;

    }

 

//    CCSprite *bg = CCSprite::create("Icon.png");

//    bg->setAnchorPoint(CCPointZero);

//    bg->setPosition(CCPointZero);

//    this->addChild(bg);

//    

//    CCSprite *Player = CCSprite::create("Player.png");

//    Player->setAnchorPoint(CCPointZero);

//    Player->setPosition(ccp(50,150));

//    this->addChild(Player);

//    

//    CCSprite *Projectile = CCSprite::create("Projectile.png");

//    Projectile->setAnchorPoint(CCPointZero);

//    Projectile->setPosition(ccp(200, 150));

//    this->addChild(Projectile);

//    

//    CCSprite *Target = CCSprite::create("Target.png");

//    Target->setAnchorPoint(CCPointZero);

//    Target->setPosition(ccp(350,150));

//    this->addChild(Target);

    

    images_dictionary = CCDictionary::createWithContentsOfFile("hand02.plist");

    images_array = (CCArray *)images_dictionary->objectForKey("imagesArray");

    

    for (int i = 0; i < images_array->count(); i++) {

        CCString *string = (CCString *)images_array->objectAtIndex(i);

        CCSprite *sprite = CCSprite::create(string->getCString());

        sprite->setAnchorPoint(CCPointZero);

        sprite->setPosition(ccp(200 + 100*i, 300));

        this->addChild(sprite);

    }

    

    return true;

}


附注:注释部分是直接添加3个精灵到界面上,,没有注释部分是从 hand02.plist 中读取3个精灵然后显示到界面上。

hand02.plist文件结构如下:


运行结果如下:

 

你可能感兴趣的:(cocos2d-x)