2013-02-15 32 views
1

我正在開發cocos 2D中的遊戲。在我的遊戲中,我需要點擊我的視圖一定次數。如果用戶嘗試點擊超出限制的視圖,則應顯示警報。如何在一定時間內查看某個視圖是否在Cocos中超過2次2D

請有人幫我找到視圖上的水龍頭數量。最重要的是水龍頭的數量不同時。在Total遊戲中,用戶只能點擊一定次數,之後他們不應該點擊視圖。

回答

0

你可以用NSTimeInterval來做到這一點。

//decalre this in interface file 
    NSTimeInterval  mLastTapTime; 

-(id)init 
{ 
    if(self = [super init]) 
    { 
     mLastTapTime = [NSDate timeIntervalSinceReferenceDate]; 
    } 
    return self; 
} 

//聯繫方法

NSTimeInterval currentTime = [NSDate timeIntervalSinceReferenceDate]; 
    NSTimeInterval diff = currentTime - mLastTapTime; 

    if(diff < 0.3) 
    { 
     //do whatever you want if user press with 0.3second 
    } 
    mLastTapTime = [NSDate timeIntervalSinceReferenceDate]; 
+1

替代方案:UITapGestureRecognizer(設置爲檢測雙擊) – LearnCocos2D 2013-02-15 14:25:31

+0

感謝您的信息先生。快樂的編碼。 – Guru 2013-02-15 17:59:33

+0

謝謝它適用於我..但我怎麼會限制一個視圖的水龍頭的數量 – 2013-02-18 06:02:03

0

由於LearnCocos建議你可以簡單地使用敲擊手勢識別。它需要一個可變數量來點擊和/或觸摸點燃。

UITapGestureRecognizer *tapLimitRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapLimitRecognizer:)]; 
[tapLimitRecognizer setNumberOfTapsRequired:6]; 

[self addGestureRecognizer:tapLimitRecognizer]; 
[tapLimitRecognizer release]; // if not using arc. 

-(void)tapLimitRecognizer:(UITapGestureRecognizer *)tapLimitRecognizer 
{ 
...add your alert view here 
} 
+0

其實我正在使用cocos2d。在那我只加一行代碼\t \t [self setIsTouchEnabled:YES]; (void)ccTouchesEnded:(NSSet *)觸及事件:(UIEvent *)event { }觸摸動作代碼後的響應將在此方法中 – 2013-02-18 05:54:25

相關問題