0

Please help me as am fed with the searches and new to cocos 2d. Only am getting is the last sprite got moving if i schedule addRed and i want all the sprites moving randomly in the screen. Any help will be Appreciated and thanks in advance.

-(void)addRed 
{
redSprite = [CCSprite spriteWithImageNamed:@"Red.png"];;
CGSize screenSize = [[CCDirector sharedDirector] viewSize];
[redSprite setPosition:CGPointMake(screenSize.width/2, screenSize.height/2)];
[self resizeSprite:redSprite toWidth:80 toHeight:80];
[self addChild:redSprite z:10];

[self gameStart];
}

- (void)gameStart {
    // Create the actions
CGSize result = [[UIScreen mainScreen] bounds].size;
CGPoint nextPoint;

if (redSprite.position.x == result.width-40.0) {
    nextPoint.x = redSprite.position.x - kAccelXAxis;
    backx = YES;
}
else {
    if (redSprite.position.x == 40.0) {
        nextPoint.x = redSprite.position.x + kAccelXAxis;
        backx = NO;

    }
    else {
        if (backx) {

            nextPoint.x = redSprite.position.x - kAccelXAxis;
        }
        else
        {
            nextPoint.x = redSprite.position.x + kAccelXAxis;
        }
    }
}

if (redSprite.position.y == 40.0) {
    nextPoint.y = redSprite.position.y + kAccelYAxis;
    backy = YES;

}
else {
    if (redSprite.position.y == result.height-40.0) {
        nextPoint.y = redSprite.position.y - kAccelYAxis;
        backy = NO;

    }
    else {
        if (backy) {
            nextPoint.y = redSprite.position.y + kAccelYAxis;
        }
        else
        {
            nextPoint.y = redSprite.position.y - kAccelYAxis;
        }
    }
}

    CCAction *myAction = [CCActionSequence actions:[CCActionMoveTo actionWithDuration:0.01 position:nextPoint], [CCActionCallFunc actionWithTarget:self selector:@selector(gameStart)], [CCActionCallFunc actionWithTarget:self selector:@selector(updateCol:)],nil];
[myAction setTag:10];

[redSprite runAction:myAction];

}
Rojonrocks
  • 11
  • 1
  • This is not about cocos2d but about programming. Get some basic understanding about OOP and objective-c, and you will feel much better. Your redSprite variable used in addRed contains a reference to the last CCSprite created. period. that's it. It works as specified. – YvesLeBorg Aug 11 '14 at 19:32

1 Answers1

0

Try creating a updater with an interval of 3 sec for creating your sprites. For your created sprites, use a CCRepeatForever which will generate your next positions for your sprite.

For your case, I also thought about using a custom CCSprite to store your next position to move.

You will need something like...

   [self schedule:@selector(createSprite) interval:(3)]; //to schedule your sprite generator

// Your sprite generator with action
-(void)createSprite{
    CCCustomRed *red = [CCCustomRed spriteWithFile:@"red.png"];
    [self addChild:red];
    CCCallFuncN *changePos = [CCCallFuncN actionWithTarget:self selector:@selector(setRandomPos:)];
    CCMoveTo *move = [CCMoveTo actionWithDuration:1 position:red.nextPosition];
    CCDelayTime *delay = [CCDelayTime actionWithDuration:1.0];
    [red runAction:[CCRepeatForever actionWithAction:[CCSequence actions:move,changePos,delay,nil]]];
}

//Generate a random pos as your sprite next move
-(void)setRandomPos:(id)sender{
    CCCustomRed *red = (CCCustomRed *) sender;
    CGSize screenSize = [[CCDirector sharedDirector] winSize];
    red.nextPosition = ccp(arc4random()%screenSize.width,arc4random()%screenSize.height);
}

Hope it helps :)

Kalvin
  • 71
  • 4