gpt4 book ai didi

ios - 不相互 react 的不同触摸 objective-c

转载 作者:行者123 更新时间:2023-12-01 20:20:01 25 4
gpt4 key购买 nike

我在 Ipad 上为两个玩家制作简单的游戏,就像乒乓球一样,当一个手指在屏幕上时,玩家可以用他的 Racket 使用react,但是当第二个手指在他的 Racket 上时,他不能移动他的 Racket ,但他移动了第一个 Racket 相反,我设置了一些 NSLog它说他们两个都在移动,但这是不对的,这是我的代码示例:

-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
/* Called when a touch begins */
for (UITouch *touch in touches) {
//First player
CGPoint touchLocation = [touch locationInNode:self];
SKPhysicsBody* body = [self.physicsWorld bodyAtPoint:touchLocation];

if (body && [body.node.name isEqualToString: paddleCategoryName]) {
NSLog(@"Began touch on first paddle");
self.isFingerOnPaddle = YES;
}
//Second player
CGPoint secondTouchLocation = [touch locationInNode:self];
SKPhysicsBody* secondbody = [self.physicsWorld bodyAtPoint:secondTouchLocation];
if (secondbody && [secondbody.node.name isEqualToString: secondPaddleCategoryName]) {
NSLog(@"Began touch on second paddle");
self.isSecondFingerOnPaddle = YES;
}
}
}

-(void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
for (UITouch *touch in touches) {
//for first player
if (self.isFingerOnPaddle) {
CGPoint touchLocation = [touch locationInNode:self];
CGPoint previousLocation = [touch previousLocationInNode:self];
SKSpriteNode* paddle = (SKSpriteNode*)[self childNodeWithName: paddleCategoryName];
int paddleY = paddle.position.y + (touchLocation.y - previousLocation.y);
paddleY = MAX(paddleY, paddle.size.height/2);
paddleY = MIN(paddleY, self.size.height - paddle.size.height/2);
paddle.position = CGPointMake(paddle.position.x, paddleY);
NSLog(@"First paddle moving");

}
//for second player
if (self.isSecondFingerOnPaddle) {
CGPoint touchLocation = [touch locationInNode:self];
CGPoint previousLocation = [touch previousLocationInNode:self];
SKSpriteNode* secondPaddle = (SKSpriteNode*)[self childNodeWithName: secondPaddleCategoryName];
int secondPaddleY = secondPaddle.position.y + (touchLocation.y - previousLocation.y);
secondPaddleY = MAX(secondPaddleY, secondPaddle.size.height/2);
secondPaddleY = MIN(secondPaddleY, self.size.height - secondPaddle.size.height/2);
secondPaddle.position = CGPointMake(secondPaddle.position.x, secondPaddleY);
NSLog(@"Second paddle moving");
}
}
}
-(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
self.isFingerOnPaddle = NO;
self.isSecondFingerOnPaddle = NO;
}

我做错了什么,我需要改变我的代码工作,就像它应该的那样

最佳答案

更好的方法是子类 paddle 并在 paddle 子类中执行触摸代码。这消除了循环遍历触摸数组以查看您的拨片是否被触摸的麻烦,而且它使代码更加整洁和易于阅读。唯一的事情是你必须对你的节点设置稍微不同的样式,因为触摸移动在桨之外不起作用。

这是你的风格

1) Subclass Paddle to SKNode
2) Create a child of SKSpriteNode for your actual paddle gfx
3) Paddle frame should be set at the width of your scene, with a height that is allowed for the player to touch (probably the height of the paddle gfx)
4) override the touch code inside Paddle, all touch code in the set should only relate to what is being touched by the paddle
5) Do all of your sliding logic inside these overrides



现在您已经为您正在寻找的行为定义了 Paddle,您可以将它添加到您喜欢的场景中。

这种方法的好处是你消除了很多重复的代码,(因为两边的桨表现不同),如果你想增加一些乐趣,你可以在两边添加更多的桨以提供更多的变化。 (每个玩家在比赛 field 中间和底部都有一个桨,可以独立移动)

关于ios - 不相互 react 的不同触摸 objective-c ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35862109/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com