简体   繁体   English

在X轴上随机生成精灵

[英]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. 嗨,我需要沿着屏幕的x轴底部随机生成气球,这些气球是我的精灵。 I used this code from Ray Wenderliches website but when i click the button that starts the spawning they don't spawn? 我从Ray Wenderliches网站使用了此代码,但是当我单击开始生成的按钮时,它们不会生成吗?

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

 }

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 ); 该方法的工作方式如下: 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. 现在,您正在将x值设置为框架的宽度加上精灵的宽度,因此这将导致精灵从屏幕上生成。 You are giving the y coordinate the randomized value. 您正在给y坐标随机值。 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 . 这些坐标基于视图,但是monster.position基于SKScene The SKScene origin usually does not match up with the origin for the view. 通常, SKScene原点与视图的原点不匹配。 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. 另外,我注意到您正在将frame.size.width用于预期的Y轴坐标,并将frame.size.height用于预期的X轴坐标。 Width is for x and height is for y, so make sure you are using those properly. 宽度用于x,高度用于y,因此请确保正确使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM