Search code examples
objective-ccocos2d-iphone

Objective C Cocos2d create references between objects


I am pretty new to objective c and I have a simple question. I am making an iphone app and I call a method regularly to create 3 obstacles.

-(void)createObstacles{
    for(int i = 0;i < 3;i++){
        CGPoint groundWorldPosition = [_physicsNode convertToWorldSpace:self.position];
        CGPoint groundScreenPosition = [self convertToNodeSpace:groundWorldPosition];
        Obstacle *_obstacle = (Obstacle *) [CCBReader load:@"Obstacle"];
        _obstacle.position = ccp(-groundScreenPosition.x + 3 * screenSize.width + arc4random()%100,groundScreenPosition.y + arc4random()%900 + 100);
        [_physicsNode addChild:_obstacle];
    }
}

Every time I create an obstacle I would like to create a warning label at the side of the screen that an obstacle is coming. I have a custom class for the Label. The problem I am having is that I need to update the opacity of the label according to the position of the cooresponding obstacle. So my question is how I can add a warning label for each obstacle and in the update method be able to update each warning labels opacity according to the position of its corresponding obstacle. Thanks for any help


Solution

  • First of all, use an array for your Obstacles so you won't lose reference of it. You can put your custom label inside of Obstacle Class and with that you will know and add them in your scene.

    So you will need something like...

    In a update method, check for obstacle in array, see obstacle position and update the label opacity within the Obstacle.

    Hope it helps :)