8,303
社区成员
发帖
与我相关
我的任务
分享
// 实现滑屏的关键代码
CCTableViewCell *CColorBox::tableCellAtIndex(CCTableView *table, unsigned int idx)
{
// 格式化相应的图片名称
CCString *pString = CCString::createWithFormat("%d.png", idx + 1);
CCTableViewCell *pCell = table->dequeueCell();// 返回空闲的子项
if (!pCell)
{
pCell = new CCTableViewCell();
pCell->autorelease();
CCSprite *penSprite = CCSprite::createWithSpriteFrameName(pString->getCString());
penSprite->setAnchorPoint(CCPointZero);
penSprite->setPosition(CCPointZero);
penSprite->setTag(TABLEVIEW_ICON_TAG); // 此处要设置相同的TAG,不然就取不对该图片精灵
pCell->addChild(penSprite);
}
retrun pCell;
}
m_paintLayer = CPaintLayer::create();
this->addChild(m_paintLayer, HIGH);
就是在转场前我把这个新场景以这种方式添加到这个旧场景当中, 然后再转场到 CPaintLayer,就是这两句导致出错了。
现在知道了出错的原因,但还是没理解为什么会出错,不是转场后,旧有场景的所有资源都会被释放掉吗?为啥会因为这两句
而出错呢?
其二,另外一个问题还是没解决, 就是滑屏对象不停进行滚动时,就出现了这个情况。
滑屏对象上的子项,被不断地addChild进去(就是2.X左下角的第一项) 不断添加到一个相当大的值时,就崩溃了。
无论是把滑屏对象添加在旧场景,还是添加到转场后的新场景。
唉,困难重重。
CCSprite* CCSprite::createWithSpriteFrameName(const char *pszSpriteFrameName)
{
CCSpriteFrame *pFrame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(pszSpriteFrameName);
#if COCOS2D_DEBUG > 0
char msg[256] = {0};
sprintf(msg, "Invalid spriteFrameName: %s", pszSpriteFrameName);
CCAssert(pFrame != NULL, msg);
#endif
return createWithSpriteFrame(pFrame);
}
这地方就只能有一种情况,所以先从这里入手吧,我现在猜测是pszSpriteFrameName路径不对,可能是你不存在的图片
#include "ColorBox.h"
#include "assist/PrioritySetting.h"
#include "PublicData.h"
#define TABLEVIEW_ICON_TAG 100 // CCTableView中加入的精灵设置的TAG
CColorBox::CColorBox(void) : m_frameCache(NULL), m_pTableView(NULL)
{
}
CColorBox::~CColorBox(void)
{
// 删除精灵帧缓存
CCSpriteFrameCache::sharedSpriteFrameCache()->removeSpriteFramesFromFile("shoeDrawing/pen.plist");
}
CCScene *CColorBox::scene()
{
// 场景
CCScene *scene = CCScene::create();
// 层
CColorBox *layer = CColorBox::create();
// 层加入场景
scene->addChild(layer);
return scene;
}
bool CColorBox::init()
{
if (!CCLayer::init())
{
return false;
}
// 将图片加载到精灵帧缓存池
m_frameCache = CCSpriteFrameCache::sharedSpriteFrameCache();
m_frameCache->addSpriteFramesWithFile("shoeDrawing/pen.plist", "shoeDrawing/pen.png");
// 滑屏相关操作
m_pTableView = CCTableView::create(this, CCSizeMake(98, 450));
// 设置滚动方向
m_pTableView->setDirection(kCCScrollViewDirectionVertical);
m_pTableView->setPosition(ccp(WINSIZE_WIDTH - 130, 30));
m_pTableView->setDelegate(this);
// 决定VIEW的单元如何填充
m_pTableView->setVerticalFillOrder(kCCTableViewFillTopDown);
m_pTableView->setScale(0.8f);
//pTableView->setAnchorPoint(ccp(0.5f, 0));
this->addChild(m_pTableView, HIGH);
// 刷新VIEW
m_pTableView->reloadData();
return true;
}
void CColorBox::tableCellTouched(CCTableView* table, CCTableViewCell* cell)
{
CCLOG("cell touched at index: %i", cell->getIdx());
}
CCSize CColorBox::cellSizeForTable(CCTableView *table)
{
return CCSizeMake(97, 72);
}
CCTableViewCell *CColorBox::tableCellAtIndex(CCTableView *table, unsigned int idx)
{
// 格式化相应的图片名称
CCString *pString = CCString::createWithFormat("%d.png", idx + 1);
CCTableViewCell *pCell = table->dequeueCell();// 返回空闲的子项?
if (!pCell)
{
pCell = new CCTableViewCell();
pCell->autorelease();
CCSprite *penSprite = CCSprite::createWithSpriteFrameName(pString->getCString());
penSprite->setAnchorPoint(CCPointZero);
penSprite->setPosition(CCPointZero);
penSprite->setTag(TABLEVIEW_ICON_TAG); // 此处要设置相同的TAG,不然就取不对该图片精灵
pCell->addChild(penSprite); // 这里会出现不断添加的问题
}
else
{
// CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage(pString->getCString());
// CCSprite *penSprite = static_cast<CCSprite *>(pCell->getChildByTag(TABLEVIEW_ICON_TAG));
// penSprite->setTexture((CCSprite::createWithSpriteFrameName(pString->getCString()))->getTexture());
}
return pCell;
}
unsigned int CColorBox::numberOfCellsInTableView(CCTableView *table)
{
return 18;
}
void CColorBox::scrollViewDidScroll(CCScrollView* view)
{
CCLOG("scroll");
}
void CColorBox::scrollViewDidZoom(CCScrollView* view)
{
;
}
void CColorBox::tableCellHighlight(CCTableView* table, CCTableViewCell* cell)
{
CCSprite *temp = (CCSprite *)(cell->getChildByTag(1));
}
void CColorBox::tableCellUnhighlight(CCTableView* table, CCTableViewCell* cell)
{
}
从代码中能看出来,图片已经在init上面加载了,而且也像我 在1楼上所说的,我把这一楼贴出来的代码的代码,用在一个新建的工程上,是完全没有问题的。 所以,我不认为是图片没加载之类的问题。 唉,我也不怎么确定,已经把代码番.来番.去好多遍,还是没找到一点线索。