blitzeus
blitzeus

Reputation: 495

iOS detecting multiple touches at once

I'm creating a game, and currently if you touch the left half of the screen a gun rotates clockwise and if you touch the right half of the screen it shoots.

I want to create an action so that if I touch the left half of the screen it will rotate clockwise and if you touch the right half it rotates counterclockwise. THIS IS EASY.

I want the shooting to happen if I'm touching the left and right half's at the same time. How is this possible to detect 2 touches at the same time.

This is the code for rotating clockwise if touching the left half and shooting when touching the right half

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self.view];

    if (touchPoint.x < self.size.width / 2.0 ) {
        isTouching = true;
    }
    else
        [self setupBullet];
}

Upvotes: 0

Views: 126

Answers (1)

Putz1103
Putz1103

Reputation: 6211

You will need to either setup two separate objects that listen for touch events in their respective side, or you can use the event.allTouches object and get a complete array of all current touch objects and do with that array as you will.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    //either:
    if(touch in left side)
    {
        //Left side touch down, create object
    }
    else
    {
        //Right side touch down, create object
    }

    //Or:
    NSArray *arrayOfAllTouches = event.allTouches;
    //Now loop through that array and see what is going on on screen
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    //either:
    if(touch in left side)
    {
        //Left side release, release object
    }
    else
    {
        //Right side release, release object
    }

    //Or:
    NSArray *arrayOfAllTouches = event.allTouches;
    //Now loop through that array and see what is going on on screen
}

Upvotes: 1

Related Questions