1

我在 Cocos2D (v2.0) 中的游戏有问题。我遇到了两个问题(我在代码部分中对它们进行了评论)。这是 Bullet.m 文件:

-(BOOL)checkCollisions:(CGRect)r
{
BOOL x = NO;

if(CGRectIntersectsRect([theGame myRect:self.mySprite],r)) //FIRST ISSUE - Sending 'id' to parameter of incompatible type 'CGRect' (aka 'struct CGRect'); 
                                                           //SECOND ISSUE - Instance method'-myRect:' not found (return type defaults to 'id') 
{
    x=YES;
    [self reset];
}
return x;
}

稍后在此文件中:

-(void)update
{
switch (self.whoFired)
{
    case 1:
        [self.mySprite setPosition:ccp(self.mySprite.position.x,self.mySprite.position.y + self.firingSpeed)];

        for(Enemy * s in theGame.enemies)
        {
            if(ccpDistance(self.mySprite.position, s.mySprite.position)<30)
            {
              if([self checkCollisions:[theGame myRect:s.mySprite]]) //FIRST ISSUE - Sending 'id' to parameter of incompatible type 'CGRect' (aka 'struct CGRect')
                                                                     //SECOND ISSUE - Instance method '-damage' not found (return type defaults to 'id')
                {
                    [s damage];
                }
            }
        }
        break;
...

因此,两个错误是相同的:将“id”传递给不兼容类型“CGRect”(又名“struct CGRect”)的参数。另外两个是关于 Damage 和 MyRect 函数的。它们当然存在(在 GameScene.m 和 Enemy.m 文件中;所有内容都由 .h 文件连接,并且在 Damae 和 MyRect 函数中没有错误):

-(CGRect)myRect:(CCSprite *)sp
{
CGRect rect = CGRectMake(sp.position.x-sp.textureRect.size.width/2, sp.position.y-sp.textureRect.size.height/2, sp.textureRect.size.width, sp.textureRect.size.height);
return rect;
}


-(void)damage
{
self.hp--;
[self.mySprite runAction:[CCSequence actions:
                          [CCTintTo actionWithDuration:0.5 red:255 green:0 blue:0],
                          [CCTintTo actionWithDuration:0.5 red:255 green:255 blue:255],nil]];
if(hp<=0)
{
    [self destroy];
}
}

有什么问题?为什么编译器看不到 myRect 和损坏函数?

4

1 回答 1

1

为什么不能使用 CCSprite 的默认 boundingBox ?

if(CGRectIntersectsRect([self.mySprite boundingBox],r))
于 2013-03-09T08:26:48.560 回答