随机x位置问题
random x position issue
我有一个每隔几秒产卵的圆圈,我有五个车道,我希望它们在其中产卵,但我希望它随机进入不同的车道。我使用的代码工作正常,但它不允许我使用我的五个 x 位置?我尝试了多种解决方案,但它们都行不通。我仍然是 objective c 的新手,因此我们将不胜感激。 (另外,下面的代码显然不是我的全部方法,但它是我需要展示的唯一部分。)
#import "GameScene.h"
int xPos1;
int xPos2;
int xPos3;
int xPos4;
int xPos5;
@implementation GameScene
-(void) addBall:(CGSize) size{
xPos1 = 8.5;
xPos2 = 74;
xPos3 = 138;
xPos4 = 203;
xPos5 = 267.5;
CGRect box = CGRectMake( arc4random() , self.frame.size.height, //pos
45, 45); //size
UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:box];
SKShapeNode *circle = [SKShapeNode node];
circle.path = circlePath.CGPath;
circle.fillColor = [SKColor colorWithRed:(243.0f/255) green:(134.0f/255) blue:(48.0f/255) alpha:1.0];
circle.strokeColor = nil;
circle.lineWidth = 3;
//add physics
circle.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:circle.frame.size.width/2];
//appy actions to move sprite
SKAction *wait = [SKAction waitForDuration:2];
SKAction *move = [SKAction moveToY:-self.size.height - 50 duration:2];
SKAction *remove = [SKAction removeFromParent];
SKAction *sequence = [SKAction sequence:@[wait, move, remove]];
SKAction *repeat = [SKAction repeatActionForever:sequence];
SKAction *spawn = [SKAction performSelector:@selector(addBall:) onTarget:self];
SKAction *delay = [SKAction waitForDuration:2.5];
SKAction *spawnThenDelay = [SKAction sequence:@[delay, spawn]];
[self runAction:spawnThenDelay];
[circle runAction:repeat];
[self addChild:circle];
}
以下是我尝试使用 arc4random 中的 x 位置的不同方法:
CGRect box = CGRectMake( arc4random() %(xPos1, xPos2, xPos3, xPos4, xPos5) , self.frame.size.height, //pos
45, 45);
CGRect box = CGRectMake( arc4random(xPos1, xPos2, xPos3, xPos4, xPos5) , self.frame.size.height, //pos
45, 45);
我也试过直接把整数放在那里...
尝试创建一个 X 位置数组并使用随机索引从中进行选择。
CGFloat xPositions[5] = {8.5,74,138,203,267.5};
int randomIndex = arc4random() % 5;
int randomXPosition = xPositions[randomIndex]
CGRect box = CGRectMake(randomXPosition, self.frame.size.height, 45, 45);
UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:box];
我有一个每隔几秒产卵的圆圈,我有五个车道,我希望它们在其中产卵,但我希望它随机进入不同的车道。我使用的代码工作正常,但它不允许我使用我的五个 x 位置?我尝试了多种解决方案,但它们都行不通。我仍然是 objective c 的新手,因此我们将不胜感激。 (另外,下面的代码显然不是我的全部方法,但它是我需要展示的唯一部分。)
#import "GameScene.h"
int xPos1;
int xPos2;
int xPos3;
int xPos4;
int xPos5;
@implementation GameScene
-(void) addBall:(CGSize) size{
xPos1 = 8.5;
xPos2 = 74;
xPos3 = 138;
xPos4 = 203;
xPos5 = 267.5;
CGRect box = CGRectMake( arc4random() , self.frame.size.height, //pos
45, 45); //size
UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:box];
SKShapeNode *circle = [SKShapeNode node];
circle.path = circlePath.CGPath;
circle.fillColor = [SKColor colorWithRed:(243.0f/255) green:(134.0f/255) blue:(48.0f/255) alpha:1.0];
circle.strokeColor = nil;
circle.lineWidth = 3;
//add physics
circle.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:circle.frame.size.width/2];
//appy actions to move sprite
SKAction *wait = [SKAction waitForDuration:2];
SKAction *move = [SKAction moveToY:-self.size.height - 50 duration:2];
SKAction *remove = [SKAction removeFromParent];
SKAction *sequence = [SKAction sequence:@[wait, move, remove]];
SKAction *repeat = [SKAction repeatActionForever:sequence];
SKAction *spawn = [SKAction performSelector:@selector(addBall:) onTarget:self];
SKAction *delay = [SKAction waitForDuration:2.5];
SKAction *spawnThenDelay = [SKAction sequence:@[delay, spawn]];
[self runAction:spawnThenDelay];
[circle runAction:repeat];
[self addChild:circle];
}
以下是我尝试使用 arc4random 中的 x 位置的不同方法:
CGRect box = CGRectMake( arc4random() %(xPos1, xPos2, xPos3, xPos4, xPos5) , self.frame.size.height, //pos
45, 45);
CGRect box = CGRectMake( arc4random(xPos1, xPos2, xPos3, xPos4, xPos5) , self.frame.size.height, //pos
45, 45);
我也试过直接把整数放在那里...
尝试创建一个 X 位置数组并使用随机索引从中进行选择。
CGFloat xPositions[5] = {8.5,74,138,203,267.5};
int randomIndex = arc4random() % 5;
int randomXPosition = xPositions[randomIndex]
CGRect box = CGRectMake(randomXPosition, self.frame.size.height, 45, 45);
UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:box];