3

I have a rotating enemy body that shoots bullets. It's working pretty good, but it appears to fire in all directions except straight down. It's been a while since I took trig, and I think I'm forgetting something. Do I have to check the rotation of the enemy?

        SKAction *shoot = [SKAction moveTo:CGPointMake(2000*cosf(enemy.zRotation),2000*sinf(enemy.zRotation)) duration:5];
        SKAction *remove = [SKAction removeFromParent];

        [bullet runAction:[SKAction sequence:@[shoot,remove]]];
Negora
  • 281
  • 4
  • 13
  • Are you looking to shoot randomly 360 degrees? – sangony Apr 07 '14 at 20:21
  • No, the enemy is just rotating around. – Negora Apr 07 '14 at 21:06
  • Ok. I am trying to understand your code's logic. Why don't you use a simple zRotation = XXX on your enemy sprite to rotate your sprite instead of what you have? – sangony Apr 07 '14 at 21:17
  • Ah I'm not trying to rotate the enemy sprite or bullet. The enemy is rotating and bullets are firing from it. I want the bullet to fire from the front of the enemy as its rotating. – Negora Apr 07 '14 at 21:40
  • Got it. This link might help you in so far that you can substitute the angle value in the equation by your sprites zRotation. Good luck. http://stackoverflow.com/questions/14096138/find-the-point-on-a-circle-with-given-center-point-radius-and-degree – sangony Apr 07 '14 at 21:54
  • Or take a look at this tutorial: http://www.raywenderlich.com/57370/trigonometry-game-programming-sprite-kit-version-part-2 –  Apr 08 '14 at 11:23

1 Answers1

1

Here is some code for a ship that can turn 360 degrees and fire from any angle. Touch the left hand side of the screen to slightly rotate the ship and the right side of the screen to shoot.

@implementation MyScene
{
    SKSpriteNode *ship1;
    SKShapeNode *beam1;
}

-(id)initWithSize:(CGSize)size 
{
    if (self = [super initWithSize:size])
    {
        //self.physicsWorld.contactDelegate = self;
        [self createSpaceships];
    }
    return self;
}


-(void)createSpaceships 
{
    ship1 = [SKSpriteNode spriteNodeWithColor:[SKColor blueColor] size:CGSizeMake(50, 50)];
    ship1.position = CGPointMake(300, 150);
    ship1.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:ship1.size];
    ship1.physicsBody.dynamic = NO;
    [self addChild:ship1];

    SKSpriteNode *frontOfShip = [SKSpriteNode spriteNodeWithColor:[SKColor grayColor] size:CGSizeMake(2, 50)];
    frontOfShip.position = CGPointMake(25, 0);
    [ship1 addChild:frontOfShip];
}

// touch the left side of the screen to rotate the ship by +0.0785398 radians
// touch the right hand side of the screen to fire laser

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInNode:self.scene];

    if(touchLocation.x < self.size.width/2) // left side of screen
    {
        SKAction *block0 = [SKAction runBlock:^{
            ship1.zRotation = ship1.zRotation +0.0785398;
        }];
        [self runAction:block0];
    } else // right side of screen
    {
        int x = ship1.position.x + 1000 * cos(ship1.zRotation);
        int y = ship1.position.y + 1000 * sin(ship1.zRotation);

        beam1 = [SKShapeNode node];
        CGMutablePathRef pathToDraw = CGPathCreateMutable();
        CGPathMoveToPoint(pathToDraw, NULL, ship1.position.x, ship1.position.y);
        CGPathAddLineToPoint(pathToDraw, NULL, x, y);
        beam1.path = pathToDraw;
        [beam1 setStrokeColor:[UIColor redColor]];
        [self addChild:beam1];
    }
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    //[beam1 removeFromParent];
}

-(void)update:(CFTimeInterval)currentTime
{
    //
}

@end
sangony
  • 11,636
  • 4
  • 39
  • 55