触摸后动画精灵?
问题描述:
在我使用Cocos2d制作的游戏中,我有一个精灵在屏幕底部静止不动。当点击屏幕时,我希望精灵移动到屏幕被点击的位置,然后在一系列帧中生成动画,然后移动到原始位置。我知道我需要使用CCS序列,但我还不知道如何将其移动到触摸位置。目前,我已经搜索周围,我正在使用此代码:触摸后动画精灵?
-(void) TouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *Touch = [touches anyObject];
CGPoint location = [Touch locationInView:[Touch view]];
[swat runAction:[CCMoveTo actionWithDuration:3 position:location]];}
我没有收到错误,但精灵没有反应。有任何想法吗?
答
首先,你必须在方法的名称拼写错误。这是“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];
答
使用
-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
答
首先确保你已经注册TouchDispatcher
:
(void) registerWithTouchDispatcher
{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}
然后实现:
(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event method
,并确认您有:
self.isTouchEnabled = YES;
init()
方法中的代码行。
我已经试过这个,精灵仍然没有移动。我是否需要导入任何文件或类似的东西?如何让它直接使用OpenGL移动,就像不需要convertToGL一样? – akuritsu 2012-02-16 05:57:44
否则,它可能是一个层的问题? – akuritsu 2012-02-16 06:05:29
您是否为此图层启用了触摸功能?添加self.isTouchEnabled = YES;层的init方法。 – Kreiri 2012-02-16 08:30:41