I need some ideas for these questions in Cocos2dx
How to prevent users touch continuously. For example, I shoot bullet continuously, I want there is a delay about 1 second even I press touch continuously. I mean touch continuously not touch and hold.
How to detect if the hold touch event?
1: When your bullet fires you could set a boolean (e.g. bulletPassing) to notify you that touches should be ignored (you use it in your ccTouchBegan method). Then you create an action that will notify you when one second passes. Let's say your class is called MyLayer and you have a function bulletDelayPassed that will set you boolean:
bool ArmourTest::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent){
if( !bulletPassing ){
this->runAction( CCSequence::create( CCDelayTime::create(1),
CCCallFunc::create(this, callfunc_selector(MyLayer::bulletDelayPassed)), NULL) );
bulletPassing = true;
shootBullet();
}
isTouching = true;
...
}
void MyLayer::bulletDelayPassed(){
bulletPassing = false;
}
2: Best way to do this is to create a boolean to tell you that a touch is active (e.g. isTouching), you set it to true in your ccTouchBegan method and to false in your ccTouchEnded. If you need to measure how long the touch is held, you should schedule an update and update some time variable that you set to 0 in ccTouchBegan and you update it only when your touching boolean is true:
void MyLayer::update( float dt ){
if( isTouching ) touchDuration += dt;
}