CcmenuItemToggle* musicbuttonToggle = [CcmenuItemToggle itemWithItems:[NSArray arrayWithObjects:soundbuttonOn,soundbuttonOff,nil] block:^(ID sender) { [self stopSounds]; }];
有人知道在SpriteKit上做这个的方法吗?
解决方法 基本切换按钮子类化SKLabelNode.H
typedef NS_ENUM(NSInteger,buttonState){ On,Off};@interface Togglebutton : SKLabelNode- (instancetype)initWithState:(buttonState) setUpState;- (voID) buttonpressed;@end
.M
#import "Togglebutton.h"@implementation Togglebutton{ buttonState _currentState;}- (ID)initWithState:(buttonState) setUpState{ if (self = [super init]) { _currentState = setUpState; self = [Togglebutton labelNodeWithFontnamed:@"Chalkduster"]; self.text = [self updateLabelForCurrentState]; self.FontSize = 30; } return self;}- (Nsstring *) updateLabelForCurrentState{ Nsstring *label; if (_currentState == On) { label = @"ON"; } else if (_currentState == Off) { label = @"OFF"; } return label;}- (voID) buttonpressed{ if (_currentState == Off) { _currentState = On; } else { _currentState = Off; } self.text = [self updateLabelForCurrentState];}@end
在场景中添加切换按钮
Togglebutton *myLabel = [Togglebutton new];myLabel = [myLabel initWithState:Off];myLabel.position = CGPointMake(CGRectGetMIDX(self.frame),CGRectGetMIDY(self.frame));[self addChild:myLabel];
检测触摸
-(voID)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UItouch* touch = [touches anyObject]; CGPoint loc = [touch locationInNode:self]; SKNode *node = [self nodeAtPoint:loc]; if ([node isKindOfClass:[Togglebutton class]]) { Togglebutton *btn = (Togglebutton*) node; [btn buttonpressed]; }}总结
以上是内存溢出为你收集整理的ios – 如何在SpriteKit上创建一个切换按钮全部内容,希望文章能够帮你解决ios – 如何在SpriteKit上创建一个切换按钮所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)