Search code examples
xcodearrayscocos2d-iphoneccsprite

cocos2d deducting objects from array


In the following code how could I say have 5 apples in an array, they will fall down one by one with a couple of seconds (or random seconds) between them. Every time an apple falls the array goes 5-1=4 then 4-1=3 etc. and when it reaches 1-1=0 it should stop with dropping apples.

my .h file:

@interface xyz : CCLayer {
        CCArray *appleArray;
}

@property (nonatomic, retain) CCArray *appleArray;

my .m file:

@synthesize appleArray;

-(id) init
    {
        if( (self=[super init])) {

            // Init CCArray
            self.appleArray = [CCArray arrayWithCapacity:5];

            for (int i = 0; i < 5; i++) {
                CCSprite *Apple = [CCSprite spriteWithFile:@"Apple4.png"];
                [self addChild:Apple];
                int positionX = arc4random()%450;
                [Apple setPosition:ccp(positionX, 768)];

                // Add CCSprite into CCArray
                [appleArray addObject:Apple];
            }

            [self scheduleUpdate];
        }
        return self;
    }

    -(void) update: (ccTime) dt
    {
        for (int i = 0; i < 5; i++) {

            // Retrieve 
            CCSprite *Apple = ((CCSprite *)[appleArray objectAtIndex:i]);

            Apple.position = ccp(Apple.position.x, Apple.position.y -300*dt);
            if (Apple.position.y < -100+64)
            {
                int positionX = arc4random()% 450; //not 1000
                [Apple setPosition:ccp(positionX, 768)];
            }
        }
    }

Any help would be appreciated!!


Solution

  • Make sure to include the QuartzCore framework and link to it.

    Add these instance variables in your .h:

    int _lastSpawn;
    double _mediaTime;
    int _mediaTimeInt;
    int _lastIndex;
    BOOL _randomTimeSet;
    int _randomTime;
    

    In your .m init method add the following line:

    _mediaTime = CACurrentMediaTime();
    _lastSpawn = (int)_mediaTime;
    

    Change your update method to this:

    -(void) update: (ccTime) dt
    {
    
        // Get Random Time Interval between 0 and 10 seconds.
        if(!_randomTimeSet) {
            _randomTime = arc4random() % 11;
            _randomTimeSet = YES;
        }
    
        // Set current time
        _mediaTime = CACurrentMediaTime();
        _mediaTimeInt = (int)_mediaTime;
    
        // Check to see if enough time has lapsed to spawn a new Apple.
        if(_mediaTimeInt < (_lastSpawn + _randomTime)) { return; }
    
        // Check if first apple has been added or last apple has been added.
        NSNumber *num = [NSNumber numberWithInt:_lastIndex];
        if(num == nil) {
            _lastIndex = 0;
        } else if(num == [appleArray count]-1) {
            _lastIndex = 0;
        }
    
        CCSprite *Apple = ((CCSprite *)[appleArray objectAtIndex:_lastIndex]);
    
        Apple.position = ccp(Apple.position.x, Apple.position.y -300*dt);
        if (Apple.position.y < -100+64)
        {
            int positionX = arc4random()% 450; //not 1000
            [Apple setPosition:ccp(positionX, 768)];
        }
        _lastIndex += 1;
        _randomTimeSet = NO;
        _mediaTime = CACurrentMediaTime();
        _lastSpawn = (int)_mediaTime;
    
    }