在cocos2d中设置sprite(旋转)的角度

时间:2014-01-03 12:39:10

标签: ios cocos2d-iphone

我是cocos2d游戏开发的新手。我在屏幕上设置了一个炮塔。当我们触摸屏幕时,炮塔会发射子弹,但我的炮塔角度设置不正确。此代码取自raywenderlich教程系列。此外,子弹没有经过触摸点。

以下是我的代码。

Screenshot

 // Determine offset of location to projectile
    CGPoint offset = ccpSub(cointouchLocation,turretArms.position);

    NSLog(@"offset: %@",NSStringFromCGPoint(offset));

    // Bail out if you are shooting down or backwards

    if (offset.x <= 0) return;
    int realX = winSize.width +(turretArms.contentSize.width/2);
    float ratio =  (float) offset.y/(float) offset.x;
    int realY =(realX * ratio) +turretArms.position.y;
    CGPoint realDest = ccp(realX, realY);

    // Determine the length of how far you're shooting
    int offRealX = realX - turretArms.position.x;
    int offRealY = realY - turretArms.position.y;
    float length = sqrtf((offRealX*offRealX)+(offRealY*offRealY));
     float velocity = 480/1;
    float realMoveDuration = length/velocity;




    // Determine angle to face
   float angleRadians = atanf((float)offRealY / (float)offRealX);

   // NSLog(@"angleRadians : %f",angleRadians);
    float angleDegrees = CC_RADIANS_TO_DEGREES(angleRadians);
    float cocosAngle = -1 * angleDegrees;
    float rotateDegreesPerSecond = 180 / 0.5; // Would take 0.5 seconds to rotate 180 degrees, or half a circle
    float degreesDiff = turret.rotation - cocosAngle;
    float rotateDuration = fabs(degreesDiff / rotateDegreesPerSecond);
    [turret runAction:
     [CCSequence actions:
      [CCRotateTo actionWithDuration:rotateDuration angle:cocosAngle],
      [CCCallBlock actionWithBlock:^{
         // OK to add now - rotation is finished!
         [self addChild:turretArms];
         [turretsAry addObject:turretArms];

         // Release
         [turretArms release];
         turretArms = nil;
     }],
      nil]];

    // Move projectile to actual endpoint
    [turretArms runAction:
     [CCSequence actions:
      [CCMoveTo actionWithDuration:realMoveDuration position:realDest],
      [CCCallBlockN actionWithBlock:^(CCNode *node) {
         [turretsAry removeObject:node];
         [node removeFromParentAndCleanup:YES];
     }],
      nil]];

    turretArms.tag =2;

    //coin delete from window

    for (CCSprite *cmonster in coinmonstersToDelete) {
        // [monster stopAllActions];
        [coinmonstersOnScreen removeObject:cmonster];
        [self removeChild:cmonster cleanup:YES];

    }

1 个答案:

答案 0 :(得分:0)

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

    if (_nextProjectile != nil) return;

    // Choose one of the touches to work with
    UITouch *touch = [touches anyObject];
    CGPoint location = [self convertTouchToNodeSpace:touch];

    // Set up initial location of projectile
    CGSize winSize = [[CCDirector sharedDirector] winSize];
    _nextProjectile = [[CCSprite spriteWithFile:@"projectile2-Hd.png"] retain];


     _nextProjectile.position = ccp(winSize.width/2,20);

    // Determine offset of location to projectile
    CGPoint offset = ccpSub(location, _nextProjectile.position);
     NSLog(@"offset.x : %f",offset.x);
    NSLog(@"offset.x : %f",offset.y);

    // Bail out if you are shooting down or backwards
   if (offset.y <= 0) return;

    int realY = winSize.height - (_nextProjectile.contentSize.width/2);
     float ratio = (float) offset.x / (float) offset.y;
    int realX = (realY * ratio) + _nextProjectile.position.x;
    CGPoint realDest = ccp(realX, realY);

    // Determine the length of how far you're shooting
    int offRealX = realX - _nextProjectile.position.x;
    int offRealY = realY - _nextProjectile.position.y;
    float length = sqrtf((offRealX*offRealX)+(offRealY*offRealY));
    float velocity = 480/1; // 480pixels/1sec
    float realMoveDuration = length/velocity;

    // Determine angle to face
   // float angleRadians = atanf((float)offRealY / (float)offRealX);
    float angleRadians = atanf((float)offRealX / (float)offRealY);

    float angleDegrees = CC_RADIANS_TO_DEGREES(angleRadians);
   // float cocosAngle = -1 * angleDegrees;
     float cocosAngle = angleDegrees;
    float rotateDegreesPerSecond = 180 / 0.5; // Would take 0.5 seconds to rotate 180 degrees, or half a circle
    float degreesDiff = _player.rotation - cocosAngle;
    float rotateDuration = fabs(degreesDiff / rotateDegreesPerSecond);
    [_player runAction:
     [CCSequence actions:
      [CCRotateTo actionWithDuration:rotateDuration angle:cocosAngle],
      [CCCallBlock actionWithBlock:^{
         // OK to add now - rotation is finished!
         [self addChild:_nextProjectile];
         [_projectiles addObject:_nextProjectile];

         // Release
         [_nextProjectile release];
         _nextProjectile = nil;
     }],
      nil]];

    // Move projectile to actual endpoint
    [_nextProjectile runAction:
     [CCSequence actions:
      [CCMoveTo actionWithDuration:realMoveDuration position:realDest],
      [CCCallBlockN actionWithBlock:^(CCNode *node) {
         [_projectiles removeObject:node];
         [node removeFromParentAndCleanup:YES];
    }],
      nil]];

    _nextProjectile.tag = 2;

  // [[SimpleAudioEngine sharedEngine] playEffect:@"pew-pew-lei.caf"];
}