【问题标题】:CCTouch Bounding Box not being created未创建 CCTouch 边界框
【发布时间】:2012-08-30 18:39:29
【问题描述】:

我正在使用 Cocos2d 在 iOS 中制作游戏。

这是我用来检查立方体是否被触摸的方法,

- (void)selectSpriteForTouch:(CGPoint)touchLocation {
    CubeSprite * newSprite = nil;
    for (CubeSprite *sprite in movableSprites) {
        NSLog(@"tested against sprite %i", sprite.boundingBox.origin.x);
        if (CGRectContainsPoint(sprite.boundingBox, touchLocation)) {      
            singleCube = true;
            newSprite = sprite;
            activeTag = sprite.tag;
            break;
        }
    }    
    if (newSprite != selSprite) {

        selSprite = newSprite;
    }
}

但由于某种原因,sprite.boundingBox 的设置不正确。

“测试精灵”日志只打印出“测试精灵 0”,这似乎是不可能的,因为我可以在屏幕上看到精灵。

这是我用来将立方体添加到场景中的方法,

-(void)addCube:(CubeSprite *)cube {
    int totalCubes = [cubes count];
    [cube setPosition:ccp(700 - (totalCubes * 50), 120)];
    [cubes addObject:cube];
    [movableSprites addObject:cube];
    [self addChild:cube];
}

可能出了什么问题?

提前致谢。

编辑,这是我的立方体初始化方法

-(id)initWithNumber:(int)number {
    if( (self=[super init])) {
        [self setTag:number];
        CCSprite* sprite = [CCSprite spriteWithFile:string];
        [self addChild:sprite];
        NSLog(@"Cube created with value of %i and with file %@", number, string);
    }
    return self;
}

【问题讨论】:

    标签: ios cocos2d-iphone


    【解决方案1】:

    尝试使用这个

     for (CubeSprite *sprite in movableSprites) {
    CGRect projectileRect = [sprite boundingBox];
    
    if (CGRectContainsPoint(projectileRect, touchLocation)) {      
                singleCube = true;
                newSprite = sprite;
                activeTag = sprite.tag;
                break;
            }
        }    
    

    【讨论】:

      【解决方案2】:

      也许 CGRectContainsPoint 返回假阳性。如果禁用“break ;”声明,它应该遍历所有的多维数据集。

      一些可能出错的事情: - 立方体的边界框太大,位置不是你想的 - 所有立方体都有相同的位置 - touchLocation 为 (0,0)

      【讨论】:

        【解决方案3】:

        Xcode 没有给你一个关于不正确格式字符串的警告吗?

        NSLog(@"tested against sprite %i", sprite.boundingBox.origin.x);
        

        %i 表示整数值。 CGPoint 的坐标是浮点值。请改用%f

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-07-28
          • 1970-01-01
          • 2021-02-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-09-12
          • 2021-10-05
          相关资源
          最近更新 更多