user3808710
user3808710

Reputation: 27

In SpriteKit how to move two different sprites with two different touches

I want to move two different sprites at the same time but have two different touches for each of them. So one touch will move player One wherever that touch is and touch two will move player Two to where it goes. I do not want both sprites to go to the same direction or towards the same thing. I have tried many things like, having two sprite nodes that would border the areas of where the players could move and where the users could tap and move inside the sprite, that did not work. I would like to try the method above but I could not get the code to work. Please help! Thanks! Here is my code

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

for (UITouch *touch in touches) {
    CGPoint location = [touch locationInNode:self.playerOneBorderBody];
    CGPoint location2 = [touch locationInNode:self.playerTwoBorderBody];
    if (CGRectContainsPoint(self.playerOneBorderBody.frame, location)) {
        [self location:location];
    }

    else if (CGRectContainsPoint(self.playerTwoBorderBody.frame, location2)) {
        [self locationTwo:location2];


}

}

Upvotes: 0

Views: 123

Answers (1)

user1872384
user1872384

Reputation: 7127

To achieve that you need to set the boundaries to control item one and item two. In this case the screen is split into 2 sections. One is to move the left paddle. another one to move the right paddle. (e.g. as shown below if (location.x < self.size.width / 2.0)).

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
        for (UITouch *touch in touches)
        {
            CGPoint location = [touch locationInNode:self];

            if (self.playerOnePaddleControlTouch == nil)
            {
                if (location.x < self.size.width / 2.0)
                {
                    self.playerOnePaddleControlTouch = touch;
                }
            }
            if (self.playerTwoPaddleControlTouch == nil)
            {
                if (location.x > self.size.width / 2.0)
                {
                    self.playerTwoPaddleControlTouch = touch;
                }
            }
        }
        return;
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    for (UITouch *touch in touches)
    {
        if (touch == self.playerOnePaddleControlTouch)
        {
            [self moveFirstPaddle];
        }
        else if (touch == self.playerTwoPaddleControlTouch)
        {
            [self moveSecondPaddle];
        }
    }
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    for (UITouch *touch in touches) {
        if (touch == self.playerOnePaddleControlTouch)
        {
            self.playerOnePaddleControlTouch = nil;
        }
        else if (touch == self.playerTwoPaddleControlTouch)
        {
            self.playerTwoPaddleControlTouch = nil;
        }
    }
}

// Add in any movement that you want base on the previous location
-(void)moveFirstPaddle
{
    CGPoint previousLocation = [self.playerOnePaddleControlTouch previousLocationInNode:self];
    CGPoint newLocation = [self.playerOnePaddleControlTouch locationInNode:self];
    if (newLocation.x > self.size.width / 2.0)
    {
        //finger is on the other player side
        return;
    }
    CGFloat x = self.playerOnePaddleNode.position.x;
    CGFloat y = self.playerOnePaddleNode.position.y + (newLocation.y - previousLocation.y) * kPaddleMoveMult;
    CGFloat yMax = self.size.height - self.playerOnePaddleNode.size.width/2.0 - self.playerOnePaddleNode.size.height/2.0;
    CGFloat yMin = self.playerOnePaddleNode.size.width/2.0 + self.playerOnePaddleNode.size.height/2.0;
    if (y > yMax)
    {
        y = yMax;
    }
    else if(y < yMin)
    {
        y = yMin;
    }
    self.playerOnePaddleNode.position = CGPointMake(x, y);
}

// Add in any movement that you want base on the previous location
-(void)moveSecondPaddle
{
    CGPoint previousLocation = [self.playerTwoPaddleControlTouch previousLocationInNode:self];
    CGPoint newLocation = [self.playerTwoPaddleControlTouch locationInNode:self];
    if (newLocation.x < self.size.width / 2.0)
    {
        //finger is on the other player side
        return;
    }
    CGFloat x = self.playerTwoPaddleNode.position.x;
    CGFloat y = self.playerTwoPaddleNode.position.y + (newLocation.y - previousLocation.y) * kPaddleMoveMult;
    CGFloat yMax = self.size.height - self.playerTwoPaddleNode.size.width/2.0 - self.playerTwoPaddleNode.size.height/2.0;
    CGFloat yMin = self.playerTwoPaddleNode.size.width/2.0 + self.playerTwoPaddleNode.size.height/2.0;
    if (y > yMax)
    {
        y = yMax;
    }
    else if(y < yMin)
    {
        y = yMin;
    }
    self.playerTwoPaddleNode.position = CGPointMake(x, y);
}

Upvotes: 1

Related Questions