How set and count Angle of sprite (turret) object in portrait mode in cocos2d?

| | August 5, 2015

I am new in cocos2d.I follow Raywenderlich tutorial series.
I wish to set turrets in below of the screen in portrait mode.when user touch that location fire bullet and turret fire position at the touch location. how can i do that.
Below is my image of turret position.enter image description here

One Response to “How set and count Angle of sprite (turret) object in portrait mode in cocos2d?”

  1. Renish Dadhaniya on November 30, -0001 @ 12:00 AM
    - (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"];
    }
    

Leave a Reply