如何将addChild添加到SWIFT

时间:2015-11-16 07:38:13

标签: ios xcode swift sprite-kit scenekit

好的,所以我有所有的部分。最后代码运行没有问题,但正如我在最后一步,试图弄清楚如何添加我遇到麻烦的孩子。我创建了一个覆盖原始场景的叠加场景,但我无法弄清楚将节点/图像添加到实际屏幕的最后一步,因为传统的self.addChild(%^%)不适用于“我的代码中的“节点”。任何帮助。谢谢

代码:

import iAd
import UIKit
import GameKit
import SceneKit
import StoreKit
import SpriteKit
import QuartzCore
import Foundation
import AVFoundation
import AudioToolbox

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

//--------------True-False-statments-------------------------------------------------
var stickActive:Bool = false
var OnOffense = Bool()
var OnDefense = Bool()
var HasBall = Bool()
var OnCampusField = Bool()
var UserControlled = Bool()
//--------Offense-------------
var QuaterBack = SCNNode()
var RunningBack = SCNNode()
var WideReceiver1 = SCNNode()
var WideReceiver2 = SCNNode()
var Linemen1 = SCNNode()
var Linemen2 = SCNNode()
var Linemen3 = SCNNode()
//--------Defense-------------
var LineBacker1 = SCNNode()
var LineBacker2 = SCNNode()
var CornerBack1 = SCNNode()
var CornerBack2 = SCNNode()
var DefensiveLinemen1 = SCNNode()
var DefensiveLinemen2 = SCNNode()
var DefensiveLinemen3 = SCNNode()
//-----------------Controller-Buttons/Joystick---------------------------------------------------
let base = SKSpriteNode(imageNamed:"VirtualJoystickBase")
let ball = SKSpriteNode(imageNamed:"VirtualJoyStickHandle")
let ship = SKSpriteNode(imageNamed:"Ship")
let Button1 = SKSpriteNode(imageNamed:"BlackAButton")
let Button2 = SKSpriteNode(imageNamed:"BlackAButton")
let Button3 = SKSpriteNode(imageNamed:"BlackAButton")
let Button4 = SKSpriteNode(imageNamed:"BlackAButton")
//-------------------3D-Fields--------------------------------------------
let FieldScene = SCNScene(named: "art.scnassets/TesingCampusField.dae")!
//-------------------3D-Players--------------------------------------------
let GuyScene = SCNScene(named: "art.scnassets/Guy.dae")!

override func viewDidLoad() {
    super.viewDidLoad()


    let scnView = self.view as! SCNView
    let skScene = scnView.overlaySKScene
    scnView.overlaySKScene = skScene
    scnView.backgroundColor = UIColor.whiteColor()
    scnView.scene = FieldScene
    scnView.delegate = self
    scnView.allowsCameraControl = true
    scnView.showsStatistics = false


    let Guy1: SCNNode = GuyScene.rootNode.childNodeWithName("Bob_014", recursively: true)!
    FieldScene.rootNode.addChildNode(Guy1)

    //----Positioning-the-Base-of-the-Joystick-----------
    base.size = CGSize(width: 100, height: 100)
    base.anchorPoint = CGPointMake(-3.4, -5.2)
    skScene?.self.addChild(base)
    //----Positioning-the-Ball/Joystick-----------
    ball.size = CGSize(width: 50, height: 50)
    ball.position = base.position

    //---Setting-Up-Ships-Position/PhysicsBody---------------------
    ship.position = CGPointMake(0, 200)
    ship.size = CGSize(width: 80, height: 80)
    ship.physicsBody?.dynamic = true
    ship.physicsBody?.allowsRotation = true
    ship.physicsBody?.affectedByGravity = true
    ship.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "Ship"), size: ship.size)
    //----A-Button--Creation -------------------
    Button1.size = CGSize(width: 40, height: 40)
    Button1.anchorPoint = CGPointMake(-3.4, -5.2)

    //----B-Button--Creation -------------------
    Button2.size = CGSize(width: 40, height: 40)
    Button2.anchorPoint = CGPointMake(-1.2, -5.2)
    //----C-Button--Creation -------------------
    Button3.size = CGSize(width: 40, height: 40)
    Button3.anchorPoint = CGPointMake(-2.2, -6.4)
    //----C-Button--Creation -------------------
    Button4.size = CGSize(width: 40, height: 40)
    Button4.anchorPoint = CGPointMake(-2.2, -3.4)

    let tapGesture = UITapGestureRecognizer(target: self, action: "handleTap:")
    scnView.addGestureRecognizer(tapGesture)
    //--------------------------
    let cameraNode = SCNNode()
    cameraNode.camera = SCNCamera()
    FieldScene.rootNode.addChildNode(cameraNode)
    cameraNode.position = SCNVector3(x: 0, y: 5, z: 15)
    //-----------------------------------------------
    let lightNode = SCNNode()
    lightNode.light = SCNLight()
    lightNode.light!.type = SCNLightTypeOmni
    lightNode.position = SCNVector3(x: 0, y: 10, z: 10)
    FieldScene.rootNode.addChildNode(lightNode)
    //-----------------------------------------------
    let ambientLightNode = SCNNode()
    ambientLightNode.light = SCNLight()
    ambientLightNode.light!.type = SCNLightTypeAmbient
    ambientLightNode.light!.color = UIColor.darkGrayColor()
    FieldScene.rootNode.addChildNode(ambientLightNode)
    //----------------------------------------------
}
func GotoMainMenu() {
    let scene = MainMenuController()
    let sKView = self.view! as! SKView
    sKView.ignoresSiblingOrder = true
    scene.size = sKView.bounds.size
    scene.scaleMode = .AspectFill
    let reveal = SKTransition.fadeWithDuration(0.45)
    sKView.presentScene(scene, transition: reveal)
}
   //====================================================================
override func shouldAutorotate() -> Bool {
    return true
}
   //====================================================================
override func prefersStatusBarHidden() -> Bool {
    return true
}
   //====================================================================
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
        return .AllButUpsideDown
    } else {
        return .All
    }
}
   //====================================================================
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Release any cached data, images, etc that aren't in use.
}

}

0 个答案:

没有答案