【问题标题】:Incompatible pointer types sending 'class' to parameter of type CCMenuItem不兼容的指针类型将“类”发送到 CCMenuItem 类型的参数
【发布时间】:2013-07-18 01:18:21
【问题描述】:

我正在让一个 14 岁的孩子在 cocos2d 中制作游戏。我对 cocos2d 很陌生。我想将同一个硬币精灵并排显示以制作图案。

所以我在我的主要游戏层中添加了这个:

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

这就是我初始化菜单的方式:

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

这是我的硬币类中的内容:

- (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];
}

我不断收到Incompatible pointer types sending 'class' to parameter of type 'CCMenuItem'。有人可以告诉我应该如何更改代码以使其正常工作吗?

谢谢!

【问题讨论】:

    标签: iphone objective-c cocos2d-iphone kobold2d


    【解决方案1】:

    menuWithItems: 接受CCMenuItem 对象的数组,您正在发送一个类本身。我不知道Coin 的类是做什么的,但如果目的是显示图像然后在被点击时执行某些操作,我建议您这样做:

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

    你应该创建一个方法coin1WasTapped:,当硬币被点击时会被调用,你可以在这里“收集”硬币。也许将它们从menu 或动画中删除。

    如果您要创建许多硬币,我建议您使用for 循环将它们全部创建在一个数组中。这样以后操作起来会更容易。

    这个tutorial真的很好,它可以帮助你更好地了解你需要做什么以及如何去做。

    祝你好运!

    【讨论】:

    • 我按照你说的做了,但它仍然给我一个警告Incompatible pointer types sending 'Coins *__strong' to parameter of type 'CCMenuItem *'
    • coin是什么对象?你想让它们表现得像按钮吗?
    • 不,可以收集硬币,但我只想用硬币做一条水平线,这样它们就成了一个图案。我只是想使用 CCMenu,这样我就可以水平对齐硬币。不使用 CCMenu 有没有更好的方法?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-12
    • 1970-01-01
    相关资源
    最近更新 更多