title: Spawn a node at random coordinates in SpriteKit
body: <p>I'm trying to spawn a node I've created at random coordinates. So far, I've made the attempt by doing the following:</p>
  
  <pre><code>- (SKShapeNode *)createTargetNode
  {
  
    int maxXCoord = CGRectGetMaxX(self.frame);
    int maxYCoord = CGRectGetMaxY(self.frame);
  
    int x = arc4random()% maxXCoord + 40;
    int y = arc4random()% maxYCoord + 40;
  
    SKShapeNode *target = [SKShapeNode new];
    CGMutablePathRef circle = CGPathCreateMutable();
    CGPathAddArc(circle, NULL, x, y, 5, 0, M_PI*2, YES);
    target.path = circle;
    target.fillColor = [SKColor redColor];
    target.strokeColor = [SKColor redColor];
    NSLog(@"%d, %d", x, y);
    return target;
  }
  </code></pre>
  
  <p>Sometimes the node appears on the screen, sometimes it doesn't. I guess it's going out of bounds. How can I fix that? The orientation of the phone is landscape, so do the coordinates change?</p>
  
  <p>Thanks!</p>
  
  <p><strong>Edit 1:</strong></p>
  
  <p>Here are some coordinates that have been plotted:</p>
  
  <pre><code>2014-02-14 17:54:09.629 reactions[16096:70b] 221, 402
  2014-02-14 17:54:10.990 reactions[16096:70b] 273, 542
  2014-02-14 17:54:11.586 reactions[16096:70b] 88, 299
  2014-02-14 17:54:14.660 reactions[16096:70b] 69, 306
  </code></pre>
  
  <p>Only <code>(88, 299)</code> and <code>(69, 306)</code> are actually plotted. 
  Here's the link to the image (I can't post it yet, I don't have enough reputation):</p>
  
  <p><img src="https://i.stack.imgur.com/UEVx1.png" alt=""></p>
  
  <p><strong>Edit 2:</strong></p>
  
  <p>Here's my main view controller. Pretty standard, just loading the <code>SKScene</code>.</p>
  
  <pre><code>#import "DKViewController.h"
  #import "DKPlayScene.h"
  
  @implementation DKViewController
  
  - (void)viewDidLoad
  {
      [super viewDidLoad];
  
      SKView * skView = (SKView *)self.view;
      skView.showsFPS = YES;
      skView.showsNodeCount = YES;
  
      SKScene * scene = [DKPlayScene sceneWithSize:skView.bounds.size];
      scene.scaleMode = SKSceneScaleModeAspectFill;
  
      [skView presentScene:scene];
  }
  
  - (BOOL)shouldAutorotate
  {
      return YES;
  }
  
  - (NSUInteger)supportedInterfaceOrientations
  {
      if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
          return UIInterfaceOrientationMaskAllButUpsideDown;
      } else {
          return UIInterfaceOrientationMaskAll;
      }
  }
  
  - (void)didReceiveMemoryWarning
  {
      [super didReceiveMemoryWarning];
  }
  
  @end
  </code></pre>
  
  <p>And here's the implementation file for the SKScene:</p>
  
  <pre><code>#import "DKPlayScene.h"
  #include &lt;stdlib.h&gt;
  
  
  @interface DKPlayScene ()
  
  @property (nonatomic, strong) SKLabelNode *scoreLabel;
  @property bool *gameHasStarted;
  @property int  *score;
  
  @end
  
  @implementation DKPlayScene
  
  - (id)initWithSize:(CGSize)size
  {
    if (self = [super initWithSize:size]) {
  
      self.gameHasStarted = NO;
      self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];
      self.scoreLabel = [SKLabelNode new];
  
      self.scoreLabel.text = @"High Score:";
      self.scoreLabel.fontSize = 10;
      self.scoreLabel.position = (CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame) + 70));
      [self createTargetNode];
      [self addChild:self.scoreLabel];
    }
    return self;
  }
  
  - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  {
    [self addChild:[self createTargetNode]];
  }
  
  -(void)update:(CFTimeInterval)currentTime {
  
  }
  
  #pragma mark - Private
  
  - (SKShapeNode *)createTargetNode
  {
  
    int maxXCoord = self.frame.size.width;
    int maxYCoord = self.frame.size.height;
  
    int circleWidth = 6;
  
    int x = arc4random() % (maxXCoord - (circleWidth / 2));
    int y = arc4random() % (maxYCoord - (circleWidth / 2));
  
    SKShapeNode *target = [SKShapeNode new];
    CGMutablePathRef circle = CGPathCreateMutable();
    CGPathAddArc(circle, NULL, x, y, circleWidth, 0, M_PI*2, YES);
    target.path = circle;
    target.fillColor = [SKColor redColor];
    target.strokeColor = [SKColor redColor];
    NSLog(@"%d, %d", x, y);
    return target;
  }
  </code></pre>