2012-08-30 97 views
0

我正在使用Cocos2d在iOS中進行遊戲。未創建CCTouch邊界框

這是法林使用來檢查,看是否有立方體已被觸摸,

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

怎麼可能是錯的?

在此先感謝。

編輯,這裏是我的立方體init方法

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

回答

0

嘗試使用此

for (CubeSprite *sprite in movableSprites) { 
CGRect projectileRect = [sprite boundingBox]; 

if (CGRectContainsPoint(projectileRect, touchLocation)) {  
      singleCube = true; 
      newSprite = sprite; 
      activeTag = sprite.tag; 
      break; 
     } 
    }  
0

也許CGRectContainsPoint返回false獨到之處。如果你禁用「休息」;聲明,它應該遍歷所有的立方體。

有些事情是錯的: - 多維數據集的外接矩形框過大,位置不是你想 - 所有多維數據集具有相同的位置 - touchLocation是(0,0)

0

沒有的Xcode給你一個關於不正確格式字符串的警告?

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

%i表示整數值。 CGPoint的座標是浮點值。改爲使用%f