cocos2d 创建精灵图

// 在init这个函数当中做一些初始化的事情
bool HelloWorld::init()
{
    //////////////////////////////
    // 先构造父级对象
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    CCSize size=CCDirector::sharedDirector()->getWinSize();

    //添加一个雪碧图
    CCSprite* pSprite = CCSprite::create("love.png");
    pSprite->setPosition(ccp(size.width*0.2, size.height*0.7));
    this->addChild(pSprite);
    
    //ccrect 和ccrectmake都可以
    CCSprite* pSprite1 = CCSprite::create("love.png",CCRect(0,0, 30, 30));
    //CCSprite* pSprite1 = CCSprite::create("love.png",CCRectMake(0,0, 30, 30));
    pSprite1->setPosition(ccp(size.width*0.4, size.height*0.7));
    this->addChild(pSprite1);

    //sharedTextureCache()后面的括号很重要
    CCTexture2D *pTexture=CCTextureCache::sharedTextureCache()->addImage("love.png"); 
    CCSprite* pSprite2 = CCSprite::createWithTexture(pTexture);
    pSprite2->setPosition(ccp(size.width*0.6, size.height*0.7));
    this->addChild(pSprite2);

    //sharedTextureCache()后面的括号很重要
    CCSprite* pSprite3 = CCSprite::createWithTexture(pTexture,CCRect(0,0, 30, 30));
    pSprite3->setPosition(ccp(size.width*0.8, size.height*0.7));
    this->addChild(pSprite3);

    CCSpriteFrame* spriteFrame=CCSpriteFrame::create("love.png",CCRect(0,0,30,30));
    CCSprite* pSprite4 = CCSprite::createWithSpriteFrame(spriteFrame);
    pSprite4->setPosition(ccp(size.width*0.3, size.height*0.3));
    this->addChild(pSprite4);

    //这个list文件是以后我门将会使用到的一些文件打包工具帮助我们生成的文件。也是一个xml文件
    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("img.list");
    CCSprite* pSprite5 = CCSprite::createWithSpriteFrameName("icon.png");
    pSprite5->setPosition(ccp(size.width*0.5, size.height*0.3));
    this->addChild(pSprite5);
    
    return true;
}

 

posted @ 2014-03-19 23:14  yufenghou  阅读(369)  评论(0编辑  收藏  举报