2012-02-15 232 views
0

在我使用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]];} 

我沒有收到錯誤,但精靈沒有反應。有任何想法嗎?

回答

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

+0

我已經試過這個,精靈仍然沒有移動。我是否需要導入任何文件或類似的東西?如何讓它直接使用OpenGL移動,就像不需要convertToGL一樣? – akuritsu 2012-02-16 05:57:44

+0

否則,它可能是一個層的問題? – akuritsu 2012-02-16 06:05:29

+1

您是否爲此圖層啓用了觸摸功能?添加self.isTouchEnabled = YES;層的init方法。 – Kreiri 2012-02-16 08:30:41

1

使用

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

首先確保你已經註冊TouchDispatcher

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

然後實現:

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

,並確認您有:

self.isTouchEnabled = YES; 

init()方法中的代碼行。