真的需要SKSpriteNode孩子才能正确拍摄

时间:2014-04-09 15:02:13

标签: ios objective-c sprite-kit

所以我有一个shipNode(最初面向右边)有两个名为_laserCannon1(和2)的子项,它们是通过以下方法配置的:

-(void)initializeLaserCannonLocations
{
    CGSize size = CGSizeMake(4.0, 4.0);
    _laserCannon1 = [SKSpriteNode spriteNodeWithColor:[SKColor yellowColor] size:size];
    _laserCannon2 = [SKSpriteNode spriteNodeWithColor:[SKColor yellowColor] size:size];
    _laserCannon1.position = CGPointMake(self.size.width/2, self.size.height/2 + 15);
    _laserCannon2.position = CGPointMake(self.size.width/2, self.size.height/2 - 15);
    [self addChild:_laserCannon1];
    [self addChild:_laserCannon2];
}

然后我根据船的zRotation从他们那里“发射”激光如下

-(void)fireLocalLaser
{
    CGVector standardLaserVelocity = CGVectorMake(700*cosf(_myShip.zRotation - _myShip.laserCannon1.zRotation),
                                                  700*sinf(_myShip.zRotation - _myShip.laserCannon2.zRotation));
    [self spawnLaserFrom:CGPointAdd(_myShip.laserCannon1.position, ship.position); withVelocity:standardLaserVelocity fromShip:_myShip];
    [self spawnLaserFrom:CGPointAdd(_myShip.laserCannon2.position, ship.position); withVelocity:standardLaserVelocity fromShip:_myShip];
}

当船只直接面向左或右时,现在一切正常,但由于一些奇怪的原因,当船开始更多地朝向顶部或底部时,射击越来越朝向船的中部发射,到了一旦船垂直朝向,两艘船都从船的中间位置射出。是否与我放置_laserCannons的位置有关?

2 个答案:

答案 0 :(得分:3)

我希望我的数学老师不会看到这个!

你的问题在于你的2个激光炮旋转时的相对位置。我已经包含了一张图片,所以我不必深入了解正在发生的事情。

enter image description here

如果您真的想要,我已经为您提供了有关如何执行此操作的有关,sorta,胶带解决方案的代码。它不漂亮,但它的工作原理。代码从0到90度是很好的,所以你应该能够从那里解决剩下的事情,如果你的心脏设置有2门大炮。但我强烈建议你的Mighty Space Empire投资单炮,因为它会让你的编码工作变得更加轻松!

@implementation MyScene {
SKSpriteNode *ship1;
SKShapeNode *turret1;
SKShapeNode *turret2;
}

-(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];
}

-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
}

// 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 lasers

-(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
{

    float turret1offsetX = 0;
    float turret1offsetY = 0;
    float turret2offsetX = 0;
    float turret2offsetY = 0;

    if((ship1.zRotation >0) && (ship1.zRotation <=0.785399))
    {
        turret1offsetX = (14/0.785398) * ship1.zRotation;
        turret1offsetY = (23/0.785398) * ship1.zRotation;

        turret2offsetX = (-20/0.785398) * ship1.zRotation;
        turret2offsetY = (10/0.785398) * ship1.zRotation;
    }

    if((ship1.zRotation >0.785399) && (ship1.zRotation <=1.570797))
    {
        turret1offsetX = (14 - (9/0.785398)) * ship1.zRotation;
        turret1offsetY = -4 + (27/0.785398) * ship1.zRotation;

        turret2offsetX = (3 - (25/0.785398)) * ship1.zRotation;
        turret2offsetY = 20 - (10/0.785398) * ship1.zRotation;
    }

    int x = ship1.position.x + 1000 * cos(ship1.zRotation);
    int y = ship1.position.y + 1000 * sin(ship1.zRotation);

    turret1 = [SKShapeNode node];
    CGMutablePathRef pathToDraw = CGPathCreateMutable();
    CGPathMoveToPoint(pathToDraw, NULL, (ship1.position.x+20)+turret1offsetX, (ship1.position.y-25)+turret1offsetY);
    CGPathAddLineToPoint(pathToDraw, NULL, x, y);
    turret1.path = pathToDraw;
    [turret1 setStrokeColor:[UIColor redColor]];
    [self addChild:turret1];

    turret2 = [SKShapeNode node];
    CGMutablePathRef pathToDraw2 = CGPathCreateMutable();
    CGPathMoveToPoint(pathToDraw2, NULL, (ship1.position.x+20)+turret2offsetX, (ship1.position.y+25)+turret2offsetY);
    CGPathAddLineToPoint(pathToDraw2, NULL, x, y);
    turret2.path = pathToDraw2;
    [turret2 setStrokeColor:[UIColor redColor]];
    [self addChild:turret2];
    }
}

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

@end

答案 1 :(得分:-2)

是的,正如sangony所说的,它实际上只与你的shipNode中_laserCannons的相对位置有关。我个人现在就开始使用一个奇异的laserCannon,并且在你准备继续添加更多内容之前,对所涉及的数学工作更加满意。