//CCSprite by 雲瑜/流光月舞
//~ minimap
CCTexture2D *tex;
tex->generateMipmap();
ccTexParams tex_params = { GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE };
tex->setTexParameters(&tex_params);
CCSpriteFrameCache *sfcache = CCSpriteFrameCache::sharedSpriteFrameCache();
CCSpriteFrame *frame = sfcache->spriteFrameByName("wheel.png");
CCSprite *wheel_l = CCSprite::spriteWithSpriteFrame(frame);
//设置绘图的足以点 (0, 1) 是以左上角上中心,(1, 1)是右上角
sprite->setAnchorPoint(ccp(0, 1));
//打开抗锯齿 可以使地图没有那个间隙
sprite->getTexture()->setAntiAliasTexParameters();
sprite->getTexture()->setAliasTexParameters(); //不抗锯齿
//加载图片
CCSprite *sprite = CCSprite::create("yy.png", CCRectMake(112*0, 64*0, 112, 64));
CCSprite *sprite = CCSprite::create("yy.png");
//停止所有动画
sprite->stopAllActions();
//启动动画
sprite->runAction(animate);
//播放一次
sprite->runAction(CCRepeat::create(_animate_go, 1));
//重复播放
sprite->runAction(CCRepeatForever::create(_animate_go));
//设置缩放
sprite->setScale(0.8);
CCAnimate *animate;
aimate->stop(); //停止动画
//获取图片的大小
CCSize s_sprite = m_sprite->getContentSize();
CCLog("%f %f", s_sprite.width, s_sprite.height);
//清除精灵
sprite->removeFromParentAndCleanup(true);
layer->removeChild(spirte, true);
//透明 127 半透明 (0~255); 完全透明 0
sprite->setOpacity(127);
//隐藏精灵
sprite->setVisible(false); //2.0
//水平翻转图像
sprite->setFlipX(true); //
sprite->isFlipX(); //如果水平翻转则返回 true
//函数调用 CCCallFunc
void YY::Hello()
{
CCLog("Hello, this your world!\n");
}
这里影响获取的物品的位置
### 使用 plist 的动画
void YY::Action()
{
sprite->stopAllActions();
CCSpriteFrameCache *sfcache = CCSpriteFrameCache::sharedSpriteFrameCache();
sfcache->addSpriteFramesWithFile("yy/yy.plist");
CCArray *anim_frames = new CCArray(); //2.0
char tmp[50];
for (int j = 0; j < 6; j++) {
sprintf(tmp, "yy_%d.png", j);
CCSpriteFrame *frame = sfcache->spriteFrameByName(tmp);
anim_frames->addObject(frame);
}
CCAnimation *animation = CCAnimation::animationWithSpriteFrames(anim_frames, 0.1f); //2.0
anim_frames->release();
CCAnimate *animate = CCAnimate::actionWithAnimation(animation); //2.0
CCFiniteTimeAction *action = CCCallFunc::create((CCObject*)this, callfunc_selector(YY::Hello));
sprite->runAction(CCSequence::create(animate, action, NULL));
}
//不使用 plist 的
CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage("yy/yy.png");
CCSpriteFrame *frame0 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(112*0, 64*0, 112, 64));
CCSpriteFrame *frame1 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(112*1, 64*0, 112, 64));
CCArray *anim_frames_go = new CCArray();
anim_frames_go->addObject(frame0);
anim_frames_go->addObject(frame1);
CCAnimation *animation_go = CCAnimation::animationWithSpriteFrames(anim_frames_go, 0.2f);
anim_frames_go->release();
CCAnimate *animate_go = CCAnimate::actionWithAnimation(animation_go);
msprite->setDisplayFrame(frame0);
sprite->runAction(CCRepeatForever::create(animate_go));
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int i;
CCArray *array = CCArray::create();
char buf[64];
for (i = 0; i < 4; i++) {
sprintf(buf, "role/0001/1_%d.png", i + 1);
CCSpriteFrame *frame = CCSpriteFrame::create(buf, CCRectMake(0, 0, 32, 32));
array->addObject(frame);
}
CCAnimation *animation = CCAnimation::createWithSpriteFrames(array, 0.2f);
CCAnimate *animate = CCAnimate::create(animation);
CCSprite *sprite = CCSprite::create("role/0001/1_1.png");
sprite->setPosition(ccp(100, 100));
this->addChild(sprite);
sprite->runAction(CCRepeatForever::create(animate));
看上去代码量着不多,但是使用 plist 的话方便管理与使用。
### 内存管理
CCSprite *sprite = new CCSprite;
sprite->autorelease(); //new 过的的需要 autorelease
//释放到目前为止所有加载的图片
CCTextureCache::sharedTextureCache()->removeAllTextures();
//将引用计数为1的图片释放掉
CCTextureCache::sharedTextureCache()->removeUnusedTextures();
//单独释放某个图片
CCTextureCache::sharedTextureCache()->removeTexture();
CCSpriteFrameCache *sprite_cache;
sprite_cache->removeSpriteFrames(); //Purges the dictionary of loaded sprite frames.
CCSpriteFrameCache::sharedSpriteFrameCache()->removeUnusedSpriteFrames(); //Removes unused sprite frames.
CCSpriteFrameCache::sharedSpriteFrameCache();
CCSpriteFrameCache::purgeSharedSpriteFrameCache();
//有时强制释放全部资源时,会使某个正在执行的动画失去引用而弹出异常,可以用它来解决
CCActionManager::sharedManager()->removeAllActions(); //1.0
//2.0
CCActionManager *action_manager;
action_manager->removeAllActions();
### CCArray by yunyu
////
int total_add = 10;
CCArray *sprites = CCArray::create(total_add);
for (int i = 0; i < total_add; i++) {
CCSprite *sprite = CCSprite::create(...);
sprites->addObject(sprite);
}
this->addChild((CCNode*)(sprites->objectAtIndex(i)));
////
CCArray *temp_array = CCArray::array();
temp_array->initWithCapacity(5);
for (int i = 0; i < 5; i++) {
CCObject *ob = new CCObject();
temp_array->addObject(ob);
}
CCObject *temp_ob = temp_array->objectAtIndex(3);
CCTexture2D *texture = CCTextureCache::sharedTextureCache()->textureForKey(RS(kRMapBG))