Reputation: 440
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?
Upvotes: 0
Views: 248
Reputation: 1
First make sure you have registered TouchDispatcher
:
(void) registerWithTouchDispatcher
{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}
Then implement :
(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event method
and also confirm you have:
self.isTouchEnabled = YES;
line of code in init()
method.
Upvotes: 0
Reputation: 7850
First, you have a typo in method's name. It's "ccTouchesBegan", not "TouchesBegan".
-(void)ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
Second, your sprite will not move to where you expect. locationInView
returns point in UIKit coordinates, and CCMoveTo
uses 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];
Upvotes: 3
Reputation: 1408
Use
-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
Upvotes: 1