Ospho
Ospho

Reputation: 2796

CCSprite not deleting

I have a class called Projectiles which inherits from CCSprite class, Currently there are 2 types of projectiles, rain1 and rain2. I have a method that creates a bunch of these sprites every 2 seconds to give the illusion of pulsating rain. Each one of these rain sprites is added to the array, _projectiles and it is effected by gravity.

In fact, its working just about perfectly, except for the memory management and soon after this rain loop keeps creating sprites I get massive frame rate drops.

Ideally, if the rain (under the constant of gravity) drops below the height of the screen, I want the rain sprite to be deleted. Deleted from the _projectiles array, deleted from the view completely!

My code isn't doing this! Please I need some assistance...

Here is a snippet:

for (Projectile *rain1 in _projectiles){
    if (rain1.position.y < -winSize.height) {
        rain1 = nil;
        [_projectiles removeObject: rain1];
        [self removeChild:rain1 cleanup:YES];
        [rain1 release];

    }
}

for (Projectile *rain2 in _projectiles){
    if (rain2.position.y < -winSize.height) {
        rain2 = nil;
        [_projectiles removeObject: rain2];
        [self removeChild:rain2 cleanup:YES];
        [rain2 release];
    }
}

Upvotes: 0

Views: 1079

Answers (1)

Lim Gim Hong
Lim Gim Hong

Reputation: 306

remove the rain1 = nil, that should work. you change the rain1 pointer to nil, thats why when you call [self removechild] it cannot find the rain1 sprite to remove.

Upvotes: 1

Related Questions