2015-01-10 1 views
0

다음은 3x5 크기의 구형 그리드의 장면 키트 프로젝트입니다. 파란색 구체를 가볍게 터치 한 다음 주황색 구를 두드리면 그 위치로 이동합니다. 문제는 운동 표시가 된 구를 식별하는 방법을 모른다는 것입니다. Blue1 또는 Blue2를 움직일 수있는 구로 만 표시 할 수 있습니다. 다음은 iOS 시뮬레이터와 .dae 파일의 구성입니다. 주황색 구체를 빈 공간으로 간주하십시오.SceneKit 장면에서 이전에 표시된 객체 식별

import SceneKit 

class GameViewController: UIViewController { 

enum Type { case Orange; case Blue } 
struct BoardLoc { var x: Int; var y: Int } 
var selectedLoc: BoardLoc? 
var board: Array<Array<Type>> = [] 

override func viewDidLoad() { super.viewDidLoad() 

    // add a scene 
    let scene = SCNScene(named: "art.scnassets/balls10.dae") 
    let scnView = view as SCNView 
    scnView.scene = scene 
    scnView.autoenablesDefaultLighting = true 

    // add a tapper 
    let gestures = NSMutableArray() 
    let tapper = UITapGestureRecognizer(target: self, action: "handleTap:") 
    gestures.addObject(tapper) 
    scnView.gestureRecognizers = gestures 

    // add a 3 by 5 array, a board with most spaces empty except for 2 places 
    for x in 0...2 { 
     board.append(Array(count:5, repeatedValue:Type.Orange)) 
     for y in 0...4 { 
      if x == 2 && y == 0 || x == 2 && y == 3 { 
       board[x][y] = Type.Blue 
      } 
      else { 
       board[x][y] = Type.Orange 
      } 
     } 
    } 
} 

// handle tap 
func handleTap(gesture: UIGestureRecognizer) { 
    let scnView = view as SCNView 
    let point = gesture.locationInView(scnView) 
    if let hitResults = scnView.hitTest(point, options: nil) { 
     if hitResults.count > 0 { 
      let result: AnyObject! = hitResults[0] 
      let tapLoc = BoardLoc(x: Int(point.x)/106, y: Int(point.y)/113) 

      // if tapLoc blue, selectedLoc set as tapLoc 
      if board[tapLoc.x][tapLoc.y] == Type.Blue { 
       selectedLoc = tapLoc 
      } 

      // if tapLoc orange and selectedLoc not nil 
      if board[tapLoc.x][tapLoc.y] == Type.Orange && selectedLoc != nil { 

       // here is where I fail by just marking Blue1 for movement 
       let ball = scnView.scene!.rootNode.childNodeWithName("Blue1", recursively: true) 

       // placement of ball according to 3 by 5 grid 
       let locX = Float(tapLoc.x) * 16.4 + 0.75 
       let locY = Float(tapLoc.y) * -16.4 - 1.25 

       let moveToTapLoc = SCNAction.moveTo(SCNVector3(x: locX, y: locY, z: 5), duration: 0.01) 
       ball!.runAction(moveToTapLoc) 

       // update array 
       board[tapLoc.x][tapLoc.y] = Type.Blue 
       board[selectedLoc!.x][selectedLoc!.y] = Type.Orange 

       // set selectedLoc back to nil 
       selectedLoc = nil 
      } 
     } 
    } 
} 
} 

내가 탭 구를 표시 할이 이동하는 방법 : enter image description here 또한

, 여기에 코드?

나는 시도했다! 볼 .runAction 오류가 발생하면

  if board[tapLoc.x][tapLoc.y] == Type.Blue { 
       selectedLoc = tapLoc 
       var ball = !result.node!.name!.hasPrefix("Blue") 
      } 

      // if tapLoc orange and selectedLoc not nil 
      if board[tapLoc.x][tapLoc.y] == Type.Orange && selectedLoc != nil { 


       // placement of ball according to 3 by 5 grid 
       let locX = Float(tapLoc.x) * 16.4 + 0.75 
       let locY = Float(tapLoc.y) * -16.4 - 1.25 

       let moveToTapLoc = SCNAction.moveTo(SCNVector3(x: locX, y: locY, z: 5), duration: 0.01) 
       ball!.runAction(moveToTapLoc) 

       // update array 
       board[tapLoc.x][tapLoc.y] = Type.Blue 
       board[selectedLoc!.x][selectedLoc!.y] = Type.Orange 

       // set selectedLoc back to nil 
       selectedLoc = nil 
      } 

는 : 해결되지 않은 식별자 '볼'의 사용의 viewDidLoad가 돈을 공 운동 표시된 사용하지만 전에

어쩌면 변수를 추가 변수의 레이블을 지정하는 유형이나 방법을 알지 못합니다.

답변

0

이렇게하려면 한 가지 방법은 selectedLoc을 selectedLocBlue1로 변경하고 유사한 변수 인 selectedLocBlue2를 추가하는 것입니다. 그런 다음 handleTap 함수에 2 개의 if 문 대신 4 개의 if 문을 사용하고 2는 selectedLocBlue1에 대해 2를, selectedLocBlue2에 대해서는 2를 사용합니다. 또한 2 개의 다른 유형의 파란색이 있고 변경 열거 형이 Orange, Blue1, Blue2의 3 가지 유형이 있다는 것을 반영하도록 배열을 변경하십시오. 이것은 확실한 방법입니다. 비평하십시오. 감사.

관련 문제