Question

just want it to make nodes fall random from anywhere on the top horizontal edge and fall down the page. without being half chopped off from the vertical edges. it currently shows a white screen

#import "MyScene.h"

@interface MyScene () <SKPhysicsContactDelegate>
@end

@implementation MyScene

-(id)initWithSize:(CGSize)size {    
    if (self = [super initWithSize:size]) {
        /* Setup your scene here */

    self.backgroundColor = [SKColor whiteColor];


    //physics world
    self.physicsWorld.gravity = CGVectorMake(0,-6);
    self.physicsWorld.contactDelegate = self;
}
return self;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */

}



-(void) addBalls {
    //create ball sprite
    SKSpriteNode *balls = [SKSpriteNode spriteNodeWithImageNamed:@"Ball.png"];
    balls.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:(balls.size.width/2)];
    balls.physicsBody.dynamic = YES;

    //determine where to spawn balls
    int minX = balls.size.height/2;
    int maxX = self.frame.size.height - balls.size.height/2;
    int rangeX = maxX - minX;
    int actualX = (arc4random() % rangeX) + minX;

    //place ball slightly off shot, and along a random position on top edge
    balls.position = CGPointMake(self.frame.size.height + balls.size.width/2, actualX);
    [self addChild:balls];

    //speed of balls
    int minDuration = 2.0;
    int maxDuration = 4.0;
    int rangeDuraton = maxDuration - minDuration;
    int actualDuration = (arc4random() & rangeDuraton) + minDuration;

    //create the actions
    SKAction *moveAction = [SKAction moveTo:CGPointMake(-balls.size.height/2, actualX) duration:actualDuration];
    SKAction *actionMovedone = [SKAction removeFromParent];

    [balls runAction:[SKAction sequence:@[moveAction, actionMovedone]]];
}

just want it to make nodes fall random from anywhere on the top horizontal edge and fall down the page. without being half chopped off from the vertical edges. it currently shows a white screen

Was it helpful?

Solution

First of all, you need to exchange the x and y values in the position of the ball nodes.

Secondly, to add multiple balls, you can modify the code as follows:

-(void) addBalls:(int)count
{
    for (int i = 0; i < count; i++)
    {
        //create ball sprite
        SKSpriteNode *balls = [SKSpriteNode spriteNodeWithImageNamed:@"Ball.png"];
        balls.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:(balls.size.width/2)];
        balls.physicsBody.dynamic = YES;

        //determine where to spawn balls
        int minX = balls.size.height/2;
        int maxX = self.frame.size.height - balls.size.height/2;
        int rangeX = maxX - minX;
        int actualX = (arc4random() % rangeX) + minX;

        //place ball slightly off shot, and along a random position on top edge
        balls.position = CGPointMake(actualX, self.frame.size.height + balls.size.width/2);
        [self addChild:balls];

        //speed of balls
        int minDuration = 2.0;
        int maxDuration = 4.0;
        int rangeDuraton = maxDuration - minDuration;
        int actualDuration = (arc4random() & rangeDuraton) + minDuration;

        //create the actions
        SKAction *moveAction = [SKAction moveTo:CGPointMake(-balls.size.height/2, actualX) duration:actualDuration];
        SKAction *actionMovedone = [SKAction removeFromParent];

        [balls runAction:[SKAction sequence:@[moveAction, actionMovedone]]];
    }
}

Then, simply pass the number of balls needed in the method.

Eg.

[self addBalls:10]; //Will add 10 balls to the scene
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top