我想同时移动两个不同的精灵,但每个精灵都有两个不同的触摸。所以一触即可移动玩家One触摸的任何地方触摸两个将移动玩家二到它去的地方。我不希望两个精灵都朝同一个方向走向同一个方向。我已经尝试过很多东西,例如,有两个精灵节点可以与玩家可以移动的区域以及用户可以在精灵内部移动和移动的区域相邻,但这些节点不起作用。我想尝试上面的方法,但我无法让代码工作。请帮忙!谢谢! 这是我的代码
-(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];
}
}
答案 0 :(得分:1)
要实现这一点,您需要设置边界以控制第一项和第二项。在这种情况下,屏幕分为2个部分。一个是移动左桨。另一个移动右桨。 (例如,如下所示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);
}