找不到Cocos2D CGRectIntersect?

时间:2013-03-08 19:11:38

标签: iphone ios xcode cocos2d-iphone

我在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和损坏函数?

1 个答案:

答案 0 :(得分:1)

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

if(CGRectIntersectsRect([self.mySprite boundingBox],r))