ios - 真的需要SKSpriteNode child 才能正确射击

标签 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];
}

现在,当飞船直接面向左侧或右侧时,一切正常,但由于某些奇怪的原因,当飞船开始更多地朝向顶部或底部时,炮弹越来越多地朝向飞船的中部发射,以至于一旦船垂直面向,两枪都会从船的 middle.position.x 发射。这是否与我放置 _laserCannons 的位置有关?

最佳答案

我希望我的数学老师没有看到这个!

你的问题是你的 2 个激光炮在旋转时的相对位置。我附上了一张照片,这样我就不必对正在发生的事情进行冗长的描述。

enter image description here

我已经包含了一个有点,有点,管道胶带解决方案的代码,如果你真的想要的话,你可以如何做到这一点。它不漂亮,但它有效。代码适用于 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

关于ios - 真的需要SKSpriteNode child 才能正确射击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22966579/

相关文章:

objective-c - 自定义 UIGestureRecognizer : selector does not receive UIGestureRecognizerStateBegan

iOS IAP - 如何区分 "restoring done, but not purchased before"状态

ios - SpriteKit 异常 'PKPhysicsBody' 崩溃

ios - SpriteKit中的加速度计不起作用

iphone - 从 nib 文件加载 View 时对其进行动画处理

iphone - nsurlconnection 服务器需要特殊设置吗?

ios - 将数组中的闭包/ block 作为参数传递 iOS

ios - Unwind segue 不会释放我的 View Controller

ios - 如何将按钮的状态保存到 NSUserDefaults?

swift - 如何使物理体粘附在节点 anchor 上