2016-09-02 2 views
1

누구나 나를 도울 수 있습니까? 당신이 화면을 터치하고 4 개의 이미지가 무작위로 표시되는 간단한 앱을 만들려고 노력하고 있습니다. 문제는 화면을 탭해도 이미지가 표시되지 않으며 오류가 발생하지 않습니다. 도움!화면을 터치하면 왜 이미지가 나타나지 않습니까?

import SpriteKit 

class GameScene: SKScene { 

    var num1 = SKSpriteNode(imageNamed: "num1") 
    var num2 = SKSpriteNode(imageNamed: "num2") 
    var num3 = SKSpriteNode(imageNamed: "num3") 
    var num4 = SKSpriteNode(imageNamed: "num4") 

    override func didMoveToView(view: SKView) { 

     self.physicsWorld.gravity = CGVectorMake(0, -9.8) 
     num1.size = CGSize(width: 150, height: 150) 
     num2.size = CGSize(width: 150, height: 150) 
     num3.size = CGSize(width: 150, height: 150) 
     num4.size = CGSize(width: 150, height: 150) 

     let sceneBody = SKPhysicsBody(edgeLoopFromRect: self.frame) 
     sceneBody.friction = 0 
     self.physicsBody = sceneBody 
    } 

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 

     for touch: AnyObject in touches { 
       _ = touch.locationInNode(self) 

      let random = arc4random_uniform(4) 

      switch random { 
      case 0: 
       self.addChild(num1) 
       print("number 0") 
      case 1: 
       self.addChild(num2) 
       print("number 1") 
      case 2: 
       self.addChild(num3) 
       print("number 2") 
      default: 
       self.addChild(num4) 
       print("default") 
      } 
     } 
    } 
} 
+0

어디에 추가해야합니까? CGPoint로 자신의 위치를 ​​지정하지 않았습니다. addChild()에 접두사를 붙일 필요는 없습니다. 또한 알 수는 없지만 동일한 스프라이트로 무작위가 두 번 발생하면 동일한 스프라이트를 두 번 추가하므로 num1에 이미 부모가 있다는 오류가 표시됩니다. –

+0

흠, 나는 touchesBegan 함수에 코드를 넣었을 때 화면이 터치 될 때만 인식한다는 것을 알았습니다. 말해 주셔서 감사합니다. –

+0

예, 화면을 터치 한 경우에만 4 개의 변수 중 하나가 추가됩니다. 화면을 다시 터치하면 4 가지 변수 중 하나가 다시 추가됩니다. 화면을 계속 누르고 있으면 NSException이 발생합니다. 이미 부모가있는 노드를 추가하려고 시도합니다. –

답변

1

각 스프라이트가이

numX.position = location 

같은 것을 간부 인과 touchesBegan를 재 배열한다 각 self.addChild(...) 전에

를 표시 할 위치를 설정되지 않은 : 여기

내 코드입니다 이 방법은

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    var location 

    for touch in touches { 
     location = touch.locationInNode(self) 
    } 

    let random = arc4random_uniform(4) 

    switch random { 
    case 0: 
     num1.position = location 
     self.addChild(num1) 
     print("number 0") 
    case 1: 
     num2.position = location 
     self.addChild(num2) 
     print("number 1") 
    case 2: 
     num3.position = location 
     self.addChild(num3) 
     print("number 2") 
    default: 
     num4.position = location 
     self.addChild(num4) 
     print("default") 
    } 

} 
+0

고맙습니다. 테스트 할게요. 에는 anyObject 멤버가 없습니다라는 오류가 있습니다. –

+0

어떤 스위프트 버전을 사용하고 있습니까? – ddb

+0

이것이 무슨 뜻인지는 모르지만 Xcode 7.3.1을 사용하고 있습니다. –

0
class GameScene: SKScene { 

var handbag = SKSpriteNode(imageNamed: "HandBag") 
var bible = SKSpriteNode(imageNamed: "Bible") 
var watchtower = SKSpriteNode(imageNamed: "Watchtower") 
var awake = SKSpriteNode(imageNamed: "Awake") 

override func didMoveToView(view: SKView) { 

    self.physicsWorld.gravity = CGVectorMake(0, -9.8) 
    handbag.size = CGSize(width: 150, height: 150) 
    bible.size = CGSize(width: 150, height: 150) 
    watchtower.size = CGSize(width: 150, height: 150) 
    awake.size = CGSize(width: 150, height: 150) 

    let sceneBody = SKPhysicsBody(edgeLoopFromRect: self.frame) 
    sceneBody.friction = 0 
    self.physicsBody = sceneBody 
} 

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 

     for touch in touches { 
      let location = touch.locationInNode(self) 
     } 
     let random = arc4random_uniform(4) 

     switch random { 
     case 0: 
      handbag.position = location 
      self.addChild(handbag) 
      print("number 0") 
     case 1: 
      bible.position = touch 
      self.addChild(bible) 
      print("number 1") 
     case 2: 
      watchtower.position = touch 
      self.addChild(watchtower) 
      print("number 2") 
     default: 
      awake.position = touch 
      self.addChild(awake) 
      print("default") 
     } 
    } 
} 
관련 문제