2014-09-23 6 views
1

저는 Swift and Sprite Kit를 처음 사용합니다.스프라이트 키트 버튼의 이미지를 업데이트하는 방법은 무엇입니까?

MyScene.swift에서 특정 이미지가있는 단추를 만들려고합니다. 버튼을 클릭 할 때마다 새 이미지로 버튼 이미지를 변경하고 싶습니다. 어떻게하면됩니까?

GameScene.swift 

class GameScene: SKScene { 
var rollButton:SKSpriteNode = SKSpriteNode(imageNamed: "dice1.png") 

override func didMoveToView(view: SKView) { 

rollButton.position = CGPoint(x: 79, y: 290) //this position is based to what? Is there an easier way to put a button using storyborard 

    rollButton.setScale(0.5) //what does setScale mean? 
self.addChild(rollButton) 
} 

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { 
    for touch: AnyObject in touches { 
    if CGRectContainsPoint(rollButton.frame, touch.locationInNode(self)) { 
     changeButtonImage() 
    } 
    } 
} 

func changeButtonImage() { 
//radom get value beteween 1 - 6 
change the rollButton image with a new value??? //how to do that 
} 
  1. 내가 연결이 스토리 보드에 신속 GameScene을 스토리 보드에있는 UIButton을 추가하고보다 방법이 있나요? (자식 노드를 추가하면 프로그래밍 방식으로 매우 어려울 수 있습니다.)
  2. 노드의 CGPoint는 무엇을 기반으로합니까? 현재 UIView? 예를 들어, I는 화상이있는 경우, 프로그램의 다른 이미지 ( SpriteKit 노드를 사용하여 신속한에서) 버튼의 형상을 변경하는 방법
  3. : dice1, dice2, dice3을 ...

이 대물 인 C 코드 :

NSString *fileName = [NSString stringWithFormat:@"dice%d.png", num]; 
// Setting the uiimageview to the appropriate image 
self.rollButton.image = [UIImage imageNamed:fileName]; 

내가 storyborard에서 버튼을 설정하는 방법에와 생성 된 각각의 새로운 GameScene에 uiviewcontrollers를 사용하는 것보다 어떤 좋은 예를 찾을 수 없습니다,

답변

3

우선,는 A를 만들 원하는 이미지의 rray는 버튼이하기 :

let arrayOfImages = [ 
    "image1.png", 
    "image2.png", 
    "image3.png", 
    "image4.png", 
    "image5.png", 
    "image6.png" 
] 

그런 다음 버튼 스프라이트를 초기화하고 시작하는 그것을 질감을 제공 :

var rollButton = SKSpriteNode() 
rollButton.texture = SKTexture(imageNamed: arrayOfImages[0]) 

그런 다음, 호출 할 때마다 changeButtonImage()는, 임의의 숫자를 생성 당신이 버튼 또는 일반 사용을위한 스위치를 구축하려는 경우

var randomNumberBetween0And5 = Int(arc4random_uniform(6)) 
rollButton.texture = SKTexture(imageNamed: arrayOfImages[randomNumberBetween0And5]) 
1

, 당신은 아주 정직하고, 사용을 내가 만든이 컨트롤을 시도해야하고 버튼의 질감을 변경 : 당신은 단지 버튼의 종류를 초기화/당신이 (ColoredSprite, 질감 또는 TextOnly)

let control = TWButton(normalColor: SKColor.blueColor(), highlightedColor: SKColor.redColor(), size: CGSize(width: 160, height: 80)) 

원하는 그리고 초기화 후 당신이 그것에 클로저를 추가 스위치의

control.addClosureFor(.TouchUpInside, target: self, closure: { (scene, sender) ->() in 
      scene.testProperty = "Changed Property" 
     }) 
    } 

(있는 UIButton에 addTargetForSelector 등) 그것! https://github.com/txaidw/TWControls

그러나 특정 노드에 구현하려는 경우 여기에 내가 한 방법은 다음과 같습니다 :

internal override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { 
    let touch = touches.first as! UITouch 
    let touchPoint = touch.locationInNode(self.parent) 

    if self.containsPoint(touchPoint) { 
     self.touchLocationLast = touchPoint 
     touchDown() 
    } 
} 

internal override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) { 
    let touch = touches.first as! UITouch 
    let touchPoint = touch.locationInNode(self.parent) 
    self.touchLocationLast = touchPoint 
} 


internal override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) { 
    let touch = touches.first as! UITouch 
    let touchPoint = touch.locationInNode(self.parent) 

     if let lastPoint = self.touchLocationLast where self.containsPoint(lastPoint) { 
      // Ended inside 
      touchUpInside() 
     } 
     else { 
      // Ended outside 
      touchUpOutside() 
     } 
} 
GitHub의 페이지에서 추가 정보 절에 대한 추가 정보가 있습니다