Narc0t1CYM
Narc0t1CYM

Reputation: 622

Objective-C random position on screen not working properly ( SpriteKit )

Well I made a randomNumber Class, (for practice and) for calculating a random position on my screen for an object and it works properly, except for it sometimes puts the sprite out of the screen.The x and y coordinates are smaller the screen height and width. but it doesn't show it on the screen.
The whole program is just basically randomly placing instances of an object inside the screen.

randomNumber.h

#import <Foundation/Foundation.h>
#import <SpriteKit/SpriteKit.h>

@interface randomNumber : NSObject

-(int)randNumX:(int) max :(SKSpriteNode *) sprite;
-(int)randNumY:(int) max :(SKSpriteNode *) sprite;

@end

randomNumber.m

#import "randomNumber.h"

@implementation randomNumber

-(int)randNumX:(int) max :(SKSpriteNode *) sprite {
    int _spriteW = sprite.frame.size.width;

    int _random = (arc4random() % (max - _spriteW));
    NSLog(@"The x value is %d", _random);

    return _random;
}

-(int)randNumY:(int) max :(SKSpriteNode *) sprite {
    int _spriteH = sprite.frame.size.height;

    int _random = (arc4random() % (max - _spriteH));
    NSLog(@"The y value is %d", _random);

    return _random;
}

@end

MyScene.m ( only the initilazeMole method )

-(void) initilazeMole {
    int x = [self.rndNum randNumX:(self.scene.size.width):(self.mole)];
    int y = [self.rndNum randNumY:(self.scene.size.height):(self.mole)]

    self.mole = [SKSpriteNode spriteNodeWithImageNamed:@"spaceship"];
    self.mole.anchorPoint = CGPointMake(0,0);
    self.mole.position = CGPointMake(x,y);

    SKAction *pulseRed = [SKAction sequence:@[
                                              [SKAction colorizeWithColor:[SKColor redColor] colorBlendFactor:1.0 duration:0.5],
                                              [SKAction waitForDuration:0.1],
                                              [SKAction colorizeWithColorBlendFactor:0.0 duration:1.0]]];
    [self.mole runAction: pulseRed];

    NSLog(@"mole x position: %f", self.mole.position.x);
    NSLog(@"mole y position: %f", self.mole.position.y);



    [self addChild:self.mole];
}

I don't really understand why does it place it off the screen, hence I generate a random number that can maximally be ( the screen width - sprite width ) and ( the screen height - sprite height )
My project settings are set up for an iphone 3.5 inch in landscape mode. Any idea where did my code go wrong ?

Upvotes: 0

Views: 899

Answers (1)

Logan
Logan

Reputation: 53112

Try this:

- (CGPoint) randomPointWithinContainerSize:(CGSize)containerSize forViewSize:(CGSize)size {
    NSLog(@"move");
    CGFloat xRange = containerSize.width - size.width;
    CGFloat yRange = containerSize.height - size.height;

    CGFloat minX = (containerSize.width - xRange) / 2;
    CGFloat minY = (containerSize.height - yRange) / 2;

    int randomX = (arc4random() % (int)floorf(xRange)) + minX;
    int randomY = (arc4random() % (int)floorf(yRange)) + minY;
    return CGPointMake(randomX, randomY);
}

Then replace:

int x = [self.rndNum randNumX:(self.scene.size.width):(self.mole)];
int y = [self.rndNum randNumY:(self.scene.size.height):(self.mole)]

self.mole = [SKSpriteNode spriteNodeWithImageNamed:@"spaceship"];
self.mole.anchorPoint = CGPointMake(0,0);
self.mole.position = CGPointMake(x,y);

With:

self.mole.position = [self randomPointWithinContainerSize:self.scene.size forViewSize:self.mole.bounds.size];

Upvotes: 2

Related Questions