I want to replace scene in my project.
I have made project using spritekit and swift in xcode version 6
Now when I try to write replace scene code using this
for touch:AnyObject in touches
{
let location=touch.locationInNode(self)
if self.nodeAtPoint(location)==self.playbutton
{
println("Go to the game!")
var scene=PlayScene(size:self.size)
let skView = self.view as SKView //ERROR
}
}
It gives me ERROR:
Downcast from 'SKView?' to 'SKView' only unwraps optionals; did you mean to use '!'?
You have an "optional" problem over here.
Use this instead:
let skView = self.view? as SKView!
You don't need the downcast part, that's what Xcode is saying, not as .....
Remove as.... and just unwrap with let skView = self.view! or an if/let statement if you're not sure about whether its nil:
if let skView = self.view{
skView.presentScene(...)
}
Watch out for strong reference cycles when you replace the scene. Remember the view and scene are together, so..., you'll figure out.
Related
Recently, I decided to apply my previous knowledge in C++ and Python to learning Swift. After which, I decided to see what I could do with the SceneKit framework. After hours of checking through the documentation, and consulting a tutorial, I have to wonder what's going wrong with my code:
class GameViewController: UIViewController {
var gameView:SCNView!
var gameScene:SCNScene!
var cameraNode:SCNNode!
override func viewDidLoad() {
super.viewDidLoad()
initScene()
initView()
initCamera()
}
func initView() {
//initialize the game view - this view holds everything else in the game!
gameView = self.view as! SCNView
//allow the camera to move to gestures - mainly for testing purposes
gameView.allowsCameraControl = true
//use default lighting while still practicing
gameView.autoenablesDefaultLighting = true
}
func initScene() {
//initialize the scene
gameScene = SCNScene()
//set the scen in the gameView object to the scene created by this function
gameView.scene = gameScene
}
func initCamera() {
//create a node that will become the camera
cameraNode = SCNNode()
//since a node can be any object in the scene, this needs to be set up as a camera
cameraNode.camera = SCNCamera()
cameraNode.position = SCNVector3 (x:0, y:5, z:15)
}
}
After more checking through the documentation and making sure that I was now copying from the tutorial directly to get it to work, I still have no luck with this. According to a lot of the other questions I found here on StackOverflow, it looks like it has something to do with the forced unwrapping, the exclamation points, but I'm not exactly sure why that is.
I've probably been staring the answer in the face combing through this documentation, but I'm not quite seeing what the problem is.
Also, apologies if my comments are a bit long and/or distracting.
You have the following problems:
1) you should re-order the initializations in your viewDidLoad, doing so:
initView() // must be initialized before the scene
initScene() // you have been crashing here on getting `gameView.scene`, but gameView was nil
initCamera()
2) cameraNode is not attached on the rootNode, so you may add the following code at the end of initCamera:
gameScene.rootNode.addChildNode(cameraNode)
I am making a zombie apocalypse-style cheese game and I am having trouble knowing where/how to declare zombies. I thought that since I had them made in my Gamescene.sks file with each the name 'zombie,' then they would be declared like that. I am trying to make the zombies(which I made 4 in the .sks file) chase the player(which I also set up the .sks file and coded in the upper lines of code.)
override func didMove(to view: SKView) {
let player = self.childNode(withName: "player") as? SKSpriteNode
for child in self.children{
if child.name == "zombie"{
if let child = child as? SKSpriteNode {
zombies.append(child)
}
}
}
}
After a while of trying, I found out that my .sks and .swift files were not communicating and I had to set up the view.
if let view = self.view as! SKView?
if let scene = GameScene(fileNamed: "GameScene")
scene.scaleMode = .resizeFill
How do i make a Swift sprite kit game jump to another scene by searching for the scene with a string. so:
let scenename : String = "Map1"
let scene : SKSCene = SkScene.getscene(scenename)
Do we have something like that?
if not please help me with alternatives!
Thanks for your help and time!
Make a delegate call to your SKView from current scene, which is presenting the scene then present another scene like this:
let newScene = SKScene.init(fileNamed: "MyScene")
newScene!.scaleMode = SKSceneScaleMode.Fil
let reaveal = SKTransition.crossFadeWithDuration(0.01)
let aSKView = self.view as! SKView
aSKView.presentScene(newScene!, transition: reaveal)
Do check for memory leaks ;)
I'm currently having trouble with passing variables to a scene in spritekit (swift). In android when moving to a different activity, I just set the parameter to the Intent and simply get the variable. But since I'm new to spritekit and swift, i find it difficult. Is there a clear way to pass a variable to the scene ? I have a sample code below that I tested but did not work.
class GameSKView: SKView {
var mylevel = 0
}
//Inside my main GameScen
let gameView = self.view as GameSKView
sample.text = "my level is :\(gameView.mylevel)"
Try this:
class GameSKView: SKView {
internal var mylevel = 0
}
And inside you main game scene
if let gameView = self.view as GameSKView {
sample.text = "my level is :\(gameView!.mylevel)"
}
Finally found the solution.
In order to use the custom SKView, I have done the following procedures.
Open the Idenity inspector in the Main.storyboard
Replace the [SKView] inside the Custom Class with your [customSKView]
, In my case GameSKView
Save. That was all I needed to do.
I'm trying to translate SKScene * scene = [GameScene sceneWithSize:skView.bounds.size]; into swift but I get the error
'sceneWithSize' is unavailable: use object construction 'SKScene(size:)'.
I am using viewWillLayoutSubviews and cutting out the viewDidLoad() because it does not give correct dimensions for the screen dimensions of the device I choose. It actually makes me question why viewDidLoad() exists at all?
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews();
let skView = self.view as SKView;
if skView.scene != nil {
skView.showsFPS = true;
skView.showsNodeCount = true;
skView.showsPhysics = true;
// Create and configure the scene
let scene:SKScene = GameScene.sceneWithSize(skView.bounds.size); // ERROR MESSAGE!
// Objective-C code in next 2 lines
// SKScene * scene = [GameScene sceneWithSize:skView.bounds.size];
// scene.scaleMode = SKSceneScaleModeAspectFill;
// Present Scene
skView.presentScene(scene)
}
}
Try changing
let scene:SKScene = GameScene.sceneWithSize(skView.bounds.size);
by
let scene:SKScene = GameScene(size: skView.bounds.size);
Hope this helps ;)
As the error says, the function you are trying to use is not available. You should instead use the constructor init(size size: CGSize):
let scene = SKScene(size: skView.bounds.size)
Note also that the :SKScene is not necessary as the type is obvious from the constructor.
If you open the documentation, you will see that +sceneWithSize: is not available for Swift.
When a view's bounds change, the view adjusts the position of its subviews. Your view controller can override this method to make changes before the view lays out its subviews. The default implementation of this method does nothing.