还没找到原因,用vector<Dot*>替换CCArray后没有问题,求高人解答。
在继承自CCLayer的GameLayer中,声明成员变量 :
cocos2d::CCArray* dotList;
在init()函数中创建CCArray对象,并addObject81个对象dot :
dotList = new CCArray;
dotList->initWithCapacity(DOT_ROW_AMOUNT*DOT_COL_AMOUNT);
CC_SAFE_RETAIN(dotList);
在ccTouchBegan函数内,可以取到dotList的对象,但是在connectedDots函数中取不到
DEBUG时,只看到有四个对象:

bool DotGameLayer::ccTouchBegan(cocos2d::CCTouch pTouch, cocos2d::CCEvent pEvent)
{
int selectedIndex = 5; // drowDOT_COL_AMOUNT + dcol;
Dot selectedDot = (Dot*)dotList->objectAtIndex(selectedIndex);
dotList->retain();
CCArray* dots = connectedDots(*selectedDot);
printf(“dots number:%d \n”,dots->count());
return true;
}
//四连通法取所有相连的点, dotList不为空,但取到的值为NULL
CCArray* DotGameLayer::connectedDots(Dot& aDot)
{
int visited;
CCArray* expand = new CCArray();
expand->addObject(&aDot);
int i = 0;
do
{
Dot* currentDot = (Dot*)dotList->objectAtIndex(i);
//up dot
int y_up = currentDot->row - 1;
int x_up = currentDot->column;
int index_up = y_up * DOT_COL_AMOUNT + x_up;
if (y_up >= 0 && (visited == 0) )
{
Dot* cd = (Dot*)dotList->objectAtIndex(index_up);
if (cd->type == currentDot->type)
{
expand->addObject(cd);
visited = 2;
}else
{
visited = 1;
}
}
//left dot
int y_left = currentDot->row;
int x_left = currentDot->column - 1;
int index_left = y_left*DOT_COL_AMOUNT + x_left;
if (y_left >= 0 && (visited == 0) )
{
Dot* leftDot = (Dot*)dotList->objectAtIndex(index_left);
if (currentDot->type == leftDot->type)
{
expand->addObject(leftDot);
visited = 2;
}else
{
visited = 1;
}
}
//right dot
int y_right = currentDot->row;
int x_right = currentDot->column + 1;
int index_right = y_right*DOT_COL_AMOUNT + x_right;
if (y_right < DOT_COL_AMOUNT && (visited == 0))
{
Dot* rightDot = (Dot*)dotList->objectAtIndex(index_right);
if (currentDot->type == rightDot->type)
{
expand->addObject(rightDot);
visited = 2;
}else
{
visited = 1;
}
}
//down dot
int y_down = currentDot->row + 1;
int x_down = currentDot->column;
int index_down = y_down*DOT_COL_AMOUNT + x_down;
if (y_down < DOT_ROW_AMOUNT && (visited == 0))
{
Dot* downDot = (Dot*)dotList->objectAtIndex(index_down);
if (currentDot->type == downDot->type)
{
expand->addObject(downDot);
visited = 2;
}else
{
visited = 1;
}
}
i++;
}while (i < expand->count());
return expand;
}