Sprite Kit - Add random Sprite Nodes to the scene with Switch Case

风流意气都作罢 提交于 2019-12-05 10:00:40

问题


I have the following code to create random objects and add them to the scene. At the end the method is repeated after a random delay.

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

In reality I have many more cases and a lot of the same code is repeated, since every Sprite Node has the same settings. Is there a way to create a sprite node once, and in every case set a different image for the node, and add it to the scene?


回答1:


You can create SKTexture in switch case and then simply create node with that texture.

Like this:

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

Another way would be to create object before switch case and then simply create texture and set it in switch case using setTexture:



来源:https://stackoverflow.com/questions/21780165/sprite-kit-add-random-sprite-nodes-to-the-scene-with-switch-case

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!