Rotating a sprite to touch

Posted by user1691659 on Game Development See other posts from Game Development or by user1691659
Published on 2012-10-16T22:33:11Z Indexed on 2012/10/17 5:27 UTC
Read the original article Hit count: 119

Filed under:

I know this has been asked before and Ive looked through peoples questions and answers all over the internet and I just cant get it to work. Im sure its down to my inexperience. Ive got an arrow attached to a sprite, this sprite appears when touched but I want the arrow to point where the sprite will launch. I have had the arrow moving but it was very erratic and not pointing as it should. Its not moving at the moment but this is what I have.

- (void)update:(ccTime)delta
{
    arrow.rotation = dialRotation;
}

-(void) ccTouchMoved:(UITouch*)touch withEvent:(UIEvent *)event
{
    pt1 = [self convertTouchToNodeSpace:touch];

    CGPoint firstVector = ccpSub(pt, arrow.position);
    CGFloat firstRotateAngle = -ccpToAngle(firstVector);
    CGFloat previousTouch = CC_RADIANS_TO_DEGREES(firstRotateAngle);

    CGPoint vector = ccpSub(pt1, arrow.position);
    CGFloat rotateAngle = -ccpToAngle(vector);
    CGFloat currentTouch = CC_RADIANS_TO_DEGREES(rotateAngle);

    dialRotation += currentTouch - previousTouch;
    }

I have got pt from ccTouchBegan the same way as pt1

Thanks

© Game Development or respective owner

Related posts about cocos2d-iphone