2011-09-22 50 views
0

我一直在爲此苦苦掙扎。我試圖避免爲我想創建的對象創建多個類。基本上我有一個'Letter'類,它有一個字母屬性。當我初始化對象時,我將字母類型設置爲A,B,C ...我在循環中完成這一切。一切似乎都很好。Cocos2D ccTouchesMoved Sprite and Objects

問題是當我發射ccTouchesMoved事件我想知道如果我正在移動類型A或B等字母對象..我無法弄清楚這一個。

下面是一些片段,顯示我在做什麼:

信類

@implementation Letter 

- (id)init { 
    if ((self = [super init])) { 
     gamePieceType = kLetterNotAssigned; 
    } 

    return self; 
} 

我的層初始化

for (int x=0; x < NUMBER_OF_ITEMS; x++) { 
    int randomX = random() % 1024; 
    [self createPuzzlePieceAtLocation:ccp(randomX, 600) withPiece:x]; 
} 

的createPuzzlePieceAtLocation方法

- (void)createPuzzlePieceAtLocation:(CGPoint)location 
           withPiece:(int)tagValue { 

switch (tagValue) { 
case 1: 
     letterSprite = [[Letter alloc] initWithSpriteFrameName:@"upper_a.png"]; 
     letterSprite.gamePieceType = kLetterA; 
     break; 
... 
} 
[self createBodyAtLocation:location forSprite:letterSprite isBox:FALSE]; 
[sceneSpriteBatchNode addChild:letterSprite]; 

有什麼想法?我在ccTouchesMoved中獲得了touchLocation,但是如何獲取對象?

回答

0

您必須確定已觸摸的字母。

最簡單的方法是迭代所有字母(創建時將它們放入數組中)並檢查接受觸摸的字母。

最快的方法是使用物理引擎進行快速搜索(Box2D和花栗鼠引擎與cocos2d一起使用)。

當確定字母時,只需檢查它的類型