0
votes

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:

  1. Getting CCTouchBegan to run the method towerPositionTapped once the touch hits a sprite on screen (the NSLog in CCTouchBegan is also not working).
  2. 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:

Sprite Offset

Touch 1

Touch 2

2
Does you ccTouchBegan is working? If you put CClog above for loop will cclog will show anything?Zohaib

2 Answers

0
votes

If you CCTouchBegin in not working ( not inside for loop but overall) Then may be you have to try this line if you havnt EnableTouch

self.isTouchEnabled = YES;

I am using this line rather than registerWithTouchDispatcher.

0
votes

It's taken me a few days to solve this, none the less I have managed to come up with a solution.

I've basically used the same code as in Ray Wenderlich's Tower Defence Tutorial (RWTDTutorial). Using the CCTouchesBegan method.

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

for(UITouch *touch in touches){
    CGPoint location = [touch locationInView:[touch view]];

    location = [[CCDirector sharedDirector] convertToGL:location];

    for(CCSprite *tb in _towersFromClass){

        if(CGRectContainsPoint([tb boundingBox],location)){

            selectedTowerPosition = tb;
            [self towerPositionTapped];

        }
    }
}
}

I created another method which contains the CCMenu items I wanted to pop up after I selected one of the CCSprites in my _towersFromClass array, and added that under the 'if' statement of the CCTouchesBegan method.

In order to get the correct location for the menu, I created a CCSprite property '_selectedTowerPosition' and made it = the for 'tb' CCSprite.

Back in the CCMenu method I used the ccpAdd code, as seen below, to position the menu.

CCMenuItem *towerOption1 = [CCMenuItemImage itemWithNormalImage:@"tower.png" selectedImage:@"tower.png" target:self selector:@selector( towerSelectOption)];
towerOption1.position = ccpAdd(_selectedTowerPosition.position,ccp(40,0));

I also gave the menu item a slight offset.

It's taken me awhile but I've learned a lot from this experience.