简体   繁体   English

在SpriteKit中,如何用两个不同的触摸移动两个不同的精灵

[英]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. 因此,只要一触即可将玩家1移至该位置,而触二则将玩家2移至该位置。 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. 我尝试了很多事情,例如,有两个Sprite节点与玩家可以移动的区域以及用户可以在Sprite内轻击并移动的区域接壤,但是这没有用。 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];


}

}

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. 另一个移动右桨。 (eg as shown below if (location.x < self.size.width / 2.0)). (例如,如果(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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM