以圆形图案SpriteKit拍摄物体

时间:2014-10-15 08:41:23

标签: ios xcode sprite-kit sprite skaction

我真的不知道如何解释我之后的事情,所以我画了一些(非常)艺术图表来帮助传达这个想法。我也会尽力解释它。

我实际上是在尝试拍摄'子弹/激光/任何来自屏幕中心的圆圈,并以相当快的速度重复这一点。这里有两张图片,展示了我想要达到的目标:(没有足够的声誉在这里张贴它们。

(1)http://i.imgur.com/WpZlTQ7.png 这就是我希望子弹射击的地方,以及我想要多少。

(2)http://i.imgur.com/psdIjZG.png 这几乎是最终的结果,我希望他们反复点火并使屏幕看起来像这样。

为了达到这个目的,任何人都可以向我推荐我应该看到的内容吗?

2 个答案:

答案 0 :(得分:2)

处理圆圈时,通常更容易使用极坐标。在这种情况下,每个方向都可以用大小和角度来表示,其中大小是应用于子弹的力/脉冲的量,角度是射击子弹的方向。

基本步骤是

  1. 确定方向数
  2. 通过将2 * PI(360度)除以方向数
  3. 来确定角度增量
  4. 从角度= 0
  5. 开始
  6. 按角度指定的方向拍摄子弹
  7. 将角度和幅度转换为笛卡尔坐标以形成矢量
  8. 使用向量
  9. 对子弹施加冲动或力
  10. 按角度增量增加角度
  11. 以下是如何在Obj-C中执行此操作的示例:

    @implementation GameScene {
        CGFloat angle;
        SKTexture *texture;
        CGFloat magnitude;
        CGFloat angleIncr;
    }
    
    -(void)didMoveToView:(SKView *)view {
        /* Setup your scene here */
    
        long numAngles = 15;
        magnitude = 1;
    
        angleIncr = 2 * M_PI / numAngles;
        angle = 0;
    
        texture = [SKTexture textureWithImageNamed:@"Spaceship"];
    
        SKAction *shootBullet = [SKAction runBlock:^{
            SKSpriteNode *bullet = [SKSpriteNode spriteNodeWithTexture:texture];
            bullet.size = CGSizeMake(8, 8);
            bullet.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:bullet.size.width/2];
            bullet.physicsBody.affectedByGravity = NO;
            bullet.position = self.view.center;
            bullet.zRotation = angle-M_PI_2;
            [self addChild:bullet];
            CGFloat dx = magnitude * cos(angle);
            CGFloat dy = magnitude * sin(angle);
            CGVector vector = CGVectorMake(dx,dy);
            [bullet.physicsBody applyImpulse:vector];
            angle = fmod(angle+angleIncr,2*M_PI);
        }];
    
        SKAction *wait = [SKAction waitForDuration:0.25];
    
        SKAction *shootBullets = [SKAction sequence:@[shootBullet, wait]];
    
        [self runAction:[SKAction repeatActionForever:shootBullets]];
    }
    

答案 1 :(得分:0)

根据我的理解,你希望这个精灵每X秒拍摄一堆射弹。

    SKAction *ShootProjectiles = [SKAction runBlock:^{ 
                        //Create Projectile1
                          projectile1.physicsBody.applyImpulse(CGVectorMake(1, 0)); //Shoot directly right
                          //Create Projectile2
                          projectile2.physicsBody.applyImpulse(CGVectorMake(1, 1)); //Shoot diagnally Up and to the right
                          //Follow this pattern to create all projectiles desired to be shot in one burst

    }];

   SKAction *delayBetweenShots = [SKAction scaleBy:0 duration:5];
   SKAction* ShootSequence= [SKAction repeatActionForever:[SKAction sequence:@[ShootProjectiles, delayBetweenShots]]];
   [self runAction: ShootSequnce];

这样做可以根据您的需要创建尽可能多的射弹,并按照您定义的矢量方向射击它们。然后它等待5秒(scaleBy:0动作除了延迟之外什么也不做),然后一遍又一遍地重复,直到你删除动作。