錯誤筆記
1在添加新的層在原背景上面時候(模態窗口),在cocosbuilder中 應該新建爲node格式 而不是layer 否則 會彈出
CCNodeLoaderMap::iterator ccNodeLoadersIterator =this->mCCNodeLoaders.find(pClassName);
assert(ccNodeLoadersIterator !=this->mCCNodeLoaders.end());
return ccNodeLoadersIterator->second;
2期待下一個問題出現,哈哈
a,問題出現了,要彈出一個新層,此層有大小以及位置。在cocosbuilder中可以設置node的錨點位置,例如設置到中間位置的錨點爲anchor point -0.5,-0.5.但是經過操作發現這樣設置的話 在代碼中設置層的位置變得難於理解,例如 偏左上角node->setPosition(ccp(-50,
-30));
所以默認0,0 最好,將精靈都放在這個node中 在代碼中設置 node位置(想要將node放在哪就放哪)。
b,對node縮放 同理 於sprite :pClickItem->setScaleX(0.1),pClickItem->setScaleY(0.1); -》 node->setScaleX(0.5),node->setScaleY(0.5);
3移除所在父類窗口(層/node)
this->removeFromParentAndCleanup(true);
void 類名::onEnter(){
CCLog("CCAlert::onEnter()");
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, -128, true);
CCLayer::onEnter();
}
void 類名::onExit(){
CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
CCLayer::onExit();
}
boolXXX::ccTouchBegan(CCTouch *pTouch,CCEvent *pEvent){
return true;
}
void XXX::ccTouchEnded(CCTouch *pTouch,CCEvent *pEvent){
}
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, -100, true);
此時效果->左右均可觸摸5記錄存儲數據
CCUserDefault::sharedUserDefault()->setStringForKey("name","nishihou");
CCUserDefault::sharedUserDefault()->flush();//這裏一定要提交寫入哦,否則不會記錄到xml中,下次啓動遊戲就獲取不到value了。
//這裏隨便定義一個string爲了驗證我們的存儲
std::string str="select";
//取出我們剛存儲的nishihou,然後賦值給str驗證下;
str= CCUserDefault::sharedUserDefault()->getStringForKey("name");
CCLog("打印str=:%s",str.c_str());
// CCLog("打印str=:%s",CCUserDefault::sharedUserDefault()->getStringForKey("key").c_str());
文件存儲的地址可以使用CCUserDefault::sharedUserDefault()->getXMLFilePath().c_str()獲得。
刪除數據 ,設爲空即可
CCUserDefault::sharedUserDefault()->setStringForKey("key","");
CCUserDefault::sharedUserDefault()->flush();
6報錯
Apple Mach-O Linker (id) Error : linker command failed with exit code 1 (use -v to see invocation)
- void SpriteSelectScene::xuannishihou(CCObject *sender)
- {
- CCLog("點擊泥石猴菜單");
- userDefault();
- std::string a = CCUserDefault::sharedUserDefault()->getStringForKey("key1");
- //如果 取到值 則 true 不添加左側窗口 不執行菜單方法
- if (a=="nishihou")
- {
- CCLog("有數據,此方法不執行");
- CCLog("打印str=:%s",CCUserDefault::sharedUserDefault()->getStringForKey("key1").c_str());
- return;
- }
- //如果取到值爲空(無數據)則添加寵物到左側窗口
- else
- { //加載左側窗口
- CCNode * node = SceneManager::getLoadCcbiNode(ccbi_fileName_spritejia, ccbi_nodeName_spritejia, SpriteJiaLoader::loader());
- node->setPosition(ccp(80, 200));
- addChild(node,1);
- CCLog("沒有數據,執行添加");
- }
- }
- 代碼 附下
- void SpriteSelectScene::onNodeLoaded(CCNode * pNode, cocos2d::extension::CCNodeLoader * pNodeLoader)
- {
- CCLog("onNodeLoadedonNodeLoadedonNodeLoaded");
- CCSize size = CCDirector::sharedDirector()->getWinSize();
- //進入遊戲判斷有無用戶選擇寵物的記錄,若有默認執行上次的選擇
- std::string a = userDefault->getStringForKey(key_chongwu);
- if(a=="selectnishihou")
- {
- //有數據直接加載界面
- CCNode * node = SceneManager::getLoadCcbiNode(ccbi_fileName_spritejia, ccbi_nodeName_spritejia, SpriteJiaLoader::loader());
- node->setPosition(ccp(80, 200));
- addChild(node,1);
- }
- else {
- CCLog("無反映");
- return;
- }
- }
- /**
- 選擇寵物/添加寵物到左側
- @param sender <#sender description#>
- */
- void SpriteSelectScene::xuannishihou(CCObject *sender)
- {
- std::string b = userDefault->getStringForKey(key_chongwu);
- //如果 取到值 則 true 不添加左側窗口 不執行菜單方法
- if (b=="selectnishihou")
- {
- CCLOG("有數據不執行");
- return;
- }
- //如果取到值爲空(無數據)則添加寵物到左側窗口
- else
- { CCNode * node = SceneManager::getLoadCcbiNode(ccbi_fileName_spritejia, ccbi_nodeName_spritejia, SpriteJiaLoader::loader());
- node->setPosition(ccp(80, 200));
- addChild(node,1);
- CCLOG("無數據,執行添加");
- userDefault->setStringForKey(key_chongwu, "selectnishihou");
- userDefault->flush();
- CCLog("打印str=:%s");
- }
- }
This generally means that another instance of this process was already running or is hung in the debugger.(lldb)
void場景::update(float dt)
{
for(int i =0; i <數組名->count(); i++)
{
場景 * 精靈2 = (場景*)數組名->objectAtIndex(i);
if((精靈1->boundingBox()).intersectsRect(精靈2->boundingBox()))
{ int diantag = dian->getTag();
int yezitag = diantag+100;
if (getChildByTag(yezitag)!=NULL)
{
CCSprite *yezi = (CCSprite *)getChildByTag(yezitag);//強轉
this->removeChild(yezi,true);
}else {
CCLog("%d========%d",diantag,yezitag);
}
_dians->removeObject(dian);
this->removeChild(dian,true);
}
else
{
// CCLog("沒碰撞");
}
}
}
onNodeLoaded裏 例如
this->schedule(schedule_selector(YeZiScene::update));與
_yezis = CCArray::create();
for (int i=1;i<=9;i++)
{
int tag =100+i;
if (getChildByTag(tag)!=NULL)
{
CCSprite *sprite = (CCSprite *)getChildByTag(tag);//強轉
_yezis->addObject(sprite);
}
}等等 順序爲 先加載遊戲界面 然後加載數組 之後循環
int diantag = dian->getTag();
int yezitag = diantag+100;
if (getChildByTag(yezitag)!=NULL)
{
CCSprite *yezi = (CCSprite *)getChildByTag(yezitag);//強轉
this->removeChild(yezi,true);
}else {
CCLog("%d========%d",diantag,yezitag);
}
CCPoint location=pTouch->getLocation();
//設置執行動作需要移動到的位置,參數1代表時間 2位置
float juli=ccpDistance(qiu->getPosition(),ccp(location.x,location.y));
float sudu=100.0;
CCActionInterval* actionMove =CCMoveTo::create(juli*1.0/sudu,ccp(location.x,location.y));
CCFiniteTimeAction* actionMoveDone =CCCallFuncN::create(this,
callfuncN_selector(YeZiScene::spriteMoveFinished));
qiu->runAction(CCSequence::create(actionMove, actionMoveDone,NULL) );
}
int diantag = dian->getTag();
int yezitag = diantag+100;
float juli=ccpDistance(qiu->getPosition(),ccp(location.x,location.y));
。。。反正老多了要這樣做 要養成習慣 仔細看代碼裏面 大多數都是private:
CCSprite* ciqiu;
在.cpp中初始化處寫:
CCSprite*ciqiu = (CCSprite*)getChildByTag(196);本想着 以後在用到精靈時候不需要寫了,可是結果是NO
終於明白:羣裏說的,“你再次聲明並定義了 雖然名字是一樣的 但是是2個完全不同的對象啊,一個是類成員 一個是臨時,臨時的那個隨着函數的消失就消失了,你在.h裏面聲明 是代表你這個類的類成員。 你在.cpp裏面寫的那個東西是 一個臨時的變量”
解決方法:初始化中
把你前面的 CCsprite * 全部去掉。。