0

我希望能够在用户触摸时移动某些精灵图像。这些方面的东西:

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    for (UITouch *touch in touches)
    {
        CGPoint *location = [touch locationInView:[touch view]];
        location = [[CCDirector sharedDirector] convertToGL: location];
        if (CGRectContainsPoint(sprite.boundingBox, location)
        {
            sprite.location = ccp(location.x, location.y);
        }
    }
}

当然,这对我不起作用,因为没有运行此方法的刻度方法来不断移动 CCSprite。我知道方法 ccTouchMoved,但我不确定如何实现它,任何示例将不胜感激。

4

3 回答 3

4

只有 CClayer 能够检测到触摸,它是自动的。因此,您将需要处理每个滴答声。它应该是 ccTouchesMove .. 类似:

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"begin");
}

-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{    
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView: [touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];

    sprite.position = location;
}

-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"Ended");
}

当然,在你的初始化中,你必须设置self.isTouchEnabled = YES;

于 2011-03-21T11:53:05.667 回答
2

您可以在场景中使用以下方法拖动精灵图像:

-(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
    CGPoint location = [self convertTouchToNodeSpace: touch];
    sprite.position = location;
}
于 2011-08-02T02:27:55.510 回答
1

请根据以下代码修改您的代码,它对我有用。

UITouch * touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
if (CGRectContainsPoint(sprite.boundingBox,location)){
    [sprite setPosition:location];

}
于 2011-11-24T12:13:55.203 回答