2015-04-24 46 views
2

我知道如何在SpriteKit中添加Button。如何在SpriteKit中添加突出顯示的按鈕?

我已用SKSpriteNode完成。 但是我不知道如何添加突出顯示按鈕,如UIButton

我的意思是當我按SKSpriteNode(Button),我想從UIKit顯示像UIButton高亮顯示的圖像。

我該怎麼做?

這裏是我的代碼ButtonSKSpriteNode

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]; 

回答

3

我嘗試和得到了什麼你究竟在尋找,

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"]]; 
     } 
    } 
+0

這個代碼從場景的作品?如果我需要單獨的課程的按鈕按鈕? –

+0

看看這個http://stackoverflow.com/questions/19082202/setting-up-buttons-in-skscene –

+0

希望它有助於:) –

1

您可以使用方法-(void)didMoveToView:(SKView *)view中的視圖屬性在SK中創建一個UIButton。

所有的UIButton功能,包括突出顯示都可以訪問。

2

一種解決方案是創建兩個SKSpriteNodes,一個用於活動狀態,一個用於默認狀態,並將它們兩個添加到您的視圖中。

在touchesMovedMovedEndchesEnded方法中,相應地隱藏和取消隱藏活動按鈕和默認按鈕。

Here's在Swift中的一個教程。