Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

So basically i want a sprite to start from a random position and head to a random direction then take a random turn halfway through using CGPathAddCurveToPoint.

The part i fail in is how to make it take a random direction. here is what i have so far:

{
SKSpriteNode *halo = [SKSpriteNode spriteNodeWithImageNamed:@"Halo"];
halo.position = CGPointZero;

CGMutablePathRef path = CGPathCreateMutable();
CGMutablePathRef path2 = CGPathCreateMutable();


int minX = halo.size.width / 2 ;
int maxX = self .frame.size.width - halo.size.width / 2;
int rangeX = maxX - minX;
int actualX = (arc4random() % rangeX) + minX;

halo.position = CGPointMake(actualX , -halo.size.height / 2);
[self addChild:halo];

CGPathMoveToPoint(path, nil, actualX, -halo.size.height / 2);

CGPathAddCurveToPoint(path, nil, 120, 300, 170, 650, 300, -10);

CGPathMoveToPoint(path2, nil, actualX, -halo.size.height / 2);
CGPathAddCurveToPoint(path2, nil, 120, 300, 170, 650, 300, -10);



int randomDirection = arc4random_uniform(3);
switch (randomDirection) {
    case 0:
         CGPathAddCurveToPoint(path, nil, 120, 300, 170, 650, 300, -10);
        break;
    case 1:
         CGPathAddCurveToPoint(path, nil, 220, 150, 170, 300, 100, -10);
        break;
    case 2:
         CGPathAddCurveToPoint(path, nil, 400, 650, 40, 450, 300, -10);
        break;
}


SKAction *follow = [SKAction followPath:path asOffset:NO orientToPath:YES duration:2];
SKAction *remove = [SKAction removeFromParent];
[halo runAction:[SKAction sequence:@[follow, remove]]];


}

Thee switch case part doesn't work obviously because there is no place to place to put the result in. I can make several methods each with set of directions then make final method to choose randomly from them, but that's sound like wast of time and would result in ugly code, sure there is someway i can do this in a single method, right ?

I'm beginner in programming by the way.

share|improve this question
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.