Reputation: 621
How I can get the touch location on a sprite body so I know what force to apply to the body.
So for instance if the sprite and body is in the shape of a rectangle and the player touches the top half of the rectangle, I'd want to apply a downward force or if the player touches the rectangle in the middle of the shape then I don't want to apply any force.
I can work out how to check if the touch location of the player's finger is on the object but calculating where the player touched on the object's body itself, I'm not sure how to go about it.
Upvotes: 1
Views: 185
Reputation: 3384
It would be better if you shown your code, but generally you can get touch location inside the sprite either using locationInNode:
or convertToNodeSpace:
methods, depending on what you already have.
Something like this:
-(void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
//Check if you touched the sprite in case
//you’re handling touches in your scene
//If you subclassed CCSprite and handling touches there
//you don’t have to do anything here.
CCSprite *yourSprite = ...;
CGPoint touchLocation = [touch locationInNode:yourSprite];
float halfHeight = yourSprite.boundingBox.size.height * 0.5f;
if (touchLocation.y >= halfHeight)
{
//Upper part
}
else
{
//lower part
}
}
Note, that you can handle touches in the scene, and then you first need to check whether you touched your sprite at all, or you can subclass CCSprite
and implement touchBegan:
method to get notified when the player touches your sprite (then you don’t need to check anything and yourSprite
becomes self
).
Upvotes: 1