OStack程序员社区-中国程序员成长平台

标题: ios - 真的需要 SKSpriteNode child 才能正确拍摄 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 21:21
标题: ios - 真的需要 SKSpriteNode child 才能正确拍摄

所以我有一个 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 的位置有关吗?



Best Answer-推荐答案


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

您的问题在于 2 个激光炮在旋转时的相对位置。我已经附上了一张图片,所以我不必对正在发生的事情进行冗长的描述。

enter image description here

我已经包含了一个有点,有点,管道胶带解决方案的代码,如果你真的想要,你可以如何做到这一点。它不漂亮,但它有效。代码在 0 到 90 度范围内都很好,所以如果你下定决心要拥有 2 门大炮,你应该能够从那里解决剩下的问题。但我强烈建议您的 Mighty Space Empire 投资于单炮舰,因为这将使您的编码工作变得更加轻松!

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

-(id)initWithSizeCGSize)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)updateCFTimeInterval)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)touchesBeganNSSet *)touches withEventUIEvent *)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)touchesEndedNSSet *)touches withEventUIEvent *)event {
[turret1 removeFromParent];
[turret2 removeFromParent];
}

@end

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






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4