1
votes

I used the following code to choose the position the sprite spawns at:

BOOL heads = arc4random_uniform(100) < 50;
lightnut.position = (heads)? CGPointMake(257,600) : CGPointMake(50,600);

What I'm trying to do is use heads or tails to determine which sprite is spawned, either lightnut.png or my other sprite, darknut.png. They both are exactly the same, but just different colors. Here is the rest of my code:

        SKAction *createSpriteBlock = [SKAction runBlock:^{
        SKSpriteNode *lightnut = [SKSpriteNode spriteNodeWithImageNamed:@"lightnut.png"];
        BOOL heads = arc4random_uniform(100) < 50;
        lightnut.position = (heads)? CGPointMake(257,600) : CGPointMake(50,600);

        lightnut.physicsBody = [SKPhysicsBody   bodyWithRectangleOfSize:CGSizeMake(200,160)];

        lightnut.physicsBody.categoryBitMask = nutHitCategory;
        lightnut.physicsBody.contactTestBitMask = squirrelHitCategory;
        lightnut.physicsBody.collisionBitMask =  squirrelHitCategory;

        lightnut.physicsBody.affectedByGravity = NO;

        self.physicsWorld.contactDelegate = self;

        [self addChild: lightnut];

        SKAction *moveNodeUp = [SKAction moveByX:0.0 y:-700.0 duration:1.3];
        [lightnut runAction: moveNodeUp];

So how should I have it randomly choose whether it will spawn the lightnut.png or darknut.png? Thank you for any help!

1

1 Answers

2
votes

It would seem the answer is in the first line of code

BOOL heads = arc4random_uniform(100) < 50;

Why not use that in your block logic? Example:

    SKAction *createSpriteBlock = [SKAction runBlock:^{
    const BOOL isHeads   = arc4random_uniform(100) < 50;
    NSString* spriteName = isHeads ? @"lightnut.png" : @"darknut.png";
    SKSpriteNode *nut    = [SKSpriteNode spriteNodeWithImageNamed:spriteName];