Sprite Kit - 使用 Switch Case 将随机 Sprite 节点添加到场景中

Posted

技术标签:

【中文标题】Sprite Kit - 使用 Switch Case 将随机 Sprite 节点添加到场景中【英文标题】:Sprite Kit - Add random Sprite Nodes to the scene with Switch Case 【发布时间】:2014-02-14 13:02:27 【问题描述】:

我有以下代码来创建随机对象并将它们添加到场景中。最后,在随机延迟后重复该方法。

-(void)createObjects 

// Create random start point
float randomStartPoint = arc4random_uniform(4) * 64 + 32;
CGPoint startPoint = CGPointMake(self.size.width + 50, randomStartPoint);

// Create random object and add to scene
switch (arc4random_uniform(2)) 
    case 0:
    
        SKSpriteNode *crate = [SKSpriteNode spriteNodeWithImageNamed: @"crate"];

        crate.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:crate.frame.size];
        crate.physicsBody.dynamic = YES;
        crate.physicsBody.affectedByGravity = NO;
        crate.physicsBody.categoryBitMask = objectCategory;
        crate.physicsBody.collisionBitMask = 0;
        crate.physicsBody.contactTestBitMask = playerCategory;

        crate.position = startPoint;
        crate.name = @"object";
        crate.zPosition = 20;

        [self addChild:crate];
        break;
    

    case 1:
    
        SKSpriteNode *cactus = [SKSpriteNode spriteNodeWithImageNamed: @"cactus"];

        cactus.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:cactus.frame.size];
        cactus.physicsBody.dynamic = YES;
        cactus.physicsBody.affectedByGravity = NO;
        cactus.physicsBody.categoryBitMask = objectCategory;
        cactus.physicsBody.collisionBitMask = 0;
        cactus.physicsBody.contactTestBitMask = playerCategory;

        cactus.position = startPoint;
        cactus.name = @"object";
        cactus.zPosition = 20;

        [self addChild:cactus];
        break;
    

    default:
        break;


// After adding child, call same method at random delay
float randomNum = arc4random_uniform(4) * 0.4 + 0.25;
[self performSelector:@selector(createObjects) withObject:nil afterDelay:randomNum];

实际上我有更多的案例,并且重复了很多相同的代码,因为每个 Sprite 节点都有相同的设置。有没有办法一次创建一个精灵节点,然后在每种情况下为节点设置不同的图像,然后将其添加到场景中?

【问题讨论】:

【参考方案1】:

您可以在 switch case 中创建 SKTexture,然后简单地使用该纹理创建节点。

像这样:

-(void)createObjects 

    // Create random start point
    float randomStartPoint = arc4random_uniform(4) * 64 + 32;
    CGPoint startPoint = CGPointMake(self.size.width + 50, randomStartPoint);

    // Create random object and add to scene
    SKTexture* objectTexture;
    switch (arc4random_uniform(2)) 
        case 0:
            objectTexture = [SKTexture textureWithImageNamed:@"crate"];
            break;
        case 1:
            objectTexture = [SKTexture textureWithImageNamed:@"cactus"];
            break;
        default:
            break;
    
    SKSpriteNode *object = [SKSpriteNode spriteNodeWithTexture:objectTexture];

    object.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:crate.frame.size];
    object.physicsBody.dynamic = YES;
    object.physicsBody.affectedByGravity = NO;
    object.physicsBody.categoryBitMask = objectCategory;
    object.physicsBody.collisionBitMask = 0;
    object.physicsBody.contactTestBitMask = playerCategory;

    object.position = startPoint;
    object.name = @"object";
    object.zPosition = 20;

    [self addChild: object];

另一种方法是在 switch case 之前创建对象,然后简单地创建纹理并使用 setTexture 在 switch case 中设置它:

【讨论】:

效果很好!非常感谢! 嗨,当我快速关注这个时,它崩溃说致命错误 - 在展开可选值时意外发现 nil @user2255273 您从哪个方法调用 [self createObjects]?

以上是关于Sprite Kit - 使用 Switch Case 将随机 Sprite 节点添加到场景中的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Sprite-kit 中画一条线

如何在Sprite-kit中画一条线

Sprite-kit 主要角色选择器

Sprite-Kit 为单个接触注册多个碰撞

使用 Swift 在 sprite kit 中的多个场景中重用相同的 sprite 节点

Sprite Kit,删除 Sprite 以进行碰撞