您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

cocos2d 觸控事件完整指南:標準觸控與針對性觸控的差異

編輯:iOS 開發綜合

先啟用觸控

CCLayer 是接收觸控輸入的載體,但預設是關閉的,必須先自行啟用:

self.isTouchEnabled = YES;

這行設定建議放在 init 方法中,之後任何時間都可以切回 NO 或再開啟。

啟用之後,一組與觸控相關的方法便會開始被呼叫,涵蓋三個階段:手指首次接觸螢幕、手指在螢幕上移動、手指離開螢幕。

四個標準觸控回呼

// 手指首次接觸螢幕
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

// 手指在螢幕上移動
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

// 手指離開螢幕
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

// 觸控被系統取消
- (void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

取消的情況相對罕見(例如來電、通知強制插入),多數時候可以忽略,或比照 ccTouchesEnded 的方式善後。

座標系統的轉換

觸控事件由 Cocoa Touch 的 API 接收,座標基準是 UIKit 的視圖系統;但 cocos2d 的場景使用 OpenGL 座標。兩者必須做轉換,否則角色永遠不會出現在手指的位置:

- (CGPoint)locationFromTouches:(NSSet *)touches {
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView:[touch view]];
    return [[CCDirector sharedDirector] convertToGL:touchLocation];
}

針對性觸控:一次只處理一個觸點

預設情況下,層接收到的事件形態和 UIResponder 一樣——一次給你一整組觸控。cocos2d 另外支援「針對性觸控」,差異在於:

  • 每次只接收單一觸控,其餘觸點被拆開處理。
  • 可以主動把某些觸控從佇列中移除。若觸控發生在你關心的區域,辨識出來後就能標記為已處理,其他層不必再重複檢查。

在層中實作以下方法即可啟用:

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

注意:如果這個方法留空,你將收不到任何觸控事件。若只想沿用預設行為,必須呼叫 [super registerWithTouchDispatcher]。

啟用後,回呼方法的簽名會略有不同——第一個參數從 (NSSet *)touches 換成 (UITouch *)touch:

- (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 {}

最關鍵的一點:ccTouchBegan 回傳的是 BOOL。回傳 YES 代表你要把這個觸控吞下來,不讓它繼續傳給其他處理器。

完整示例

在自己的 layer 中先啟用觸控:

[self setIsTouchEnabled:YES];

cocos2d 類庫中的實作長這樣,其中註冊環節是關鍵:

- (void)setIsTouchEnabled:(BOOL)enabled {
    if (isTouchEnabled_ != enabled) {
        isTouchEnabled_ = enabled;
        if (isRunning_) {
            if (enabled) {
                [self registerWithTouchDispatcher];
            } else {
                CCDirector *director = [CCDirector sharedDirector];
                [[director touchDispatcher] removeDelegate:self];
            }
        }
    }
}

// 這句是關鍵
- (void)registerWithTouchDispatcher {
    CCDirector *director = [CCDirector sharedDirector];
    [[director touchDispatcher] addStandardDelegate:self priority:0];
}

接著實作各個回呼:

- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:[touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];
    NSLog(@"point==%@", NSStringFromCGPoint(location));
}

- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
    self.isMoving = FALSE;
}

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    // 可以在這裡過濾:例如角色已在移動中,就不接收新的觸控
    BOOL handleTouch = FALSE;
    if (!self.isMoving) {
        handleTouch = TRUE;
        self.isMoving = TRUE;
    }
    return handleTouch;
}

這個範例示範了一個很實用的技巧:用 ccTouchBegan 的回傳值當作守門員,只在角色閒置時才接受新的觸控指令,避免動作被打斷或堆疊。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved