azi_santos
azi_santos

Reputation: 105

array of objects in cocos2d with touching functionality

I want to create a simple game where fruits will fall from the top and the user collects them by clicking. My problem is for example the apple that falls disappears when it is clicked on, so there is only 1 apple to be collected so far. I want to make some kind of an array to store like 5 apples and they should fall at random speed and random positions. And then when the last one is collected it should disappear.

This is what I am trying now and it doesnt seem to work: This is where the CCSprite Apple is created:

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

and this for the movement from top to bottom:

-(void) callEveryFrame:(ccTime)dt
{
    Apple.position = ccp(Apple.position.x, Apple.position.y -300*dt);
    if (Apple.position.y < -100+64)
    {
        int positionX = arc4random()%1004;
        [Apple setPosition:ccp(positionX, 768)];
    }
}

Any help would be appreciated! Thanks

Upvotes: 1

Views: 504

Answers (1)

Sueiras
Sueiras

Reputation: 91

Note: Edited solution adding random position and speed. Note 2: New edition adding touches detection.

With a single variable to store five CCSprites?. Finally you will only have the last CCSprite in that variable.

You need to store CCSprite(s) in a CCArray (or NSMutableArray)

Try this in your header file .h:

@interface xyz : CCLayer {
        CCArray *appleArray;
}

@property (nonatomic, retain) CCArray *appleArray;

In your implementation file .m:

@synthesize appleArray;

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

        self.touchEnabled = YES;

        // Inicialize 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;
            int positionY = 768 + arc4random()%1000;
            // Store speed 
            float speed = 150 + arc4random()%400;
            Apple.tag = speed;
            [Apple setPosition:ccp(positionX, positionY)];

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

        [self scheduleUpdate];
    }
    return self;
}

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

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

        if (Apple.position.y > -250) {
            Apple.position = ccp(Apple.position.x, Apple.position.y - (Apple.tag*dt));
        }
    }
}

- (void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:[touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];

    for (CCSprite *Apple in self.appleArray)
    {
        if (CGRectContainsPoint(Apple.boundingBox, location))
        {
            Apple.visible = NO;
        }
    }
}

Upvotes: 1

Related Questions