射击方向不正确cocos2D-iPhone

时间:2013-04-13 10:20:18

标签: iphone ios objective-c cocos2d-iphone

Bows rotates 360 Degree using touch and it fires arrow while touch ended.

Moving portion works perfectly but their need some correction while touch endes..
The problem is while i touch the bow,moving direction of arrow is opposite to bow such as 

enter image description here

虽然我向相反的方向旋转弓,但它工作正常,但只有当弓旋转时..箭头应该从右向左射击,弓的位置应该是正确的。

我应该做些什么来纠正它们。?! 对不起,我的英文..

我的代码如下..

-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
    CGPoint location = [touch locationInView:[touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];

    [self setArrowFromPt:location centerPt:windowCenter];
}

-(void) setArrowFromPt:(CGPoint)touchPt centerPt:(CGPoint)centerPt
{
    diff = ccpSub(touchPt, centerPt);
    NSLog(@"%@", NSStringFromCGPoint(diff));

    angle = ccpToAngle(diff);
    normalized = ccpNormalize(diff);

    float START_ANGLE = 0;
    float RADIUS = 100.0;

    imgArrow.position = ccpAdd(centerPt, ccpMult(normalized, RADIUS));
    cocosAngle = -CC_RADIANS_TO_DEGREES(angle);
    imgArrow.rotation = START_ANGLE + cocosAngle;
}

-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
    CGPoint location = [touch locationInView:[touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];

    cocosAngle =  -CC_RADIANS_TO_DEGREES(angle);
    curangle = imgArrow.rotation;
    CGFloat rotateDiff = cocosAngle - curangle;  
    CGFloat rotateSpeed = 180; 
    rotateDuration = fabs(rotateDiff / rotateSpeed);

    if (_nextProjectile != nil) return;
    _nextProjectile = [[CCSprite spriteWithFile:@"newarrow.png"] retain];
    _nextProjectile.anchorPoint = ccp(0,0);
    _nextProjectile.position = ccp(380,150);

    [imgArrow runAction:[CCSequence actions:
                       [CCRotateTo actionWithDuration:rotateDuration angle:cocosAngle],
                       [CCCallFunc actionWithTarget:self selector:@selector(finishShoot)],
                       nil]]; 

    ccTime delta = 1.0;
    CGPoint normalizedShootVector = ccpNormalize(diff);
    CGPoint overshotVector = ccpMult(normalizedShootVector, 420);
    CGPoint offscreenPoint = ccpAdd(_nextProjectile.position, overshotVector);

    [_nextProjectile runAction:[CCSequence actions:
                                [CCMoveTo actionWithDuration:delta position:offscreenPoint],
                                [CCCallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished:)],
                                nil]];
    _nextProjectile.tag = 2;

}

任何帮助都是赞赏的。 在此先感谢!!

1 个答案:

答案 0 :(得分:0)

我可以回答我的问题,

我通过纠正

解决了我的问题

CGPoint overshotVector = ccpMult(normalizedShootVector,-420);

put - sign,导致我的箭头从右向左投掷。