触摸Cocos2d中的移动

时间:2011-11-01 13:29:20

标签: cocos2d-iphone

我在触摸事件中遇到问题。 我有5个精灵,我想只移动一个精灵所选的精灵。但是当我移动触摸时,只有一个Sprite移动如何移动只选择Sprite ..

<。>文件中的

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

1 个答案:

答案 0 :(得分: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.
    }
}

或为你的精神分配标签并检查它们。