In a game that I am making whilst using Cocos2d, I have a sprite down the bottom of the screen that stays still. When the screen is tapped, I would like the sprite to move to where the screen was tapped, and then animate through the series of frames, then move to its original position. I know that I will need to use a CCSequence, but I don’t yet know how to make it move to the location of the touch. At the moment, I have searched around and I am using this code:
-(void) TouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *Touch = [touches anyObject];
CGPoint location = [Touch locationInView:[Touch view]];
[swat runAction:[CCMoveTo actionWithDuration:3 position:location]];}
I am getting no errors, but the sprite is unresponsive. Any ideas?
First, you have a typo in method’s name. It’s “ccTouchesBegan”, not “TouchesBegan”.
-(void)ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)eventSecond, your sprite will not move to where you expect.
locationInViewreturns point in UIKit coordinates, andCCMoveTouses OpenGL coordinates. You need to convert the point to OpenGL coordinates:UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];