2016-08-19 1 views
1

"kick"이라는 함수를 호출하려고합니다. 그 안에는 제가 만든이 남자와 놀고있는 CAAnimation이 있습니다. 그래서 보통은 내가 만든 것과 동일한 뷰 컨트롤러에서 함수를 호출 할 것입니다.하지만 HUD/오버레이 장면이 있기 때문에 3D 게임을 통해 표시됩니다. 그리고 HUD/overlayscene은 다른 뷰 컨트롤러에서 HUD를 설정하기 위해 만들어졌습니다. HUD보기 컨트롤러를 통해 메인 뷰 컨트롤러에서 모든 기능을 인식 할 수 없습니다.다른보기 컨트롤러에 정의 된 함수를 호출하는 방법은 무엇입니까?

MainViewController :
코드 :

class GameViewController: UIViewController, ADBannerViewDelegate, SKPhysicsContactDelegate, SKSceneDelegate, SCNSceneRendererDelegate, SCNPhysicsContactDelegate{ 

var HUDView: HUD! 

required init?(coder: NSCoder) { 
    super.init(coder: coder) 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 

    //-----Scene-Setup---------- 
    let scnView = self.view as! SCNView 
    scnView.scene = FieldScene 
    scnView.playing = true 
    scnView.loops = true 
    self.HUDView = HUD(size: CGSizeMake(100, 100)) 
    scnView.overlaySKScene = self.HUDView 
    scnView.delegate = self 
    scnView.overlaySKScene!.delegate = self 
    scnView.overlaySKScene!.userInteractionEnabled = true 
    scnView.backgroundColor = UIColor.whiteColor() 
    scnView.allowsCameraControl = true 
    scnView.showsStatistics = false 

    let GuyScene = SCNScene(named: "art.scnassets/The1.dae") 
    let Guy: SCNNode = GuyScene!.rootNode.childNodeWithName("Armature", recursively: true)! 
    let GuyBody: SCNNode = GuyScene!.rootNode.childNodeWithName("Cube", recursively: true)! 
    //----Giveing it a physics--------- 
    let collisionCapsuleRadius2 = CGFloat(0.1) 
    let collisionCapsuleHeight2 = CGFloat(0.1) 
    Guy.position = SCNVector3(x: -30.0, y: 30.0, z: 0.0) 
    Guy.scale = SCNVector3Make(50, 50, 50) 
    Guy.rotation = SCNVector4Make(0, 1, 0, 1) 
    Guy.physicsBody = SCNPhysicsBody(type: .Dynamic, shape:SCNPhysicsShape(geometry: SCNCapsule(capRadius: collisionCapsuleRadius2, height: collisionCapsuleHeight2), options:nil)) 
    Guy.physicsBody?.affectedByGravity = true 
    Guy.physicsBody?.friction = 0 // 
    Guy.physicsBody?.restitution = 1 //bounceness of the object 
    Guy.physicsBody?.angularDamping = 1 // rotationess 
    Guy.physicsBody?.mass = 1 
    Guy.physicsBody?.rollingFriction = 0 
    scnView.scene!.rootNode.addChildNode(Guy) 
    scnView.scene!.rootNode.addChildNode(GuyBody) 
    } 

    //Ok Kick function gets declared right here 


    func Kick() { 
    //-----Animate-Guy-----Working-Perfectly----------- 
    let KickAnimation = CAAnimation.animationWithSceneNamed("art.scnassets/The1Anima.dae")! 
    Guy.addAnimation(KickAnimation, forKey: "Go") 
    Guy.removeAnimationForKey("Go", fadeOutDuration: 3.0) 
    } 


} 

    extension CAAnimation { 
class func animationWithSceneNamed(name: String) -> CAAnimation? { 
    var animation: CAAnimation? 
    if let scene = SCNScene(named: name) { 
     scene.rootNode.enumerateChildNodesUsingBlock({ (child, stop) in 
      if child.animationKeys.count > 0 { 
       animation = child.animationForKey(child.animationKeys.first!) 
       stop.initialize(true) 
      } 
     }) 
    } 
    return animation 
} 
} 



    //Ok Below is my HUD/Overlayscene which holds the button that i would like call the function in. 


    extension GameViewController { 
    // was never used 
} 

class HUD: SKScene { 
//-----------------Controller-Buttons---------------------- 
var ButtonA = SKSpriteNode(imageNamed:"EnhancedAButton") 

