Hi
I have been using cocos2D for a little while now and have decided to design and build a new game. I have come across a problem with using touches to move the scene around. What want to do is for the user to be able to pan around by dragging their finger across the screen and as they do the screen follows. This I have managed to do, however any interactive elements in the screen do not respond to touches as I expect. The sprites move as I drag my finger across the screen, but when i try to interact with something such as a wheel I created, it acts as if it is still in the same place.
I thought the best way to do what i needed would be to add all my sprites to a CCNode and then as the user pans i could move the CCNode and its children follow:
Init Method
// Set up the World Node
world = [CCNode node];
[self addChild:world];
// Create the sprites
CGSize size = [[CCDirector sharedDirector] winSize];
// background
CCSprite *background = [CCSprite spriteWithFile:@"Background.png"];
background.position = ccp(size.width/2, size.height/2);
[world addChild:background];
// wheel
shipWheel = [CCSprite spriteWithFile:@"ShipWheel.png"];
shipWheel.position = ccp(size.width/2, 56);
wheelCenter = CGPointMake(shipWheel.position.x, shipWheel.position.y);
[world addChild:shipWheel];
Then in my touches method:
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if (touch) {
CGPoint location = [touch locationInView:[touch view]];
CGPoint convertedPoint = [[CCDirector sharedDirector] convertToGL:location];
// check touch is inside wheel
if ((convertedPoint.x < shipWheel.position.x+shipWheel.contentSize.width/2) &&
(convertedPoint.x > shipWheel.position.x-shipWheel.contentSize.width/2) &&
(convertedPoint.y < shipWheel.position.y+shipWheel.contentSize.height/2) &&
(convertedPoint.y > shipWheel.position.y-shipWheel.contentSize.height/2)) {
// Imagine some code that would make a wheel spin here
} else {
worldIsPanning = YES; // BOOL
// start the pan geasture
worldPreviousPoint = convertedPoint;
worldCurrentPoint = convertedPoint;
toMoveX = 0; // float
}
}
}
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if (touch) {
CGPoint location = [touch locationInView:[touch view]];
CGPoint convertedPoint = [[CCDirector sharedDirector] convertToGL:location];
if (wheelShouldSpin == YES) { // user is interacting with the wheel
// imagine some more code that would cause a wheel sprite to spin
} else { // user is interacting with the screen and wants to pan the screen left or right.
// World is Panning (world is being dragged)
worldCurrentPoint = convertedPoint;
toMoveX = worldCurrentPoint.x - worldPreviousPoint.x;
world.position = ccp(world.position.x + toMoveX, world.position.y);
worldPreviousPoint = worldCurrentPoint;
}
}
}
This is fine but the wheel seems to pretend it is in the same place so if i touch the centre of the screen (where the wheel is initially) even if it is not under my touch it reacts.
Is there a way I can resolve the problem and should I be doing this a completely different way?
Thanks,
JetWilson