So I have a game so far and everything seems to be working very well. I'm only using the basic gamescreen.swift that is default project along with the .sks file for it as well. After a certain condition is met (opponent winning by certain amount of points), it transitions to the .sks file. I even double checked by removing the .sks file to see if it was maybe just transitioning to the .swift file but when I did that I got the nil error (for obvious reasons).
So when I do things like (below) on the .swift file it should theoretically change the .sks file and how it looks visually when I build, but it doesn't. Nothing happens
var background = SKSpriteNode(imageNamed: "theimage")
override func didMove(to view: SKView) {
background.position = CGPoint(x: frame.size.width / 2, y: frame.size.height / 2)
addChild(background)
}
So then I remove everything from the .swift file and instead manually add the image to the .sks file and it doesn't do a single thing when I build it.
So I'm thinking, maybe it's just not even showing the .sks file, but rather a random thing. So I changed the background of the .sks file and built it and the change occurred. I made sure I checked with multiple colors and it didn't work. I just was wondering why if I manually add image to the .sks it's not working yet when I change background image it does...hm
Next time if anyone encounters this make sure that the image you're trying to add as a node isn't significantly large. That was my issue but when I resized the image it all worked out :D
Related
Am I missing something obvious?
For reasons unknown, Xcode 10 keeps crashing on me when I go to assign a texture to a named sprite using the Scene Editor. After having to delete and recreate my .sks files a couple times, I decided to try and reproduce the issue using Xcode's game template. The steps I took to do this are as follows:
Open Xcode and select Create a new Xcode project.
Choose the Game template, name the project Test and save it somewhere, i.e. your desktop.
Drag any image asset into Assets.xcassets. I used this green square:
Open GameScene.sks and drag a Color Sprite onto the scene.
Name the sprite something descriptive like square_green.
Attempt to set the texture in this field:
Other similar questions on Stack Overflow:
Xcode is crashing in SpriteKit
XCode 9 Crashing while setting a Texture in SKScene in Spritekit
If I do not set a name for the color sprite prior to assigning a texture, everything works fine. However, if Xcode is forced to crash at least one time, I have to delete the .sks file completely just to get Xcode to open again.
For the time being I've decided to avoid the trouble all together and just assign the textures programmatically in the didMove(to:) of my SKScene class:
override func didMove(to view: SKView) {
let greenSquare = childNode(withName: "square_green") as! SKSpriteNode
greenSquare.texture = SKTexture(imageNamed: "square_green")
}
I ran into the same problem, and later I moved the images out of Assets.xcassets and the problem was solved. I guess the image can't be placed in Assets.xcassets.
I use SceneKit to construct a 3D Scene, which contains a few object. I convert all the COLLADA file and OBJ file to SCN file. Then I use a blank SCN file to put all the object together.
It looks fine in SceneEditor. And it runs on iPad. Everything looks exactly like it was in the SceneEditor.
Then I come up with some idea with the scale of one object. I change it in the property bar. It looks nice at the SceneEditor. And I tried to run it again.
Then boooom.
There was only one object left in the scene. Everything was gone. I can see all the object on SceneEditor. They just not show up on my iPad.
This happens all the time -- after I change any property of any object on the scene, the scene went wrong.
So far I was trying to build a new SCN file again with all those objects. But as the project gets bigger this might be a nightmare.
What is going on? Do you guys have any solution?
UPDATE:
I've notice that the Full Path of the normal SCN files I add in the scene look like: "/xxx.scn". But after some editing, it automatically changed to "xxx.scn" without slash. I think this may be the reason why objects disappear. The wrong path leads to nil and BANG.
Everytime when I use "Quick View" it will automatically change the Full Path. Even if I don't do anything.
I tried to relocate the file but the Full Path stay with no slash. PLS someone help me with it.
I am generating my levels in SpriteKit via code (in Swift), and its not working out entirely how I'd like. Is there a way to inspect the scene of the simulator at run time. E.g. So I can view how things are being placed outside the visible screen.
Thanks!
Believe me, level editor via code rarely is a good idea. There are exceptions but if you need static levels then you should find another way to defined it.
How can you see your entire scene?
AFAIK you con't with Xcode 7, however you can change how the scene is resize in order to show the full scene inside the view. Depending by the size of your level you can judge whether this is solutions fits your case.
Just open GameViewController.swift and change this
scene.scaleMode = AspectFill
into this
scene.scaleMode = .AspectFit
Now your scene will be compressed to fit the size of the screen. Of course don't forget to restore the original value once you are done.
Whenever I run my sprite kit app this error is logged constantly. It makes it really hard to debug because the log is filled with these messages. They don't seem to effect how the app runs, so simply suppressing the error would be sufficient. Anyone have any idea how to fix this?
I think that it's just a left over debug message that hasn't been cleaned up in iPod/iPhone devices.
In my app, the issue seems to be related to using Sprite Atlases in an xcassets file.
if I initialise a sprite with:
SKTexture(imageNamed: "Sprite")
I get the message; However, using the following:
SKTextureAtlas(named: "Atlas").textureNamed("Sprite")
I do not get the message.
Also, any sprites created in an .sks file will display the error, as you have no opportunity (or need) to define an atlas.
I agree that this is an bug in SpriteKit. I'll share what solved it for me.
I was keeping my image assets within a folder inside Assets.xcassets:
Like all the images in my project, I was loading the images as textures like so:
myTexture = SKTexture(imageNamed: "iPhoneX")
This was causing the error message to appear in my console. As suggested in Darren Branford's answer, I tried doing it the following way instead:
myTexture = SKTextureAtlas(named: "MyTextureAtlas").textureNamed("iPhoneX")
But the error messages still appeared. My solution was to remove all those images from the folder within Assets.xcassets, delete the folder, and keep all the images individually at the top level of Assets.xcassets:
The error messages no longer appeared after this. Granted, I would prefer to organize the images in Assets.xcassets using folders like I was doing before. But this is a rather small project with not many images, so I can live with it. In a larger project with lots of image assets, I can see how this would be much more of a problem. Hopefully Apple will fix this bug someday.
Aside:
It may be of interest to note that the console warnings did not appear when creating the SKTexture using one of these images. What triggered the warning was when I ran the texture's size() method and assigned the result to an SKSpriteNode's size property:
size = texture!.size()
I am trying to understand SpriteBuilder. I made a new file hero.ccb for sprite and set it's sprite frame to the image I need. When I go back to the MainScene and drag hero.ccb from the left menu,there is just a CCBFile without anything(no sprite image). What am I doing wrong?
This can happen if you didn't save the hero.ccb. Choose File -> Save All or simply Publish the project to have SpriteBuilder update all references.