2015-03-25 52 views
0

我用这个问题的解决方案SpriteKit Objective-C: programming directional pad controls以及从这里Correct way to create button in Sprite Kit?的一个版本的自定义类按钮与省略目标行动方法来创建我的SpriteKit游戏的虚拟控制按钮。所以这里是问题所在:我试图实现的是两组彼此无关的按钮(例如,左侧的D-pad和右侧的右侧的按钮)以及能够在相同位置使用它们的能力可以在多点触摸的帮助下进行,就像在右侧有动作按钮和左侧有Dirs的硬件控制器上一样。所以我们来想象一下这种情况:一种方法来检测哪些按钮应该被释放

1)我先拿着一个方向按钮和一个开火按钮。然后,将我的左手指拖到方向按钮外面,这会触发if(node.name)的ELSE语句,它将所有按钮的“选定”布尔设置为NO。这不是我想要的,因为火按钮仍然保持着,它应该保持它的“选定”状态,而方向按钮 - 不应该。 2)相同的情况,但是如果我将右手指放在火按钮外面,并保持按住方向按钮。两个按钮都会被取消选中,当按下按钮时不应该这样做。

那么,我该如何正确实现它,以一种方式来检测,哪个按钮完全取消选择“取消选择”只是特定的一组按钮?请把我推向正确的方向。希望我所要求的是可能的。

当前代码(这实在是太乱了,原谅大猩猩式编码):

DirectionalButton.h

#import <SpriteKit/SpriteKit.h> 
@interface DirectionalButton : SKSpriteNode 

@property (nonatomic) BOOL isEnabled; 
@property (nonatomic) BOOL isSelected; 
@property (nonatomic, readonly, strong) SKLabelNode *title; 
@property (nonatomic, readwrite, strong) SKTexture *normalTexture; 
@property (nonatomic, readwrite, strong) SKTexture *selectedTexture; 
@property (nonatomic, readwrite, strong) SKTexture *disabledTexture; 

- (instancetype)initWithTextureNormal:(SKTexture *)normal selected:(SKTexture *)selected; 

- (instancetype)initWithTextureNormal:(SKTexture *)normal selected:(SKTexture *)selected disabled:(SKTexture *)disabled; 

- (instancetype)initWithImageNamedNormal:(NSString *)normal selected:(NSString *)selected; 
- (instancetype)initWithImageNamedNormal:(NSString *)normal selected:(NSString *)selected disabled:(NSString *)disabled; 

@end 

DirectionalButton.m

#import "DirectionalButton.h" 



@implementation DirectionalButton 

#pragma mark Texture Initializer 

- (instancetype)initWithTexture:(SKTexture *)texture color:(UIColor *)color size:(CGSize)size { 
    return [self initWithTextureNormal:texture selected:nil disabled:nil]; 
} 

- (instancetype)initWithTextureNormal:(SKTexture *)normal selected:(SKTexture *)selected { 
    return [self initWithTextureNormal:normal selected:selected disabled:nil]; 
} 

- (instancetype)initWithTextureNormal:(SKTexture *)normal selected:(SKTexture *)selected disabled:(SKTexture *)disabled { 
    self = [super initWithTexture:normal color:[UIColor whiteColor] size:normal.size]; 
    if (self) { 
     [self setNormalTexture:normal]; 
     [self setSelectedTexture:selected]; 
     [self setDisabledTexture:disabled]; 
     [self setIsEnabled:YES]; 
     [self setIsSelected:NO]; 

     _title = [SKLabelNode labelNodeWithFontNamed:@"Arial"]; 
     [_title setVerticalAlignmentMode:SKLabelVerticalAlignmentModeCenter]; 
     [_title setHorizontalAlignmentMode:SKLabelHorizontalAlignmentModeCenter]; 

     [self addChild:_title]; 
     [self setUserInteractionEnabled:YES]; 
    } 
    return self; 
} 

