John paul
John paul

Reputation: 69

Spawning sprites randomly on x-axis

Hi I need to spawn balloons which are my sprites along the bottom of the x axis out of the screen randomly. I used this code from Ray Wenderliches website but when i click the button that starts the spawning they don't spawn?

 @implementation MyScene2



 - (void)addMonster {

// Create sprite
SKSpriteNode * monster = [SKSpriteNode spriteNodeWithImageNamed:@"balloon11.png"];

// Determine where to spawn the monster along the X axis
int minX = monster.size.height / 2;
int maxX = self.frame.size.height - monster.size.height / 2;
int rangeX = maxX - minX;
int actualX = (arc4random() % rangeX) + minX;

// Create the monster slightly off-screen along the right edge,
// and along a random position along the X axis as calculated above
monster.position = CGPointMake(self.frame.size.width + monster.size.width/2, actualX);
[self addChild:monster];

// Determine speed of the monster
int minDuration = 2.0;
int maxDuration = 4.0;
int rangeDuration = maxDuration - minDuration;
int actualDuration = (arc4random() % rangeDuration) + minDuration;

   // Create the actions
SKAction * actionMove = [SKAction moveTo:CGPointMake(-monster.size.width/2, actualX)   
 duration:actualDuration];
SKAction * actionMoveDone = [SKAction removeFromParent];
[monster runAction:[SKAction sequence:@[actionMove, actionMoveDone]]];

}


  - (void)updateWithTimeSinceLastUpdate:(CFTimeInterval)timeSinceLast {

  self.lastSpawnTimeInterval += timeSinceLast;
  if (self.lastSpawnTimeInterval > 1) {
    self.lastSpawnTimeInterval = 0;
     [self addMonster];
   }
 }



  - (void)update:(NSTimeInterval)currentTime {
 // Handle time delta.
 // If we drop below 60fps, we still want everything to move the same distance.
 CFTimeInterval timeSinceLast = currentTime - self.lastUpdateTimeInterval;
 self.lastUpdateTimeInterval = currentTime;
 if (timeSinceLast > 1) { // more than a second since last update
    timeSinceLast = 1.0 / 60.0;
    self.lastUpdateTimeInterval = currentTime;
    }

  [self updateWithTimeSinceLastUpdate:timeSinceLast];

 }

Upvotes: 1

Views: 438

Answers (1)

WolfLink
WolfLink

Reputation: 3317

Your main problem lies in this lines of code:

monster.position = CGPointMake(self.frame.size.width + monster.size.width/2 , actualX);

That method works like this: CGPointMake( x_coord , y_coord );.

Right now, you are setting the x value to width of the frame plus the width of the sprite, so this will cause the sprite to be spawned off the screen. You are giving the y coordinate the randomized value. Instead, use

CGPointMake(actualX , self.frame.size.height);

Now there is another problem with this code. These coordinates are based on the view, but monster.position is based on the SKScene. The SKScene origin usually does not match up with the origin for the view. So you have to do it like this:

monster.position = [CGPointMake(actualX , self.frame.size.height); locationInNode: self];

Also, I noticed that you are using frame.size.width for intended Y axis coordinates and frame.size.height for intended X axis coordinates. Width is for x and height is for y, so make sure you are using those properly.

Upvotes: 1

Related Questions