【问题标题】:Shoot from rotating body从旋转体拍摄
【发布时间】:2014-04-07 18:43:17
【问题描述】:

我有一个可以发射子弹的旋转敌人身体。它工作得很好,但它似乎向所有方向射击,除了直下。自从我参加三角学习以来已经有一段时间了,我想我忘记了一些事情。我必须检查敌人的旋转吗?

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

【问题讨论】:

  • 你想360度随机拍摄吗?
  • 不,敌人只是在旋转。
  • 好的。我正在尝试理解您的代码逻辑。你为什么不在你的敌人精灵上使用一个简单的 zRotation = XXX 来旋转你的精灵而不是你拥有的?
  • 啊我不是要旋转敌人的精灵或子弹。敌人在旋转,子弹从中射出。我希望子弹在旋转时从敌人的正面发射。
  • 知道了。到目前为止,此链接可能会对您有所帮助,您可以用精灵 zRotation 替换方程式中的角度值。祝你好运。 stackoverflow.com/questions/14096138/…

标签: sprite-kit 2d-games


【解决方案1】:

这是一艘可以 360 度转动并从任何角度射击的船的代码。轻触屏幕左侧轻微旋转飞船,轻触屏幕右侧进行射击。

@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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-01
    • 1970-01-01
    • 2017-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多