Cocos2D ccTouchesMoved Sprites and Objects
I have been struggling with this for a bit. I am trying to avoid have multiple classes for the objects I am trying to create. Basically I have a 'Letter' class that has a letter property. When I initialize the object I set the letter type to A, B, C...I am doing this all in a loop. Everything seems fine.
The issue is when I am firing the ccTouchesMoved event I would like to know if I am moving the Letter object of type A or B etc..I cannot figure this one out.
Here are some snippets to show what I am doing:
Letter Class
@implementation Letter
- (id)init {
if ((self = [super init])) {
gamePieceType = kLetterNotAssigned;
}
return self;
}
My Layer Init
for (int x=0; x < NUMBER_OF_ITEMS; x++) {
int randomX = random() % 1024;
[self createPuzzlePieceAtLocation:ccp(randomX, 600) withPiece:x];
}
The createPuzzlePieceAtLocati开发者_如何学Pythonon method
- (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];
Any thoughts? I get the touchLocation in ccTouchesMoved but how can I get the object?
You have to determine what letter has been touched.
The simplest way is to iterate over all your letters (put them in array when creating) and checking for the letters that accepts touch.
The fastest way is to use physics engine for fast searching (Box2D and chipmunk engines are coming with cocos2d).
When letter is determined simply check it's type
精彩评论