亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

iOS開發(fā)中使用cocos2d添加觸摸事件的方法

 更新時(shí)間:2015年10月12日 09:48:45   作者:xiangzilv1987  
這篇文章主要介紹了iOS開發(fā)中使用cocos2d添加觸摸事件的方法,cocos2d是制作iOS游戲的利器,需要的朋友可以參考下

CCLayer類是用來接收觸摸輸入的。不過你要首先啟用這個(gè)功能才可以使用它。你通過設(shè)置isTouchEnabled為YES來讓層接收觸摸事件:

復(fù)制代碼 代碼如下:
self.isTouchEnabled = YES;

此項(xiàng)設(shè)定最好在init方法中設(shè)置。你可以在任何時(shí)間將其設(shè)置為NO或者YES。

一旦啟用isTouchEnabled屬性,許多與接收觸摸輸入相關(guān)的方法將會(huì)開始被調(diào)用。這些事件包括:當(dāng)新的觸摸開始的時(shí)候,當(dāng)手指在觸摸屏上移動(dòng)的時(shí)候,還有在用戶手指離開屏幕以后。很少會(huì)發(fā)生觸摸事件被取消的情況,所以你可以在大多數(shù)情況下忽略它,或者使用ccTouchesEnded方法來處理。

當(dāng)手指首次觸摸到屏幕時(shí)調(diào)用的方法:

復(fù)制代碼 代碼如下:

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

手指在屏幕上移動(dòng)時(shí)調(diào)用的方法:

復(fù)制代碼 代碼如下:

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

當(dāng)手指從屏幕上提起時(shí)調(diào)用的方法:

復(fù)制代碼 代碼如下:

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

當(dāng)觸摸事件被取消時(shí)調(diào)用的方法:

復(fù)制代碼 代碼如下:

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

取消事件的情況很少發(fā)生,所以在大多數(shù)情況下它的行為和觸摸結(jié)束時(shí)相同。

因?yàn)橛|摸事件由Cocoa TouchAPI接收,所以觸摸的位置必須被轉(zhuǎn)換為OpenGL的坐標(biāo)。

以下是一個(gè)用來轉(zhuǎn)換坐標(biāo)的方法:

復(fù)制代碼 代碼如下:

-(CGPoint) locationFromTouches:(NSSet *)touches 

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

默認(rèn)情況下,層接收到的事件和蘋果UIResponder類接收到的是一樣的。cocos2d也支持有針對(duì)性的觸摸處理。和普通處理的區(qū)別是:它每次只接收一次觸摸,而UIResponder總是接收到一組觸摸。有針對(duì)性的觸摸事件處理只是簡(jiǎn)單的把一組觸摸事件分離開來,這樣就可以根據(jù)游戲的需求提供所需的觸摸事件。更重要的是,有針對(duì)性的處理允許你把某些觸摸事件從隊(duì)列里移除。這樣的話,如果觸摸發(fā)生在屏幕某個(gè)指定的區(qū)域,你會(huì)比較容易識(shí)別出來;識(shí)別出來以后你就可以把觸摸標(biāo)記為已經(jīng)處理,并且其它所有的層都不再需要對(duì)這個(gè)區(qū)域再次做檢查。

在你的層中添加以下方法可以啟用有針對(duì)性的觸摸事件處理:

復(fù)制代碼 代碼如下:

-(void) registerWithTouchDispatcher 

    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:kCCMenuTouchPriority swallowsTouches:YES]; 
}

注:如果你把registerWithTouchDispatcher方法留空,你將不會(huì)接收到任何觸摸事件!如果你想保留此方法,而且使用它的默認(rèn)處理方式,你必須調(diào)用[super registerWithTouchDispatcher]這個(gè)方法。


現(xiàn)在,你將使用一套有點(diǎn)不一樣的方法來代替默認(rèn)的觸摸輸入處理方法。它們幾乎完全一樣,除了一點(diǎn):用 (UITouch *)touch 代替 (NSSet *)touches 作為方法的第一個(gè)參數(shù):

復(fù)制代碼 代碼如下:

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {}

-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {}

-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {}

-(void) ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event {}

這里很重要的一點(diǎn)是:ccTouchBegan返回的是一個(gè)布爾值(BOOL)。如果你返回了YES,那就意味著你不想讓當(dāng)前的觸摸事件傳導(dǎo)到其它觸摸事件處理器。你實(shí)際上是“吞下了”這個(gè)觸摸事件。

下面來看一個(gè)完整的例子:
在自己的layer里面,添加

復(fù)制代碼 代碼如下:

[self setIsTouchEnabled:YES];

以下方法是cocos2d類庫的方法:

復(fù)制代碼 代碼如下:

-(void) setIsTouchEnabled:(BOOL)enabled

{

if( isTouchEnabled_ != enabled ) {

isTouchEnabled_ = enabled;

if( isRunning_ ) {

if( enabled )

[self registerWithTouchDispatcher];

else {

CCDirector *director = [CCDirector sharedDirector];

[[director touchDispatcher] removeDelegate:self];

}

}

}

}

//這句是關(guān)鍵

-(void) registerWithTouchDispatcher

{

CCDirector *director = [CCDirector sharedDirector];

[[director touchDispatcher] addStandardDelegate:self priority:0];

}

接下來就實(shí)現(xiàn)方法:

復(fù)制代碼 代碼如下:

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

{

//    for( UITouch *touch in touches ) {

// CGPoint location = [touch locationInView: [touch view]];

//       

// location = [[CCDirector sharedDirector] convertToGL: location];

// CGPoint touchPos = location;

//       

//        NSLog(@"point==%@",NSStringFromCGPoint(touchPos));

// }

   

   

    UITouch* touch = [touches anyObject];

    CGPoint location = [touch locationInView: [touch view]];

    location = [[CCDirector sharedDirector] convertToGL: location];

    //self.position = location;

     NSLog(@"point==%@",NSStringFromCGPoint(location));

}

- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {

    self.isMoving = FALSE;

}

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {

   

    // you can set up a check here if you're not interested in handling every touch.

    // For example if your player is already moving, return no...

   

    BOOL handleTouch = FALSE;

    if (!self.isMoving) {

        handleTouch = TRUE;

        self.isMoving = TRUE;

    }

    return handleTouch;

   

}

相關(guān)文章

最新評(píng)論