在SKScene中设置按钮

在SKScene中设置按钮

我发现它UIButtons不能很好地工作SKScene,所以我试图子类化SKNode来制作一个按钮SpriteKit

我希望它工作的方式是,如果我初始化一个按钮SKScene并启用触摸事件,那么按钮将在我SKScene按下时调用我的方法。

我很感激任何能让我找到解决这个问题的建议。谢谢。


HUH函数
浏览 705回答 3
3回答

慕容3067478

我已经制作了自己的Button-Class,我正在使用它。SKButton.h:#import <SpriteKit/SpriteKit.h>@interface SKButton : SKSpriteNode@property (nonatomic, readonly) SEL actionTouchUpInside;@property (nonatomic, readonly) SEL actionTouchDown;@property (nonatomic, readonly) SEL actionTouchUp;@property (nonatomic, readonly, weak) id targetTouchUpInside;@property (nonatomic, readonly, weak) id targetTouchDown;@property (nonatomic, readonly, weak) id targetTouchUp;@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;- (id)initWithTextureNormal:(SKTexture *)normal selected:(SKTexture *)selected;- (id)initWithTextureNormal:(SKTexture *)normal selected:(SKTexture *)selected disabled:(SKTexture *)disabled; // Designated Initializer- (id)initWithImageNamedNormal:(NSString *)normal selected:(NSString *)selected;- (id)initWithImageNamedNormal:(NSString *)normal selected:(NSString *)selected disabled:(NSString *)disabled;/** Sets the target-action pair, that is called when the Button is tapped.&nbsp;"target" won't be retained.&nbsp;*/- (void)setTouchUpInsideTarget:(id)target action:(SEL)action;- (void)setTouchDownTarget:(id)target action:(SEL)action;- (void)setTouchUpTarget:(id)target action:(SEL)action;@endSKButton.m:#import "SKButton.h"#import <objc/message.h>@implementation SKButton#pragma mark Texture Initializer/**&nbsp;* Override the super-classes designated initializer, to get a properly set SKButton in every case&nbsp;*/- (id)initWithTexture:(SKTexture *)texture color:(UIColor *)color size:(CGSize)size {&nbsp; &nbsp; return [self initWithTextureNormal:texture selected:nil disabled:nil];}- (id)initWithTextureNormal:(SKTexture *)normal selected:(SKTexture *)selected {&nbsp; &nbsp; return [self initWithTextureNormal:normal selected:selected disabled:nil];}/**&nbsp;* This is the designated Initializer&nbsp;*/- (id)initWithTextureNormal:(SKTexture *)normal selected:(SKTexture *)selected disabled:(SKTexture *)disabled {&nbsp; &nbsp; self = [super initWithTexture:normal color:[UIColor whiteColor] size:normal.size];&nbsp; &nbsp; if (self) {&nbsp; &nbsp; &nbsp; &nbsp; [self setNormalTexture:normal];&nbsp; &nbsp; &nbsp; &nbsp; [self setSelectedTexture:selected];&nbsp; &nbsp; &nbsp; &nbsp; [self setDisabledTexture:disabled];&nbsp; &nbsp; &nbsp; &nbsp; [self setIsEnabled:YES];&nbsp; &nbsp; &nbsp; &nbsp; [self setIsSelected:NO];&nbsp; &nbsp; &nbsp; &nbsp; _title = [SKLabelNode labelNodeWithFontNamed:@"Arial"];&nbsp; &nbsp; &nbsp; &nbsp; [_title setVerticalAlignmentMode:SKLabelVerticalAlignmentModeCenter];&nbsp; &nbsp; &nbsp; &nbsp; [_title setHorizontalAlignmentMode:SKLabelHorizontalAlignmentModeCenter];&nbsp; &nbsp; &nbsp; &nbsp; [self addChild:_title];&nbsp; &nbsp; &nbsp; &nbsp; [self setUserInteractionEnabled:YES];&nbsp; &nbsp; }&nbsp; &nbsp; return self;}#pragma mark Image Initializer- (id)initWithImageNamedNormal:(NSString *)normal selected:(NSString *)selected {&nbsp; &nbsp; return [self initWithImageNamedNormal:normal selected:selected disabled:nil];}- (id)initWithImageNamedNormal:(NSString *)normal selected:(NSString *)selected disabled:(NSString *)disabled {&nbsp; &nbsp; SKTexture *textureNormal = nil;&nbsp; &nbsp; if (normal) {&nbsp; &nbsp; &nbsp; &nbsp; textureNormal = [SKTexture textureWithImageNamed:normal];&nbsp; &nbsp; }&nbsp; &nbsp; SKTexture *textureSelected = nil;&nbsp; &nbsp; if (selected) {&nbsp; &nbsp; &nbsp; &nbsp; textureSelected = [SKTexture textureWithImageNamed:selected];&nbsp; &nbsp; }&nbsp; &nbsp; SKTexture *textureDisabled = nil;&nbsp; &nbsp; if (disabled) {&nbsp; &nbsp; &nbsp; &nbsp; textureDisabled = [SKTexture textureWithImageNamed:disabled];&nbsp; &nbsp; }&nbsp; &nbsp; return [self initWithTextureNormal:textureNormal selected:textureSelected disabled:textureDisabled];}#pragma -#pragma mark Setting Target-Action pairs- (void)setTouchUpInsideTarget:(id)target action:(SEL)action {&nbsp; &nbsp; _targetTouchUpInside = target;&nbsp; &nbsp; _actionTouchUpInside = action;}- (void)setTouchDownTarget:(id)target action:(SEL)action {&nbsp; &nbsp; _targetTouchDown = target;&nbsp; &nbsp; _actionTouchDown = action;}- (void)setTouchUpTarget:(id)target action:(SEL)action {&nbsp; &nbsp; _targetTouchUp = target;&nbsp; &nbsp; _actionTouchUp = action;}#pragma -#pragma mark Setter overrides- (void)setIsEnabled:(BOOL)isEnabled {&nbsp; &nbsp; _isEnabled = isEnabled;&nbsp; &nbsp; if ([self disabledTexture]) {&nbsp; &nbsp; &nbsp; &nbsp; if (!_isEnabled) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [self setTexture:_disabledTexture];&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [self setTexture:_normalTexture];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}- (void)setIsSelected:(BOOL)isSelected {&nbsp; &nbsp; _isSelected = isSelected;&nbsp; &nbsp; if ([self selectedTexture] && [self isEnabled]) {&nbsp; &nbsp; &nbsp; &nbsp; if (_isSelected) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [self setTexture:_selectedTexture];&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [self setTexture:_normalTexture];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}#pragma -#pragma mark Touch Handling/**&nbsp;* This method only occurs, if the touch was inside this node. Furthermore if&nbsp;&nbsp;* the Button is enabled, the texture should change to "selectedTexture".&nbsp;*/- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {&nbsp; &nbsp; if ([self isEnabled]) {&nbsp; &nbsp; &nbsp; &nbsp; objc_msgSend(_targetTouchDown, _actionTouchDown);&nbsp; &nbsp; &nbsp; &nbsp; [self setIsSelected:YES];&nbsp; &nbsp; }}/**&nbsp;* If the Button is enabled: This method looks, where the touch was moved to.&nbsp;* If the touch moves outside of the button, the isSelected property is restored&nbsp;* to NO and the texture changes to "normalTexture".&nbsp;*/- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {&nbsp; &nbsp; if ([self isEnabled]) {&nbsp; &nbsp; &nbsp; &nbsp; UITouch *touch = [touches anyObject];&nbsp; &nbsp; &nbsp; &nbsp; CGPoint touchPoint = [touch locationInNode:self.parent];&nbsp; &nbsp; &nbsp; &nbsp; if (CGRectContainsPoint(self.frame, touchPoint)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [self setIsSelected:YES];&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [self setIsSelected:NO];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}/**&nbsp;* If the Button is enabled AND the touch ended in the buttons frame, the&nbsp;* selector of the target is run.&nbsp;*/- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {&nbsp; &nbsp; UITouch *touch = [touches anyObject];&nbsp; &nbsp; CGPoint touchPoint = [touch locationInNode:self.parent];&nbsp; &nbsp; if ([self isEnabled] && CGRectContainsPoint(self.frame, touchPoint)) {&nbsp; &nbsp; &nbsp; &nbsp; objc_msgSend(_targetTouchUpInside, _actionTouchUpInside);&nbsp; &nbsp; }&nbsp; &nbsp; [self setIsSelected:NO];&nbsp; &nbsp; objc_msgSend(_targetTouchUp, _actionTouchUp);}示例:要初始化按钮,请编写以下行:&nbsp; &nbsp; SKButton *backButton = [[SKButton alloc] initWithImageNamedNormal:@"buttonNormal" selected:@"buttonSelected"];&nbsp; &nbsp; [backButton setPosition:CGPointMake(100, 100)];&nbsp; &nbsp; [backButton.title setText:@"Button"];&nbsp; &nbsp; [backButton.title setFontName:@"Chalkduster"];&nbsp; &nbsp; [backButton.title setFontSize:20.0];&nbsp; &nbsp; [backButton setTouchUpInsideTarget:self action:@selector(buttonAction)];&nbsp; &nbsp; [self addChild:backButton];此外,您需要在班级中使用'buttonAction'方法。 *不保证本课程在每种情况下都能正常使用。我对Objective-c还很新。*如果您认为有做,这是恼人的和毫无意义的,你可以通过设置禁用构建设置检查“启用严格的检查objc_msgSend Calls',以” No'
打开App,查看更多内容
随时随地看视频慕课网APP