在init-method中我初始化一个包含4个图像的NSMutableArray。我使用这段代码来精确地放置精灵(图像):
_obstacles = [[NSMutableArray alloc] initWithCapacity:TOTAL_OBSTACLES_PER_WALL];
for (int i = 0; i < TOTAL_OBSTACLES_PER_WALL; i++)
{
obstacle = [CCSprite spriteWithTexture:obstaclesTxt];
int minX = obstacle.contentSize.width/2;
int maxX = winSize.width-obstacle.contentSize.width/2;
int xRange = maxX-minX;
int minY = obstacle.contentSize.height/2;
int maxY = winSize.height-obstacle.contentSize.height/2;
int yRange = maxY - minY;
randomXloc = (arc4random() % xRange) + minX;
randomYloc = (arc4random() % yRange) + minY;
obstacle.position = ccp(randomXloc, randomYloc);
[_wall2 addChild:obstacle];
}
然后,在更新方法中,如果_wall2将屏幕留在左侧(它从右向左连续滚动),则应删除障碍物。
if (_wall2.position.x+_wall2.boundingBox.size.width <- 0)
{
_wall2.position = ccp(_wall.boundingBox.origin.x+_wall.boundingBox.size.width+1, 0);
[obstacle removeFromParentAndCleanup:YES];
}
当我运行应用程序时,图像随机放置,但当_Wall2离开屏幕时,应用程序崩溃。
我的问题是这个代码是否有问题?