我用這個問題的解決方案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
}
}
你目前的代碼是什麼? – rakeshbs 2015-03-25 09:14:51
@rakeshbs新增示例代碼。不好意思,因爲我仍然是ObjC的絕對初學者。 – TheDood 2015-03-25 10:03:24