Note that there are some explanatory texts on larger screens.

plurals
  1. POCocos2D - CCSprite touched followed by popup
    text
    copied!<p>I've been stumped for days trying to get my sprite touch method under control. My aim is to have another sprite pop up offset to a touched sprite.</p> <p>I currently have a half baked code for this process, in which I have experimented with many varieties of code, see links below, none of which has worked. My questions:</p> <ol> <li>Getting CCTouchBegan to run the method towerPositionTapped once the touch hits a sprite on screen (the NSLog in CCTouchBegan is also not working).</li> <li>Find out how to get the location of the touched sprite so that it can be used in the towerPositionTapped method.</li> </ol> <p><strong>My touch specific code so far in the main layer:</strong></p> <pre><code>-(void) registerWithTouchDispatcher{ [[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES]; } -(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{ CGPoint location = [touch locationInView: [touch view]]; location = [[CCDirector sharedDirector] convertToGL: location]; //_towersFromClass is an NSMutableArray with CCSprite objects for(CCSprite *tb in _towersFromClass){ if(CGRectContainsPoint(tb.boundingBox,location)){ NSLog(@"sprite touched at %@", NSStringFromCGPoint(location)); [self towerPositionTapped] return YES; } } return NO; } </code></pre> <p>The NSLog check doesn't end up showing when I click on any of the sprites. </p> <p>For the towerPositionTapped method I am unsure how to capture the touched location (or record the touched sprite) in order to position the new sprite.</p> <p><strong>towerPositionTapped.m</strong></p> <pre><code>-(void)towerPositionTapped{ CCMenuItem *towerOption1 = [CCMenuItemImage itemWithNormalImage:@"tower.png" selectedImage:@"tower.png"]; towerOption1.position = /* place touched sprite location here, with an offset*/; CCMenu *_towerOptionsMenu = [CCMenu menuWithItems: nil]; _towerOptionsMenu.position = CGPointZero; [self addChild:_towerOptionsMenu z:5]; </code></pre> <p>}</p> <p>Any constructive criticism is appreciated. Thank you for your time.</p> <p>A few of the sites I have been testing:</p> <p><a href="http://www.cocos2d-iphone.org/forums/topic/simple-question-offset-sprite-position/" rel="nofollow noreferrer">Sprite Offset</a></p> <p><a href="https://stackoverflow.com/questions/5222375/cocos2d-handling-touch-with-multiple-layers">Touch 1</a></p> <p><a href="https://stackoverflow.com/questions/637743/how-can-i-detect-touch-in-cocos2d">Touch 2</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload