如何在 touchesEnded 中检测哪些节点仍在被按下
How to detect in touchesEnded which nodes are still being pressed
我已经被这个问题困扰好几天了。我有多个 SKSpriteNode,一个用于左箭头、右箭头和向上箭头。当我按住向右箭头时,我希望我的角色在被按住时继续向右移动,另一方面,如果你按下向上箭头,那么无论你是否按住它,你都只会跳一次。
所以我的问题是,例如,当我按住右箭头然后按下向上箭头时,touchesEnded 被调用并且它阻止我的角色向右移动,即使我的手指仍然放在右箭头上
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
for (UITouch *touch in touches){
CGPoint location = [touch locationInNode:self];
if (CGRectContainsPoint(rightArrow.frame, location)){
[wizard setTexture:[SKTexture textureWithImageNamed:@"wizardRight"]];
didTouchRightArrow = YES;
isLookingRight = YES;
isLookingLeft = NO;
rightArrow.alpha = 0.5;
NSLog(@"Touching right");
}
if (CGRectContainsPoint(leftArrow.frame, location)){
[wizard setTexture:[SKTexture textureWithImageNamed:@"wizardLeft"]];
isLookingRight = NO;
isLookingLeft = YES;
didTouchLeftArrow = YES;
leftArrow.alpha = 0.5;
NSLog(@"Touching left");
}
if (CGRectContainsPoint(upArrow.frame, location)){
didTouchUpArrow = YES;
upArrow.alpha = 0.5;
NSLog(@"Touching up");
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
if (rightArrow.alpha != 1.0){
rightArrow.alpha = 1.0;
}
if (leftArrow.alpha != 1.0){
leftArrow.alpha = 1.0;
}
if (upArrow.alpha != 1.0){
upArrow.alpha = 1.0;
for (UITouch *touch in touches){
CGPoint location = [touch locationInNode:self];
if (CGRectContainsPoint(rightArrow.frame, location)){
NSLog(@"Touching right");
didTouchRightArrow = YES;
} {
NSLog(@"Not touching right");
didTouchRightArrow = NO;
}
if (CGRectContainsPoint(leftArrow.frame, location)){
NSLog(@"Touching Left");
didTouchLeftArrow = YES;
} else {
NSLog(@"not touching left");
didTouchLeftArrow = NO;
}
didTouchUpArrow = NO;
}
这可能不是解决问题的正确方法,但在 touchesEnded 中,我试图查看触摸是否仍在所需的 Rect 中。
您需要一种方法来识别正在注册触摸的不同节点。有不止一种方法可以做到这一点,但我总是发现使用节点的名称 属性 是最简单易用的。
您已经有了正确的想法,使用 BOOL 来注册触摸状态。
我写了一些代码来处理你想要完成的事情:
#import "GameScene.h"
@implementation GameScene {
SKSpriteNode *node0;
SKSpriteNode *node1;
BOOL node0touch;
BOOL node1touch;
}
-(void)didMoveToView:(SKView *)view {
self.backgroundColor = [SKColor blackColor];
node0 = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(100, 100)];
node0.name = @"node0";
node0.position = CGPointMake(100, 300);
[self addChild:node0];
node1 = [SKSpriteNode spriteNodeWithColor:[SKColor blueColor] size:CGSizeMake(100, 100)];
node1.name = @"node1";
node1.position = CGPointMake(400, 300);
[self addChild:node1];
node0touch = false;
node1touch = false;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
CGPoint touchLocation = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:touchLocation];
if([node.name isEqualToString:@"node0"])
node0touch = true;
if([node.name isEqualToString:@"node1"])
node1touch = true;
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
CGPoint touchLocation = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:touchLocation];
if([node.name isEqualToString:@"node0"])
node0touch = false;
if([node.name isEqualToString:@"node1"])
node1touch = false;
}
}
-(void)update:(CFTimeInterval)currentTime {
if(node0touch)
NSLog(@"node0 touch");
if(node1touch)
NSLog(@"node1 touch");
}
我已经被这个问题困扰好几天了。我有多个 SKSpriteNode,一个用于左箭头、右箭头和向上箭头。当我按住向右箭头时,我希望我的角色在被按住时继续向右移动,另一方面,如果你按下向上箭头,那么无论你是否按住它,你都只会跳一次。 所以我的问题是,例如,当我按住右箭头然后按下向上箭头时,touchesEnded 被调用并且它阻止我的角色向右移动,即使我的手指仍然放在右箭头上
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
for (UITouch *touch in touches){
CGPoint location = [touch locationInNode:self];
if (CGRectContainsPoint(rightArrow.frame, location)){
[wizard setTexture:[SKTexture textureWithImageNamed:@"wizardRight"]];
didTouchRightArrow = YES;
isLookingRight = YES;
isLookingLeft = NO;
rightArrow.alpha = 0.5;
NSLog(@"Touching right");
}
if (CGRectContainsPoint(leftArrow.frame, location)){
[wizard setTexture:[SKTexture textureWithImageNamed:@"wizardLeft"]];
isLookingRight = NO;
isLookingLeft = YES;
didTouchLeftArrow = YES;
leftArrow.alpha = 0.5;
NSLog(@"Touching left");
}
if (CGRectContainsPoint(upArrow.frame, location)){
didTouchUpArrow = YES;
upArrow.alpha = 0.5;
NSLog(@"Touching up");
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
if (rightArrow.alpha != 1.0){
rightArrow.alpha = 1.0;
}
if (leftArrow.alpha != 1.0){
leftArrow.alpha = 1.0;
}
if (upArrow.alpha != 1.0){
upArrow.alpha = 1.0;
for (UITouch *touch in touches){
CGPoint location = [touch locationInNode:self];
if (CGRectContainsPoint(rightArrow.frame, location)){
NSLog(@"Touching right");
didTouchRightArrow = YES;
} {
NSLog(@"Not touching right");
didTouchRightArrow = NO;
}
if (CGRectContainsPoint(leftArrow.frame, location)){
NSLog(@"Touching Left");
didTouchLeftArrow = YES;
} else {
NSLog(@"not touching left");
didTouchLeftArrow = NO;
}
didTouchUpArrow = NO;
}
这可能不是解决问题的正确方法,但在 touchesEnded 中,我试图查看触摸是否仍在所需的 Rect 中。
您需要一种方法来识别正在注册触摸的不同节点。有不止一种方法可以做到这一点,但我总是发现使用节点的名称 属性 是最简单易用的。
您已经有了正确的想法,使用 BOOL 来注册触摸状态。
我写了一些代码来处理你想要完成的事情:
#import "GameScene.h"
@implementation GameScene {
SKSpriteNode *node0;
SKSpriteNode *node1;
BOOL node0touch;
BOOL node1touch;
}
-(void)didMoveToView:(SKView *)view {
self.backgroundColor = [SKColor blackColor];
node0 = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(100, 100)];
node0.name = @"node0";
node0.position = CGPointMake(100, 300);
[self addChild:node0];
node1 = [SKSpriteNode spriteNodeWithColor:[SKColor blueColor] size:CGSizeMake(100, 100)];
node1.name = @"node1";
node1.position = CGPointMake(400, 300);
[self addChild:node1];
node0touch = false;
node1touch = false;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
CGPoint touchLocation = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:touchLocation];
if([node.name isEqualToString:@"node0"])
node0touch = true;
if([node.name isEqualToString:@"node1"])
node1touch = true;
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
CGPoint touchLocation = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:touchLocation];
if([node.name isEqualToString:@"node0"])
node0touch = false;
if([node.name isEqualToString:@"node1"])
node1touch = false;
}
}
-(void)update:(CFTimeInterval)currentTime {
if(node0touch)
NSLog(@"node0 touch");
if(node1touch)
NSLog(@"node1 touch");
}