【问题标题】:Cocos2D CGRectIntersect not found?Cocos2D CGRectIntersect 没找到?
【发布时间】:2013-03-08 19:11:38
【问题描述】:

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

【问题讨论】:

  • -myRect:的原型是否在声明它的类的头文件中?该标头是否已导入到 Bullet 类中?
  • 标头已导入。原型没有创建,但我也尝试过,它也没有工作。

标签: iphone ios xcode cocos2d-iphone


【解决方案1】:

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

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

【讨论】:

  • 谢谢!现在可以了。还有一件事-我收到一些警告,例如“对象(敌人、子弹等)可能无法响应“更新””。更新功能正确实施,一切正常,但我收到了这些警告。如何摆脱它们?
  • 在界面中也定义相同。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-26
相关资源
最近更新 更多