2
votes

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.

2
Does leftOn exist? Show where you alloc leftOnBen Trengrove

2 Answers

0
votes

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.

0
votes

I'm just starting out with cocos2d, but the demos I've seen add sprites at ccTouchesEnded, while using ccTouchesBegan for actions on what was already present and being touched.