精灵的三种加载方法

精灵的加载方法有三种方法:
即直接加载图片、加载纹理、加载框架三种大的方法,下面将逐个分析


    CCSize winsize =  CCDirector::sharedDirector()->getWinSize();
    CCSprite* sprite = CCSprite::create("teemo2.png");
    sprite->setPosition(ccp(winsize.width / 2, winsize.height / 2));
    addChild(sprite);

    CCSprite * plant = CCSprite::create("sprite/plant.png",CCRectMake(0,0,80,80));
    plant->setAnchorPoint(ccp(0.5,0.5));
    plant->setPosition(ccp(winsize.width / 2-110, winsize.height / 2-100));
    addChild(plant);

直接在创建精灵的时候通过加载图片。场景一般用在较少精灵的地方。

    CCImage* image = new CCImage();
    image->initWithImageFile("sprite/bullet.png");

    CCTexture2D * cctexture = new CCTexture2D(); 
    cctexture->autorelease();
    cctexture->initWithImage(image);//纹理

    CCSprite* sprite2 = CCSprite::create();
    sprite2->initWithTexture(cctexture);

    sprite2->setPosition(ccp(winsize.width / 2 - 60, winsize.height / 2 - 100));
    sprite2->setAnchorPoint(ccp(0, 0));
    addChild(sprite2);

代码如上,但是为什么要代码托管呢?什么时候需要代码托管呢?

    CCTextureCache * textCache = CCTextureCache::sharedTextureCache();

    CCTexture2D* cctexture2d =textCache->addImage("sprite/p_1_01.png");
    //CCTexture2D* cctexture2d = new CCTexture2D();
    cctexture2d->autorelease();
    CCSprite* sprite4 = CCSprite::createWithTexture(cctexture2d);
    sprite4->setPosition(ccp(winsize.width / 2 - 190, winsize.height / 2 - 100));
    addChild(sprite4);

addImage()方法在加入纹理的时候会返回CCTexture2D引用

    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("sprite/zombie.plist");
    CCSprite * sprite3 = CCSprite::createWithSpriteFrameName("z_1_attack_06.png");
    sprite3->setAnchorPoint(ccp(0.5, 0.5));
    sprite3->setPosition(ccp(winsize.width / 2 + 30, winsize.height / 2 - 100));
    addChild(sprite3);

    CCJumpBy * by = CCJumpBy::create(2, ccp(0, 0), 40, 3);//增加跳的动作
    sprite3->runAction(CCRepeatForever::create(by));

这是第三种方法,具体各种加载的优点缺点后面再做分析。精灵的三种加载方法_第1张图片精灵的三种加载方法_第2张图片

你可能感兴趣的:(图片)