My goal is to add a sprite, leftOn, to the screen after a user touches the screen.
Here's my code:
-(void)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector]convertToGL:location];
if (CGRectContainsPoint([leftOff boundingBox],location)) {
CCLOG(@"Left Pressed");
[self addChild:leftOn];
}
}
The Log always outputs "Left Pressed" but stops running without adding the sprite. If I add the sprite in the init method it does show. Also, I've tried changing the order of the sprite e.g., [self addChild:leftOn z:2], and it still does not display.
What do you mean under "stops running"? Your application crashes? Make sure that leftOn is not nil
, as Ben Trengrove said and that this sprite was not added to another parent. Both cases may cause crash. If your app crashes, it can be useful to see an error from the console. It can help to understand where is a problem.