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

妖精的绣舞 提交于 2019-12-12 05:26:55

问题


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 ?


回答1:


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


来源:https://stackoverflow.com/questions/22425954/objective-c-random-position-on-screen-not-working-properly-spritekit

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