Pergunta

I am making a 14 year old kid making a game in cocos2d. I am pretty new to cocos2d. I want to display the same coin sprite next to each other to make a pattern.

So I added this in my main gameplay layer:

- (void)coinPatterns {    
    menu = [CCMenu menuWithItems:[Coins class], [Coins class], self, nil];
    [menu alignItemsHorizontally];
    [self addChild:menu];
}

And this is how I am initializing menu:

[[GameMechanics sharedGameMechanics] setSpawnRate:50 forMonsterType:menu];

This is what is in my coins class:

- (id)initWithMonsterPicture
{
    self = [super initWithFile:@"coin.png"];
    if (self)
    {
        CGRect screenRect = [[CCDirector sharedDirector] screenRect];
        CGSize spriteSize = [self contentSize];
        posX =  screenRect.size.width + spriteSize.width * 0.5f;
        posY = 150;
        self.initialHitPoints = 1;
        self.animationFrames = [NSMutableArray array];
        [self scheduleUpdate];
        inAppCurrencyDisplayNode.score = [Store availableAmountInAppCurrency];
    }
    coinValue = 3;
    return self;
}
- (void)spawn
{
    self.position = CGPointMake(posX, posY);
    self.visible = YES;
}
- (void)gotCollected {
    self.visible = FALSE;
    self.position = ccp(-MAX_INT, 0);
    [Store addInAppCurrency:coinValue];
}

I keep getting a Incompatible pointer types sending 'class' to parameter of type 'CCMenuItem'. Can someone please tell me how I should change the code so that this works?

Thanks!

Foi útil?

Solução

menuWithItems: takes an array of CCMenuItem objects, you are sending a class itself. I don't know what the class Coin does, but if the purpose is to show an image and then do something when it's tapped I suggest you to do this:

CCMenuItem *myCoin1 = [CCMenuItemImage 
  itemFromNormalImage:@"coin.png" selectedImage:@"coinSelected.png" 
  target:self selector:@selector(coin1WasTapped:)];
CCMenuItem *myCoin2 ...
menu = [CCMenu menuWithItems: myCoin1, myCoin2, myCoin3, ..., nil];

You should create a method coin1WasTapped: that will be called when the coin was tapped, you can "collect" the coins here. Maybe remove them from the menu or an animation.

If you are going to create many coins I suggest you to use a for loop to create them all in an array. This way it will be easier to manipulate later.

This tutorial is really good, it can help you to understand better what you need to do and how to do it.

Good luck!

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top