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.
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:
- Getting CCTouchBegan to run the method towerPositionTapped once the touch hits a sprite on screen (the NSLog in CCTouchBegan is also not working).
- Find out how to get the location of the touched sprite so that it can be used in the towerPositionTapped method.
My touch specific code so far in the main layer:
-(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;
}
The NSLog check doesn't end up showing when I click on any of the sprites.
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.
towerPositionTapped.m
-(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];
}
Any constructive criticism is appreciated. Thank you for your time.
A few of the sites I have been testing: