Accessing SceneKit-Node from ViewController - ios

I'm trying to build an SceneKit-App in Swift. This App should have an SpriteKit-Overlay. My problem is, that i can access the LabelNode from SpriteKit inside my ViewController, but cannot change the position-value of the Cube-Node in SceneKit, although i can access the node. Because i'm a newbie it would be very nice, if anyone could point me in the right direction.
class GameViewController: UIViewController {
var sceneView: SCNView!
var spriteScene: SpriteKitOverlay!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.sceneView = SCNView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))
self.sceneView.scene = SceneKitScene()
self.sceneView.backgroundColor = UIColor.brownColor()
self.view.addSubview(self.sceneView)
self.spriteScene = SpriteKitOverlay(size: self.view.bounds.size)
self.sceneView.overlaySKScene = self.spriteScene
SceneKitScene().cubeNode.position = SCNVector3(0,10,0)
spriteScene.labelTest.text = "Changed label"
print(SceneKitScene().cubeNode.position)
}
}
class SpriteKitOverlay: SKScene {
var labelTest: SKLabelNode!
override init(size: CGSize) {
super.init(size: size)
self.labelTest = SKLabelNode(text: "Label text")
self.labelTest.fontColor = UIColor.whiteColor()
self.labelTest.fontSize = 24
self.labelTest.position = CGPoint(x: size.width/2, y: size.height/2)
self.addChild(self.labelTest)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
class SceneKitScene: SCNScene {
var cubeNode: SCNNode!
var cameraNode: SCNNode!
var lightNode: SCNNode!
override init() {
super.init()
let cube = SCNBox(width: 3, height: 3, length: 3, chamferRadius: 0)
let cubeMaterial = SCNMaterial()
cubeMaterial.diffuse.contents = UIColor.blueColor()
cube.materials = [cubeMaterial]
self.cubeNode = SCNNode(geometry: cube)
self.cubeNode.position = SCNVector3(0,0,0)
self.cubeNode.runAction(SCNAction.repeatActionForever(SCNAction.rotateByX(0, y: 0.01, z: 0, duration: 1.0/60.0)))
let camera = SCNCamera()
camera.xFov = 60
camera.yFov = 60
let ambientLight = SCNLight()
ambientLight.type = SCNLightTypeAmbient
ambientLight.color = UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1.0)
let cameraConstraint = SCNLookAtConstraint(target: self.cubeNode)
cameraConstraint.gimbalLockEnabled = true
self.cameraNode = SCNNode()
self.cameraNode.camera = camera
self.cameraNode.constraints = [cameraConstraint]
self.cameraNode.light = ambientLight
self.cameraNode.position = SCNVector3(x: 5, y: 5, z: 5)
let omniLight = SCNLight()
omniLight.type = SCNLightTypeOmni
self.lightNode = SCNNode()
self.lightNode.light = omniLight
self.lightNode.position = SCNVector3(x: -3, y: 5, z: 3)
self.rootNode.addChildNode(self.cubeNode)
self.rootNode.addChildNode(self.cameraNode)
self.rootNode.addChildNode(self.lightNode)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}

When you call SceneKitScene().cubeNode.position = SCNVector3(0,10,0), you are trying to change cubeNode.position on a newly created SceneKitScene.
Try changing your GameViewController to:
class GameViewController: UIViewController {
var sceneView: SCNView!
// create new instance variable to hold current sceneKitScene
var sceneKitScene: SceneKitScene!
var spriteScene: SpriteKitOverlay!
override func viewDidLoad() {
super.viewDidLoad()
self.sceneView = SCNView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))
// save SceneKitScene to instance variable
self.sceneKitScene = SceneKitScene()
self.sceneView.scene = self.sceneKitScene
self.sceneView.backgroundColor = UIColor.brownColor()
self.view.addSubview(self.sceneView)
self.spriteScene = SpriteKitOverlay(size: self.view.bounds.size)
self.sceneView.overlaySKScene = self.spriteScene
// update cubeNode position in active sceneKitScene
sceneKitScene.cubeNode.position = SCNVector3(0,10,0)
spriteScene.labelTest.text = "Changed label"
print(SceneKitScene().cubeNode.position)
}
}
This creates a new instance variable to hold your current SceneKitScene (same as you did with your current SpriteKitOverlay). Then you can use this variable to access and update the position of the current cubeNode on screen.

Related

How to create a SKSpriteNode with Custom GKComponent?

So im currently working on a game with spriteKit and gameplayKit. I've created a custom GKComponent class that I'm giving to certain SKSpriteNode via the scene inspector in Xcode (image). Now, when I want to re-create random SKSpritenodes with the same GKComponent, it doesn't seems to work because the overridden update method of my custom GKComponent is not working. Any clues?
I've tried to give the node a new entity object (since it was returning nil) and I gave that entity my custom GKComponent but nothing is working
// In scene
let node = SKSpriteNode(texture: "coin", color: UIColor.clear, size: CGSize(width: 60, height: 60))
let entities: GKEntity! = GKEntity()
let component: CollectableComponent = CollectableComponent()
component.itemType = 1
node.anchorPoint = CGPoint(x: 0.5, y: 0.5)
node.zPosition = 6
node.setScale(0.5)
node.size = CGSize(width: 60, height: 60)
node.position = CGPoint(x: 90, y: 90)
entities.addComponent(component)
node.entity = entities
self.scene?.addChild(node)
// GKComponent
class CustomComponent: GKComponent {
var node: SKSpriteNode?
#GKInspectable var itemType: Int = 0
#GKInspectable var isStatic: Bool = false
override init() {
super.init()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func update(deltaTime seconds: TimeInterval) {
if node == nil {
if let nodeComponent = self.entity?.component(ofType: GKSKNodeComponent.self){
node = nodeComponent.node as? SKSpriteNode
}
}
}
}
Desired behaviour: if the nodeComponent variable in CustomComponent is not nil, I would like to make it run an animation like this:
node = nodeComponent.node as? SKSpriteNode; node.run(SKAction.moveBy(x: 0, y: 15, duration: 0.3));
but with the code, the action would not be running
This is the way you want to be doing this.
Take note that you need to retain your entities, so a global variable is needed.
Also, when you add a node to GKSKNodeComponent, the node.entity variable is set automatically.
class GameScene : SKScene{
var entities: [GKEntity] = [GKEntity]()
func doSomething(){
let node = SKSpriteNode(texture: "coin", color: UIColor.clear, size: CGSize(width: 60, height: 60))
node.anchorPoint = CGPoint(x: 0.5, y: 0.5)
node.zPosition = 6
node.setScale(0.5) //<--- YUCK!!!!!
node.size = CGSize(width: 60, height: 60)
node.position = CGPoint(x: 90, y: 90)
self.scene?.addChild(node)
let entity = GKEntity()
let nodeComponent : GKSKNodeComponent = GKSKNodeComponent(node:node)
let component: CollectableComponent = CollectableComponent()
component.itemType = 1
entity.addComponent(nodeComponent)
entity.addComponent(component)
entities.append(entity)
}
}
// GKComponent
class CustomComponent: GKComponent {
//We want this line to crash if node is empty, so do not use ?
lazy var node: SKSpriteNode! = {return self.entity!.component(ofType: GKSKNodeComponent.self).node as? SKSpriteNode! }
#GKInspectable var itemType: Int = 0
#GKInspectable var isStatic: Bool = false
override init() {
super.init()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func update(deltaTime seconds: TimeInterval) {
}
}

I'm trying to pass a command from a SKScene(SpriteKit) to an SCNScene(SceneKit)

I want to click a button to spawn a 3D object. I overlayed a SpritKit Scene onto a SceneKit Scene to use SKNodes in my interface. But for some reason the function is not being called, the rest of the code seems to be working. I would like to know the reason for this, and if there is another way of calling this function
This is the function I'm trying to pass.
func setupDice() {
var diceNode: SCNNode!
let size: CGFloat = 1.5
let diceGeometry = SCNBox(width: size, height: size, length: size, chamferRadius: 0)
diceGeometry.materials.removeFirst()
for i in 1...6 {
let diceMaterial = SCNMaterial()
diceMaterial.locksAmbientWithDiffuse = true
diceMaterial.diffuse.contents = UIImage(named: "x\(i)")?.xFlipped
diceMaterial.blendMode = .multiply
diceGeometry.materials.append(diceMaterial)
}
diceNode = SCNNode(geometry: diceGeometry)
diceNode.physicsBody = SCNPhysicsBody(type: .dynamic, shape: nil)
diceNode.physicsBody?.restitution = 1
diceNode.physicsBody?.mass = 1
diceNode.physicsBody?.categoryBitMask = 1
diceNode.physicsBody?.collisionBitMask = 2
diceNode.physicsBody?.applyTorque(SCNVector4(x: randomTorqueForce(),
y: randomTorqueForce(),
z: randomTorqueForce(),
w: randomTorqueForce()), asImpulse: true)
self.rootNode.addChildNode(diceNode)
}
This is where I'm calling the function inside of my SpriteKit Scene
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
if touch == touches.first {
if playButtom.contains(touch.location(in: self)){
mainScene.setupDice()
}
}
}
}
and this is the correct state of my GameViewController
class GameViewController: UIViewController {
var sceneView: SCNView!
var spriteScene: OverlayScene!
override func viewDidLoad() {
super.viewDidLoad()
//setupTest()
self.sceneView = SCNView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))
self.sceneView.scene = MainScene()
spriteScene = OverlayScene(size: view.bounds.size)
sceneView.backgroundColor = UIColor.white
sceneView.overlaySKScene = spriteScene
sceneView.allowsCameraControl = true
sceneView.isPlaying = true
self.view.addSubview(self.sceneView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Probably because you didn't set the mainScene property during the initialization phase of your OverlayScene. Something like this should works:
class GameViewController: UIViewController {
var sceneView: SCNView!
var spriteScene: OverlayScene!
override func viewDidLoad() {
super.viewDidLoad()
//setupTest()
self.sceneView = SCNView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))
let mainScene = MainScene()
self.sceneView.scene = mainScene
let overlayScene = OverlayScene(size: view.bounds.size)
overlayScene.mainScene = mainScene
spriteScene = overlayScene
sceneView.backgroundColor = UIColor.white
sceneView.overlaySKScene = spriteScene
sceneView.allowsCameraControl = true
sceneView.isPlaying = true
self.view.addSubview(self.sceneView)
}

Slider determines gravity

So far I've created a rectangle that starts from the bottom and moves upward using UiDynamicAnimator. I would like the user to determine the "strength" of the negative gravity. I want the user to determine the value through a slider.
This is my code so far:
import UIKit
class ViewController: UIViewController {
var orangeSquare: UIView?
var animator: UIDynamicAnimator?
override func viewDidLoad() {
super.viewDidLoad()
func sliderChanged(sender: AnyObject) {
var sliderValue = sender.value
}
//Create animation
let dim = CGRectMake(100, 500, 200, 100)
orangeSquare = UIView(frame: dim)
orangeSquare?.backgroundColor = UIColor.orangeColor()
//Add item to the screen
self.view.addSubview(orangeSquare!)
//Initialize the animator
animator = UIDynamicAnimator(referenceView: self.view)
//Add gravity
let gravity = UIGravityBehavior(items: [orangeSquare!])
let direction = CGVectorMake(0.0, sliderValue)
gravity.gravityDirection = direction
//Collision
let boundries = UICollisionBehavior(items: [orangeSquare!])
boundries.translatesReferenceBoundsIntoBoundary = true
//Add animations
animator?.addBehavior(boundries)
animator?.addBehavior(gravity)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I get two errors:
"Ambiguous use of ´value´" and
"Use of unresolved identifier ´sliderValue´"
How do I convert ´sliderValue´ into a float with just one decimal point?
your code is missing a few things. sliderValue is an unresolved identifier because you have only declared it within sliderChanged but are referring to it in the main body of viewDidLoad. Also, I think that your use of value is ambiguous because you have declared the parameter to the function as AnyObject, whose value could be any one of a number of things!
Your code was missing a mechanism linking a change in the value of the slider with a change in the gravity behaviour. As such, I've implemented this using an explicit target attached to the slider object. I've also thrown in a label showing the magnitude of the gravitational force. This is quite rough but I think it achieves what you were looking to do.
import UIKit
class ViewController: UIViewController {
var dynamicAnimator : UIDynamicAnimator!
var gravityBehaviour : UIGravityBehavior!
var orangeSquare : UIView!
var slider : UISlider!
var sliderLabel : UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Subviews
self.orangeSquare = {
let oS : UIView = UIView(frame: CGRect(origin: CGPoint(x: (self.view.frame.width / 2) - 100, y: 500), size: CGSize(width: 200, height: 200)))
oS.backgroundColor = UIColor.orange
return oS
}()
self.slider = UISlider(frame: CGRect(origin: CGPoint(x: 100, y: 100), size: CGSize(width: self.view.frame.width - 400, height: 50)))
self.slider.addTarget(self, action: #selector(self.sliderValueDidChange), for: UIControlEvents.allTouchEvents)
self.slider.minimumValue = -5
self.slider.maximumValue = 5
self.slider.value = 0
self.sliderLabel = UILabel(frame: CGRect(origin: CGPoint(x: self.view.frame.width - 100, y: 100), size: CGSize(width : 50, height: 50)))
self.sliderLabel.backgroundColor = UIColor.red
self.sliderLabel.textAlignment = NSTextAlignment.center
self.sliderLabel.textColor = UIColor.white
self.sliderLabel.text = String(self.slider.value)
// Assemble
self.view.addSubview(self.orangeSquare)
self.view.addSubview(self.slider)
self.view.addSubview(self.sliderLabel)
// Configure dynamic behaviours
self.dynamicAnimator = UIDynamicAnimator(referenceView: self.view)
self.gravityBehaviour = UIGravityBehavior(items: [self.orangeSquare])
self.gravityBehaviour.gravityDirection = CGVector(dx: 0, dy: 0)
// Configure boundaries
let boundaries : UICollisionBehavior = UICollisionBehavior(items: [self.orangeSquare])
boundaries.translatesReferenceBoundsIntoBoundary = true
self.dynamicAnimator.addBehavior(self.gravityBehaviour)
self.dynamicAnimator.addBehavior(boundaries)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func sliderValueDidChange() {
// When the slider value changes, update the label text and the gravity vector
self.sliderLabel.text = String((round(self.slider.value) * 10) / 10)
self.gravityBehaviour.gravityDirection = CGVector(dx: 0, dy: CGFloat(-1 * self.slider.value))
}
}
Hope that helps. All best!
import UIKit
class ViewController: UIViewController {
var dynamicAnimator : UIDynamicAnimator!
var gravityBehaviour : UIGravityBehavior!
var orangeSquare : UIView!
var slider : UISlider!
var sliderLabel : UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Subviews
self.orangeSquare = {
let oS : UIView = UIView(frame: CGRect(origin: CGPoint(x: (self.view.frame.width / 2) - 100, y: 500), size: CGSize(width: 200, height: 200)))
oS.backgroundColor = UIColor.orangeColor()
return oS
}()
self.slider = UISlider(frame: CGRect(origin: CGPoint(x: self.view.frame.width - 100, y: 100), size: CGSize(width: self.view.frame.width - 300, height: 150)))
self.slider.addTarget(self, action: #selector(self.sliderValueDidChange), forControlEvents: UIControlEvents.AllTouchEvents)
self.slider.minimumValue = -10
self.slider.maximumValue = 10
self.slider.value = 0
self.sliderLabel = UILabel(frame: CGRect(origin: CGPoint(x: self.view.frame.width - 100, y: 200), size: CGSize(width : 50, height: 50)))
self.sliderLabel.backgroundColor = UIColor.redColor()
self.sliderLabel.textAlignment = NSTextAlignment.Center
self.sliderLabel.textColor = UIColor.whiteColor()
self.sliderLabel.text = String(self.slider.value)
// Assemble
self.view.addSubview(self.orangeSquare)
self.view.addSubview(self.slider)
self.view.addSubview(self.sliderLabel)
// Configure dynamic behaviours
self.dynamicAnimator = UIDynamicAnimator(referenceView: self.view)
self.gravityBehaviour = UIGravityBehavior(items: [self.orangeSquare])
self.gravityBehaviour.gravityDirection = CGVector(dx: 0.0, dy: 0.0)
// Configure boundaries
let boundaries : UICollisionBehavior = UICollisionBehavior(items: [self.orangeSquare])
boundaries.translatesReferenceBoundsIntoBoundary = true
self.dynamicAnimator.addBehavior(self.gravityBehaviour)
self.dynamicAnimator.addBehavior(boundaries)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func sliderValueDidChange() {
// When the slider value changes, update the label text and the gravity vector
self.sliderLabel.text = String((round(self.slider.value) * 10) / 10)
self.gravityBehaviour.gravityDirection = CGVector(dx: 0, dy: CGFloat(-1 * self.slider.value))
}
}

Why doesn't my snow fall to the ground?

I am trying to make a application (in swift) where snow falls in the background. The only problem is I have added the gravity animation, however the snow just stays where it is.
Here is my code:
ViewController.swift
import UIKit
#IBDesignable
class ViewController: UIViewController {
#IBInspectable var BgColor:UIColor = UIColor.whiteColor()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = BgColor
/*listSubviewsOfView(self.view)*/ /*Not needed to answer this*/
var snow = Snow(frame: CGRect(x: 0, y: 0, width: 5, height: 5))
snow.opaque = false
self.view.addSubview(snow)
let animator = UIDynamicAnimator(referenceView: self.view)
let gravity = UIGravityBehavior(items: [snow])
let direction = CGVectorMake(0.0, 1.0)
gravity.gravityDirection = direction
animator.addBehavior(gravity)
/*
var snow = Snow(frame: CGRect(x: 0, y: 0, width: 5, height: 5))
snow.opaque = false
snow.viewHeight = UIScreen.mainScreen().bounds.height
snow.addSubview(snow)
let animator = UIDynamicAnimator(referenceView: view)
let gravity = UIGravityBehavior(items: [snow])
animator.addBehavior(gravity)
*/
}
/*Not needed to solve this*/
/*
func listSubviewsOfView(views: UIView) {
var index = 0
let randomNumbers = [Int](1...24).shuffle()
for view in views.subviews
{
if let _ = view.restorationIdentifier
{
view.setValue(String(Int(randomNumbers[index])), forKey: "updateText")
index++
}
if index == randomNumbers.count {
break
}
}
}
*/
}
Snow.swift:
import Foundation
import UIKit
#IBDesignable
class Snow:UIView
{
var viewHeight = CGFloat(0)
/*
required init(coder aDecoder: NSCoder) {
//Initilse UIView
super.init(coder: aDecoder)!
}
*/
override func drawRect(rect: CGRect) {
let path = UIBezierPath(ovalInRect: rect)
UIColor.whiteColor().setFill()
path.fill()
}
}
The question is, why does my snow stay on top of the screen at (0,0) and not fall down even though I have told it to have the gravity affect?
Make animator and gravity properties of your view controller.
import UIKit
#IBDesignable
class ViewController: UIViewController {
#IBInspectable var BgColor:UIColor = UIColor.whiteColor()
var animator: UIDynamicAnimator? = nil;
let gravity = UIGravityBehavior()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.blackColor()
let snow = Snow(frame: CGRect(x: 0, y: 0, width: 5, height: 5))
snow.opaque = false
self.view.addSubview(snow)
animator = UIDynamicAnimator(referenceView:self.view);
animator?.addBehavior(gravity)
gravity.addItem(snow)
let direction = CGVectorMake(0.0, 1.0)
gravity.gravityDirection = direction
}
}

How to addChild to a already built overlaySKScene in SWIFT

Ok so I have all of the parts. Finally the code runs without a problem but right as I'm in the last step, trying to figure out how to add the child I'm having trouble. I created an overlay scene that is over the original Scene but I can't figure out the last step which is added the node/image to the actually screen because the traditional self.addChild(%^%) doesn't work on the "base" node in my code. Any help. Thanks
Code:
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.
}
}

Resources