2015-01-28 4 views
0

나는 몇 초마다 원을 그리며 나는 그들이 산란하기를 원하지만 나는 무작위로 다른 곳으로 가고 싶다. 내가 사용하는 코드는 제대로 작동하지만 5 x 위치를 사용할 수 없습니다. 여러 솔루션을 시도했지만 작동하지 않습니다. 나는 아직도 객관적인 C에 새로운 것이므로 어떤 도움을 주시면 감사하겠습니다. (또한 아래의 코드는 분명 내 모든 방법은 아닙니다하지만 내가 보여줄 필요가있는 유일한 부분입니다.) 여기 임의 x 위치 문제

#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); 

I했습니다 또한 단지 X 포지션의 배열을 생성하고 선택하여 ... 거기에 직접

+0

안녕하십니까. [FAQ]와 [Ask]를 읽어주십시오. 작성한 내용을 토대로 우리는 당신이 달성하고자하는 것을 실제로 말할 수 없으며 코드가 목표에 어떻게 적용되는지 말할 수 없습니다. –

+0

CGRectMake는 내 볼의 위치입니다. 나는 몇 초마다 공을 낳고 화면 아래로 내려 간다. 그것은 잘 생기지 만 5 x 위치 중 하나에서 산란하기를 원합니다. 따라서 xPos1-xPos5. 코드를 업데이트하여이 스프라이트의 전체 메서드를 보여줍니다. @ JimGarrison –

+0

코드에서 xpos1 - 5를 어디에서든지 사용하려고합니다. 당신은 단지 그들의 가치를 설정하고, 그 다음에는 그것뿐입니다. – hamobi

답변

2

시도를 정수를 퍼팅 시도 임의의 인덱스를 사용하여 해당.

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

완벽하게 작동합니다! 고맙습니다! –