    override init(size: CGSize) { 
    super.init(size: size) 

    //----A-Button--Creation ------------------- 
    ButtonA.size = CGSize(width: 7, height: 11) 
    ButtonA.anchorPoint = CGPointMake(-11.35, -0.6) 
    ButtonA.zPosition = 0 
    ButtonA.alpha = 0.45 
    self.addChild(ButtonA) 
    } 

required init?(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder) 
} 


    //Ok here is the function I need to put "Kick()" inside it but when I do it doenst recognize it because the function wasnt declared in this view controller 


func AButtonPressed() { 

} 

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    super.touchesBegan(touches, withEvent: event) 
    for touch: AnyObject in touches { 
     let location1 = touch.locationInNode(self) 
     if self.nodeAtPoint(location1) == self.ButtonA { 
      AButtonPressed() 
      print("AButtonPressed") 
     } 
    } 
} 
} 

enter image description here

enter image description here

+0

'정적 FUNC 킥() { // ----- 애니메이션 - 가이 ----- 실무 완벽 ----------- 할 KickAnimation = CAAnimation.animationWithSceneNamed ("예술 .scnassets/The1Anima.dae ")! Guy.addAnimation (KickAnimation, forKey : "Go") Guy.removeAnimationForKey ("Go", fadeOutDuration : 3.0) } ' –

+0

감사합니다.하지만 오류가 발생했습니다. "정적 메서드는 형식에 대해서만 선언 할 수 있습니다. 정적 또는 제거 말할 것이다 "확인되지 않은 식별자 '가이'의 사용 –

+0

방금 ​​질문을 업데이 트 좀 봐 주시기 바랍니다 하단에 이미지를 넣어 –

답변

0

이 실질적으로이 문제를 해결할 수있는 가장 좋은 방법입니다. 다른 방법은 지저분한 반면 자연스럽게 이것을 처리하는 방법은 없습니다. 질문이 있으시면 알려주세요. GameViewController의 viewDidLoad에의 내부 선()이 모든 후

//below this line 
self.HUDView = HUD(size: CGSizeMake(100, 100)) 
//add in 
self.HUDView.delegate = self 

extension GameViewController: GameDelegate { 
    func Kick() { 
     //-----Animate-Guy-----Working-Perfectly----------- 
     let KickAnimation = CAAnimation.animationWithSceneNamed("art.scnassets/The1Anima.dae")! 
     Guy.addAnimation(KickAnimation, forKey: "Go") 
     Guy.removeAnimationForKey("Go", fadeOutDuration: 3.0) 
    } 
} 

protocol GameDelegate { 
    func Kick() 
} 

class HUD: SKScene { 

    var delegate: GameDelegate? 

    override init(size: CGSize) { 
     super.init(size: size) 

     //this calls Kick anywhere in the HUD class 
     delegate?.Kick() 


     //----A-Button--Creation ------------------- 
     ButtonA.size = CGSize(width: 7, height: 11) 
     ButtonA.anchorPoint = CGPointMake(-11.35, -0.6) 
     ButtonA.zPosition = 0 
     ButtonA.alpha = 0.45 
     self.addChild(ButtonA) 
    } 
} 

확장자에없는 킥의 사본을 삭제할 수 있습니다에 추가됩니다. 두 제어기 모두 기능에 액세스 할 수 있습니다.

+0

좋아, 약간의 확장과 프로토콜을 넣어 혼란 내 HUD 컨트롤러 맨 위로. 다음 "self.HUDView.delegate = self"코드 줄을 완벽하게 추가하지만 여기에 "클래스 HUD : SkScene"아래에 "var delegate : GameDelegate?" 하지만 다음 "나는 'SkSceneDelegate?'유형의 속성을 재정의 할 수 없다는 오류가 발생합니다. 또한 클래스 HUD에서 "viewDidLoad()"를 알 수 있습니다. –

+0

괜찮 았으면 지금 시도해주세요. 대답이 –

+0

입니다. 프로그램을 실행하지 못하게하는이 오류를 제외한 모든 것이 좋습니다. . var delelgate : GameDelegate? "오류 유형 'GameDelegate?'속성 '대리인' 'SKSceneDelegate'유형의 속성을 무시할 수 없습니다. (일명 'Optional ') –

관련 문제