一些零碎的代码,便于以后查找
1.添加动画
1 //添加动画帧 2 CCAnimation* animation = CCAnimation::create(); 3 for(int i = 1; i< 6;i++) 4 { 5 char imgsName[100] = {0}; 6 sprinft(imgsName,”img_%d.png”,i); 7 animation->addSpriteFrameWithFileName(imgsName); 8 } 9 animation->setDelayPerUnit(1.0f); 10 animation->setRestoreOriginalFrame(true); 11 animation->setLoop(-1); 12 //使用动画帧创建动画 13 CCAnimate* animate = CCAnimate::create(animation); 14 this->runAction(animate);
2.替换CCSprite的纹理
setTexture(CCTexture2D)
3.Touch事件处理
bool SoundSprite::ccTouchBegan(cocos2d::CCTouch *touch, cocos2d::CCEvent *event) { CCPoint touchPoint = convertToNodeSpace(touch->getLocation()); CCRect rect = CCRect(getPositionX() - getContentSize().width * getAnchorPoint().x, getPositionY() - getContentSize().height * getAnchorPoint().y, getContentSize().width, getContentSize().height); rect.origin = CCPointZero; bool isTouchedIn = rect.containsPoint(touchPoint); if (isTouchedIn) { CCLog("SoundSprite::ccTouchBegan"); m_PlaySound = !m_PlaySound; this->initWithFile(m_PlaySound?I_FARM_SOUND_ON:I_FARM_SOUND_OFF); return true; } return false; }