#pragma mark Image Initializer 

- (instancetype)initWithImageNamedNormal:(NSString *)normal selected:(NSString *)selected { 
    return [self initWithImageNamedNormal:normal selected:selected disabled:nil]; 
} 

- (instancetype)initWithImageNamedNormal:(NSString *)normal selected:(NSString *)selected disabled:(NSString *)disabled { 
    SKTexture *textureNormal = nil; 
    if (normal) { 
     textureNormal = [SKTexture textureWithImageNamed:normal]; 
    } 

    SKTexture *textureSelected = nil; 
    if (selected) { 
     textureSelected = [SKTexture textureWithImageNamed:selected]; 
    } 

    SKTexture *textureDisabled = nil; 
    if (disabled) { 
     textureDisabled = [SKTexture textureWithImageNamed:disabled]; 
    } 

    return [self initWithTextureNormal:textureNormal selected:textureSelected disabled:textureDisabled]; 
} 


#pragma - 
#pragma mark Setter overrides 

- (void)setIsEnabled:(BOOL)isEnabled { 
    _isEnabled = isEnabled; 
    if ([self disabledTexture]) { 
     if (!_isEnabled) { 
      [self setTexture:_disabledTexture]; 
     } else { 
      [self setTexture:self.normalTexture]; 
     } 
    } 
} 

- (void)setIsSelected:(BOOL)isSelected { 
    _isSelected = isSelected; 
    if ([self selectedTexture] && [self isEnabled]) { 
     if (_isSelected) { 
      [self setTexture:_selectedTexture]; 
      [self runAction:[SKAction fadeAlphaTo:0.55 duration:0.12f]]; 
     } else { 
      [self setTexture:self.normalTexture]; 
      [self runAction:[SKAction fadeAlphaTo:1 duration:0.12f]]; 
     } 
    } 
} 
@end 

GameScene.m

