如何在 SpriteKit 中添加高亮按钮?
How to add Highlighted Button in SpriteKit?
我知道如何在 SpriteKit
中添加按钮。
我已经完成 SKSpriteNode
。
但是我不知道如何添加像 UIButton
.
这样的高亮按钮
我的意思是当我按下 SKSpriteNode(Button)
时,我想显示像 UIButton
来自 UIKit 的高亮图像。
我该怎么做?
这是我的 Button
和 SKSpriteNode
的代码。
self.playButton = [SKSpriteNode spriteNodeWithImageNamed:@"playButton.png"];
self.playButton.position = CGPointMake(self.frame.origin.x + 400, 100);
self.playButton.name = @"playButton";
self.playButton.zPosition = 2;
[self addChild:self.playButton];
您可以在 -(void)didMoveToView:(SKView *)view
.
方法中使用视图 属性 在 SK 中创建一个 UIButton
所有 UIButton 功能,包括突出显示都将可用。
一种解决方案是创建两个 SKSpriteNode,一个用于活动状态,一个用于默认状态,并将它们都添加到您的视图中。
在您的 touchesMoved 和 touchesEnded 方法中,相应地隐藏和取消隐藏活动按钮和默认按钮。
Here's Swift 中的教程。
我试过了,得到了你想要的东西,
BOOL isMySpriteNodeTouched = NO;
mySprite = [[SKSpriteNode alloc]initWithImageNamed:@"ball"];
mySprite.position = CGPointMake(self.size.width/2.0f, self.size.height/2.0f);
mySprite.name = @"mySprite";
[self addChild:mySprite];
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches)
{
CGPoint touchPoint = [touch locationInNode:self];
SKNode *localNode = [[SKNode alloc]init];
localNode = [self nodeAtPoint:touchPoint];
if ([localNode.name isEqualToString:mySprite.name])
{
mySprite.texture = [SKTexture textureWithImage:[UIImage imageNamed:@"highlightedBall"]];
isMySpriteNodeTouched = YES;
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if(isMySpriteNodeTouched)
{
isMySpriteNodeTouched = !isMySpriteNodeTouched;
mysprite.texture = [SKTexture textureWithImage:[UIImage imageNamed:@"ball"]];
}
}
我知道如何在 SpriteKit
中添加按钮。
我已经完成 SKSpriteNode
。
但是我不知道如何添加像 UIButton
.
我的意思是当我按下 SKSpriteNode(Button)
时,我想显示像 UIButton
来自 UIKit 的高亮图像。
我该怎么做?
这是我的 Button
和 SKSpriteNode
的代码。
self.playButton = [SKSpriteNode spriteNodeWithImageNamed:@"playButton.png"];
self.playButton.position = CGPointMake(self.frame.origin.x + 400, 100);
self.playButton.name = @"playButton";
self.playButton.zPosition = 2;
[self addChild:self.playButton];
您可以在 -(void)didMoveToView:(SKView *)view
.
所有 UIButton 功能,包括突出显示都将可用。
一种解决方案是创建两个 SKSpriteNode,一个用于活动状态,一个用于默认状态,并将它们都添加到您的视图中。
在您的 touchesMoved 和 touchesEnded 方法中,相应地隐藏和取消隐藏活动按钮和默认按钮。
Here's Swift 中的教程。
我试过了,得到了你想要的东西,
BOOL isMySpriteNodeTouched = NO;
mySprite = [[SKSpriteNode alloc]initWithImageNamed:@"ball"];
mySprite.position = CGPointMake(self.size.width/2.0f, self.size.height/2.0f);
mySprite.name = @"mySprite";
[self addChild:mySprite];
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches)
{
CGPoint touchPoint = [touch locationInNode:self];
SKNode *localNode = [[SKNode alloc]init];
localNode = [self nodeAtPoint:touchPoint];
if ([localNode.name isEqualToString:mySprite.name])
{
mySprite.texture = [SKTexture textureWithImage:[UIImage imageNamed:@"highlightedBall"]];
isMySpriteNodeTouched = YES;
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if(isMySpriteNodeTouched)
{
isMySpriteNodeTouched = !isMySpriteNodeTouched;
mysprite.texture = [SKTexture textureWithImage:[UIImage imageNamed:@"ball"]];
}
}