您的位置:首页 > 移动开发 > Cocos引擎

[Cocos2d-x]代码段记录

2013-12-07 02:24 246 查看
一些零碎的代码,便于以后查找

1.添加动画

//添加动画帧
CCAnimation* animation = CCAnimation::create();
for(int i = 1; i< 6;i++)
{
char imgsName[100] = {0};
sprinft(imgsName,”img_%d.png”,i);
animation->addSpriteFrameWithFileName(imgsName);
}
animation->setDelayPerUnit(1.0f);
animation->setRestoreOriginalFrame(true);
animation->setLoop(-1);
//使用动画帧创建动画
CCAnimate* animate = CCAnimate::create(animation);
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: