0
\$\begingroup\$

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

\$\endgroup\$
2
  • \$\begingroup\$ This seems to localized or at least the problem is presented in such way. \$\endgroup\$ Oct 17, 2012 at 7:46
  • 1
    \$\begingroup\$ I don't understand why you keep track of the previous angle and current angle? Also why do you increment dialRotation? Just point the bloody sprite to the current touch location (eg. sprite.rotation = currentTouch). \$\endgroup\$
    – bummzack
    Oct 17, 2012 at 9:19

2 Answers 2

0
\$\begingroup\$

try updating arrow.rotation from ccTouchMoved and not in the update.

-(void)update:(ccTime)delta
{
    (empty)
}

-(void) ccTouchMoved:(UITouch*)touch withEvent:(UIEvent *)event
{
    ...

    dialRotation += currentTouch - previousTouch;
    arrow.rotation = dialRotation;
}
\$\endgroup\$
1
  • \$\begingroup\$ That makes the arrow move when the touch moves. But it just spins wildly! Where have I gone wrong? \$\endgroup\$
    – Marc
    Oct 17, 2012 at 7:07
0
\$\begingroup\$

Ok I figured it with another method.

-(void) calcAngleAndRotateObjectStartingAtPoint:(CGPoint)start endingAtPoint:(CGPoint) end
{
    CGPoint diff = ccpSub(end, start);
    float rads = atan2f( diff.y, diff.x);
    float degs = -CC_RADIANS_TO_DEGREES(rads);
    degs += 90.0f; // 0 is pointing right, so offset so our angle is pointing out of the top of the sprite
    [aim setRotation:degs];
}

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self ccTouchesMoved:touches withEvent:event];
}

- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    if( touch ) {
        CGPoint touchPoint = [touch locationInView: [touch view]];
        touchPoint = [[CCDirector sharedDirector] convertToGL:touchPoint];
        [self calcAngleAndRotateObjectStartingAtPoint:aim.position endingAtPoint:touchPoint];
    }
}

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{}
@end

Thanks for all your answers but this did it for me.

\$\endgroup\$
1
  • \$\begingroup\$ I would avoid the 90-degree offset, and instead, rotate your image 90 degrees. It can bite you later. \$\endgroup\$
    – ashes999
    Oct 18, 2012 at 20:07

Not the answer you're looking for? Browse other questions tagged .