菜鸟教程小白 发表于 2022-12-12 21:21:48

ios - 真的需要 SKSpriteNode child 才能正确拍摄


                                            <p><p>所以我有一个 shipNode(最初朝右),有两个名为 _laserCannon1(和 2)的子节点,它们通过以下方法配置:</p>

<pre><code>-(void)initializeLaserCannonLocations
{
    CGSize size = CGSizeMake(4.0, 4.0);
    _laserCannon1 = size:size];
    _laserCannon2 = 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);
    ;
    ;
}
</code></pre>

<p>然后我稍后会根据飞船的 zRotation 从他们身上“发射”激光,如下所示</p>

<pre><code>-(void)fireLocalLaser
{
    CGVector standardLaserVelocity = CGVectorMake(700*cosf(_myShip.zRotation - _myShip.laserCannon1.zRotation),
                                                700*sinf(_myShip.zRotation - _myShip.laserCannon2.zRotation));
    ;
    ;
}
</code></pre>

<p>现在,当船只直接面向左侧或右侧时,一切正常,但由于某些奇怪的原因,随着船只开始更多地面向顶部或底部,炮弹越来越多地朝船中部射击,以至于一旦船垂直面对,两枪都是从船的 middle.position.x 发射的。这与我放置 _laserCannons 的位置有关吗?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我希望我的数学老师没有看到这个!</p>

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

<p> <img src="/image/ekQcJ.png" alt="enter image description here"/> </p>

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

<pre><code>@implementation MyScene {
SKSpriteNode *ship1;
SKShapeNode *turret1;
SKShapeNode *turret2;
}

-(id)initWithSize:(CGSize)size
{
   if (self = ) {
         self.physicsWorld.contactDelegate = self;
         ;
    }
    return self;
}


-(void)createSpaceships {
ship1 = size:CGSizeMake(50, 50)];
ship1.position = CGPointMake(300, 150);
ship1.physicsBody = ;
ship1.physicsBody.dynamic = NO;
;

SKSpriteNode *frontOfShip = size:CGSizeMake(2, 50)];
frontOfShip.position = CGPointMake(25, 0);
;
}

-(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 = ;
CGPoint touchLocation = ;

if(touchLocation.x &lt; self.size.width/2) // left side of screen
{
    SKAction *block0 = [SKAction runBlock:^{
      ship1.zRotation = ship1.zRotation +0.0785398;
    }];
    ;
} else // right side of screen
{

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

    if((ship1.zRotation &gt;0) &amp;&amp; (ship1.zRotation &lt;=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 &gt;0.785399) &amp;&amp; (ship1.zRotation &lt;=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 = ;
    CGMutablePathRef pathToDraw = CGPathCreateMutable();
    CGPathMoveToPoint(pathToDraw, NULL, (ship1.position.x+20)+turret1offsetX, (ship1.position.y-25)+turret1offsetY);
    CGPathAddLineToPoint(pathToDraw, NULL, x, y);
    turret1.path = pathToDraw;
    ];
    ;

    turret2 = ;
    CGMutablePathRef pathToDraw2 = CGPathCreateMutable();
    CGPathMoveToPoint(pathToDraw2, NULL, (ship1.position.x+20)+turret2offsetX, (ship1.position.y+25)+turret2offsetY);
    CGPathAddLineToPoint(pathToDraw2, NULL, x, y);
    turret2.path = pathToDraw2;
    ];
    ;
    }
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
;
;
}

@end
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 真的需要 SKSpriteNodechild 才能正确拍摄,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/22966579/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/22966579/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 真的需要 SKSpriteNode child 才能正确拍摄