触摸后动画精灵?

时间:2012-02-15 11:28:56

标签: xcode4 cocos2d-iphone ccsprite

在我使用Cocos2d制作的游戏中,我在屏幕底部有一个保持静止的精灵。当点击屏幕时,我希望精灵移动到屏幕被点击的位置,然后通过一系列帧动画,然后移动到其原始位置。我知道我需要使用CCSequence,但我还不知道如何让它移动到触摸位置。目前,我已经四处搜索,我正在使用此代码:

-(void) TouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *Touch = [touches anyObject];
CGPoint location = [Touch locationInView:[Touch view]];
[swat runAction:[CCMoveTo actionWithDuration:3 position:location]];}

我没有错误,但精灵没有反应。有什么想法吗?

3 个答案:

答案 0 :(得分:3)

首先,你的方法名称有拼写错误。它是“ccTouchesBegan”,而不是“TouchesBegan”。

-(void)ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event

其次,你的精灵不会移动到你期望的位置。 locationInView返回UIKit坐标中的点,CCMoveTo使用OpenGL坐标。您需要将该点转换为OpenGL坐标:

UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];

答案 1 :(得分:1)

使用

-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

答案 2 :(得分:0)

首先确保您已注册TouchDispatcher

(void) registerWithTouchDispatcher
{
    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}

然后执行:

(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event method

并确认你有:

self.isTouchEnabled = YES;

init()方法中的代码行。