2011-11-01 157 views
0

我在触摸事件中遇到问题。 我有5个精灵,我想移动一个精灵只有选定的精灵。但在这时候我在.m文件移动触摸那么只有一个Sprite是移动如何移动的唯一选择雪碧..触摸在Cocos2d中移动

在.h文件中

CCSprite *puzzButton1,*puzzButton2,*puzzButton3; 

// create and initialize our seeker sprite, and add it to this layer 
    puzzButton1 = [CCSprite spriteWithFile: @"ingame_button_idal.png"]; 
    puzzButton1.position = ccp(480/2, 320/2); 
    [self addChild:puzzButton1]; 

    // create and initialize our seeker sprite, and add it to this layer 
    puzzButton2 = [CCSprite spriteWithFile: @"ingame_button_idal.png"]; 
    puzzButton2.position = ccp(100, 200); 
    [self addChild:puzzButton2]; 

    // create and initialize our seeker sprite, and add it to this layer 
    puzzButton3 = [CCSprite spriteWithFile: @"ingame_button_idal.png"]; 
    puzzButton3.position = ccp(100, 300); 
    [self addChild:puzzButton3]; 

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event 
{  
return TRUE;  
} 
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event 
{ 

    CGPoint location = [self convertTouchToNodeSpace: touch]; 


[puzzButton1 stopAllActions]; 
[puzzButton1 runAction: [CCMoveTo actionWithDuration:0.1 position:location]]; 
} 

回答

0

试试这个:

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *myTouch = [touches anyObject]; 
    CGPoint location = [myTouch locationInView:[myTouch view]]; 

    location = [[CCDirector sharedDirector] convertToGL:location]; 

    CGRect touchpoint = CGRectMake(160, 66, 564, 220); // your sprite position 

    if(CGRectContainsPoint(touchpoint, location)) 
    { 
     yoursprite.position = ccp(location.x,location.y);// write move code 
                  //here and check same 
                  //condition for all 
                  //your sprite. 
    } 
} 

或为您的精灵分配标签并检查它们。