- (void)createHUD { 
//Executed in didMoveToView 
NSString *pathToImageForLeftButtonTexture = @"buttonDirectionalLeft"; 
NSString *pathToImageForRightButtonTexture = @"buttonDirectionalRight"; 
dirBtnLeft = [[DirectionalButton alloc]initWithImageNamedNormal:pathToImageForLeftButtonTexture selected:pathToImageForLeftButtonTexture]; 
/* 
    Set size and position for left 
*/ 
dirBtnLeft.name = @"directionalLeft"; 
dirBtnRight = [[DirectionalButton alloc]initWithImageNamedNormal:pathToImageForRightButtonTexture selected:pathToImageForRightButtonTexture]; 
/* 
    Set size and position accordingly to left button for right 
*/ 
dirBtnRight.name = @"directionalRight"; 
dirBtnLeftFrame = [SKSpriteNode spriteNodeWithColor:[SKColor clearColor] size:dirBtnLeft.size]; 
dirBtnLeftFrame.position = dirBtnLeft.position; 
dirBtnRightFrame = [SKSpriteNode spriteNodeWithColor:[SKColor clearColor] size:dirBtnRight.size]; 
fireBtn = [[DirectionalButton alloc]initWithImageNamedNormal:@"default.jpg" selected:@"default.jpg"]; 
/* 
    Set position and size for fire 
*/ 
fireBtnFrame = [SKSpriteNode spriteNodeWithColor:[SKColor clearColor] size:fireBtn.size]; 
fireBtnFrame.position = fireBtn.position; 
dirBtnRightFrame.position = dirBtnRight.position; 
dirBtnLeftFrame.zPosition = 100; 
dirBtnRightFrame.zPosition = 100; 
fireBtnFrame.zPosition = 100; 
dirBtnRight.zPosition = 99; 
dirBtnLeft.zPosition = 99; 
fireBtn.zPosition = 99; 
dirBtnLeftFrame.name = @"directionalLeftFrame"; 
dirBtnRightFrame.name = @"directionalRightFrame"; 
fireBtnFrame.name = @"primaryFire"; 
[self addChild:dirBtnLeft]; 
[self addChild:dirBtnRight]; 
[self addChild:dirBtnLeftFrame]; 
[self addChild:dirBtnRightFrame]; 
[self addChild:fireBtn]; 
[self addChild:fireBtnFrame];  
} 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [self touchesMoved:touches withEvent:event]; 
} 

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
for(UITouch *touch in [event allTouches]){ 
    CGPoint touchLocation = [touch locationInNode:self]; 
    SKNode *node = [self nodeAtPoint:touchLocation]; 


    if (node.name) 
    { 
     if ([node.name isEqualToString:@"directionalRightFrame"]) 
     { 

      if(!dirBtnRight.isSelected){ 
      [dirBtnRight setIsSelected:YES]; 
       [dirBtnLeft setIsSelected:NO];} 
     } 
     else if ([node.name isEqualToString:@"directionalLeftFrame"]) 
     { 
      if(!dirBtnLeft.isSelected){ 
      [dirBtnRight setIsSelected:NO]; 
       [dirBtnLeft setIsSelected:YES];} 
     } 
     else if ([node.name isEqualToString:@"primaryFire"]) { 
      if (!fireBtn.isSelected) { 
       [fireBtn setIsSelected:YES]; 
      } 
     } 
    } 
    else 
    { 
     [dirBtnRight setIsSelected:NO]; 
     [dirBtnLeft setIsSelected:NO]; 
     [fireBtn setIsSelected:NO]; 
    }} 
} 

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
for (UITouch *touch in [event allTouches]) { 
    CGPoint touchPoint = [touch locationInNode:self]; 
    SKNode *node = [self nodeAtPoint:touchPoint]; 
    if(node.name){ 
    if ([node.name isEqualToString:@"directionalLeftFrame"]) { 
dirBtnLeft.isSelected = NO;} 
else if ([node.name isEqualToString:@"directionalRightFrame"]) 
{ 
    dirBtnRight.isSelected = NO; 
} 
else if([node.name isEqualToString:@"primaryFire"]){ 
    fireBtn.isSelected = NO;} 
    } else 
    { 
     dirBtnLeft.isSelected = NO; 
     dirBtnRight.isSelected = NO; 
     fireBtn.isSelected = NO;}} 
} 

-(void)update:(CFTimeInterval)currentTime { 
    if(dirBtnLeft.isSelected){ 
     //Do something 
    } 
    else if(dirBtnRight.isSelected){ 
     //Do something else 
    } 
    else 
    { 
     //Stop doing anything 
    } 
} 
+0

你目前的代码是什么? – rakeshbs 2015-03-25 09:14:51

+0

@rakeshbs新增示例代码。不好意思,因为我仍然是ObjC的绝对初学者。 – TheDood 2015-03-25 10:03:24

回答

0

touchesEnded中,您正在循环SKScene中的所有活动触摸。
[event allTouches]返回所有活动的触摸。
而不是仅仅使用touches NSSet来循环实际结束的触摸。

请使用下面的代码代替touchesMovedtouchesEnded

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    for (UITouch *touch in touches) { 
      // Your conditions 
    } 
} 
+0

得到了以下问题(我只能在iOS模拟器中测试它,所以我放置了它,以便我可以在同时按住Alt键的同时):1)首先保存这两个目录。并在同一时间开火。 2)移动触摸,以便在右按钮不再按下时按下按钮。 3)两个按钮都被“取消选择”。这恰好发生在目录的1/3部分。按钮。但是,如果我先按下火按钮,而不是dir,问题就不会出现在那里。一。什么可能是错误的? – TheDood 2015-03-25 11:05:18

+0

那有什么问题? – rakeshbs 2015-03-25 11:06:32

+0

你是否改变了touchesMoved里面的代码呢? – rakeshbs 2015-03-25 11:26:04

相关问题