I can’t work out how to pass the touch location in touch began into my method that is ran when touch began starts.
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
[self schedule:@selector(moveSprite:)];
return TRUE;
}
-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
[self unschedule:@selector(moveSprite:)];
}
-(void)moveSprite:(ccTime) delta
{
CGPoint spriteCenter = CGPointMake(player.contentSize.width /2,player.contentSize.height /2);
CGPoint touchPoint; //how to get this touch began?
float distanceX = touchPoint.x - spriteCenter.x;
float distanceY = touchPoint.y - spriteCenter.y;
float angle = atan2f(distanceY,distanceX); // returns angle in radians
player.rotation = angle;
}
I also have a question about [self schedule:@selector: Will this continuously call my move sprite method? As I’m going to want the sprite to continuously move and change rotation accordingly as the touch is held down and the sprites position changes.
My final question is about moving the sprite to the x coordinate of the touch. If I use ccmoveto I can’t use velocity to make the sprite slowly increase its speed can I? How would I move the sprite to the touched point increasing velocity? I’m guessing its something to do with delta.
Actually, I don’t see any need in your scheduled method. You just can implement
method and place your sprite rotation logic there.
To get changeable move velocity, you can use one of
CCActionEasesubclasses. Wrap your move action in one of them and you will see velocity changes during movement. Something likeyou can see few examples here