Reputation: 1351
I have an array of ten sprites; they all sit in the same area of the screen For ease of explanation, I'll say at the bottom-middle, stacked one on top of each other. What I need to do is take the top sprite and throw it off the screen by swiping it forward (bottom to top). When I have a single sprite, I don't have an issue with this; it's when I have multiple sprites that I have the issues. I add the items to the layer like so:
for(int i=0; i<maxCount; i++){
CCSprite *x = [listOfItems objectAtIndex:i];
//NSLog(@"(%f, %f)", x.position.x, x.position.y);
[self addChild:x];
}
They are created in a step above:
for (int i=0; i<maxCount; i++) {
CCSprite *o = [CCSprite spriteWithFile:@"image.png"];
o.position = ccp(windowSize.width/2,windowSize.width/2);
[listOfItems addObject:o];
[o release];
}
I guess the real issue is I don't know how to handle the "current" top. So, if I have array index 0 as the top, I can just go ahead and use the touch gesture and "flick" it off the screen. The first one works all right, but once I touch the second one it crashes with EXC_BAD_ACCESS. Here's the TouchesBegan method:
-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
startingPoint = location;
//Trying something here....
//if(current)
// [current release];
current = [listOfItems objectAtIndex:currentPosition];
[self reorderChild:current z:2];
actionStartTime = [NSDate timeIntervalSinceReferenceDate];
}
When the "Trying something here..." lines are uncommented, that's where the crash happens, and when they are commented out, it happens when assigning current
. I know I'm missing something here, but I just can't figure it out.
Upvotes: 1
Views: 578
Reputation: 634
If your objects are are all added in an array I find the easiest thing to do is set a different tag to each object.
for(int i = 0; i < NUMOFOBJECTS; i++)
{
CCSprite *sprite = blah blah whatever;
// I do something like 100+i so that each time I want to
// create different types of objects I can subtract the "100"
// or whatever it is to get a base index value for arrays.
sprite.tag = 100+i;
}
Inside of your CCTouchesBegan you check the touch to make sure it is on the items and then you iterate through your items there and move only the item with the correct tag.
for(CCSprite *sprite in objects)
{
if(sprite.tag - 100 == [objectOrderList objectAtIndex:0].tag)
{
// Do whatever
}
}
Treat that as pseudo code as I am just writing off the top of my head. In this situation you would keep track of object order in an NSMutableArray, removing/replacing/adding objects whenever order is changed.
Upvotes: 1
Reputation: 237010
Look at how you set current
— it's set to [listOfItems objectAtIndex:currentPosition]
, which is an object you don't own. So by the next go-round, that object might have been deallocated, and sending it any message once it's deallocated (including release
) is undefined behavior, which will result in EXC_BAD_ACCESS
if you're lucky. (It can result in much subtler and harder to catch bugs if you're unlucky.)
Upvotes: 1
Reputation: 13992
One point would be to remove [o release];
CCSprites are created as autoreleased objects.
Also, to find the reason of a crash, try opening the Debug Console which is under the Run menu. The last thing in the console will often display a message with why the error occured.
Upvotes: 1