Continuously run the method while the finger is held on the screen

I am developing a game that has weapons like a revolver, a rifle and a shotgun. You select a gun and shoot aliens that appear on the screen. I am almost done, but I have problems shooting aliens with automatic firearms, such as a machine gun. For single shots, I use this to detect when an alien is in a crosshair, and if so, hiding it:

CGPoint pos1 = enemyufoR.center; if ((pos1.x > 254) && (pos1.x < 344) && (pos1.y > 130) && (pos1.y < 165 && _ammoCount != 0)) { enemyufoR.hidden = YES; [dangerBar setProgress:dangerBar.progress-0.10]; _killCount = _killCount+3; [killCountField setText: [NSString stringWithFormat:@"%d", _killCount]]; timer = [NSTimer scheduledTimerWithTimeInterval: 4.0 target: self selector: @selector(showrUfo) userInfo: nil repeats: NO]; } 

This is great for most guns, but for a machine gun, I need him to constantly check the position of the enemies during the shooting. How can I do it?

+4
source share
2 answers

Check out timestam from UITouch

+1
source

Um, when you press the shoot button, you obviously call something like:

 // -------------------------------------- // PSEUDO-CODE // -------------------------------------- -(void)shootButtonPressed { [self checkEnemies]; } 

From this, why don't you just declare a BOOL variable and use it to check if all fingers are pressed:

 // -------------------------------------- // PSEUDO-CODE // -------------------------------------- @interface MyGameClass { // defaults to FALSE BOOL isTouching; } -(void)shootButtonPressed { // assumes isTouching is a instance variable declared somewhere isTouching = YES; [self checkEnemies]; } -(void)checkEnemies { // check enemy action // --------------------------------------------- // when finger lifts off the screen, this // isTouching variable will be reset to FALSE // so as long as isTouching is TRUE, we call // this same method checkEnemies again // --------------------------------------------- if(isTouching) { [self checkEnemies]; } } // reset the isTouching variable when user finger is taken off the screen -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { isTouching = NO; } 
+1
source

Source: https://habr.com/ru/post/1442241/


All Articles