Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sprite Kit texture sizes / resolution

I just started using Sprite Kit, and have an issue with loading textured sprites.

I created a new project with the Sprite Kit template in Xcode, and wanted to add a simple sprite with an image file as the texture. Here is the code I used in the scene's init method :

SKSpriteNode *myNode = [SKSpriteNode spriteNodeWithImageNamed:@"my_image_file"];
myNode.position = CGPointMake(100, 100);
[self addChild:myNode];

I didn't add the extension to the image name, so my file is named "my_image_file.png", but I omitted the .png at the end. I also moved the Scene creation to the viewWillLayoutSubviews method, as suggested in a number of tutorials.

I ran the project in the simulator (using retina iPhone as the device), and to my surprise, the sprite appeared twice as big as I expected, and with quite big pixels too. It looked like as if I was using a non-retina iPhone. As far as I can tell the Apple Adventure example app uses the same naming conventions, and the sprites appear just fine there.

I renamed my file "[email protected]", and it all worked fine, the sprite appeared as I expect it to look on a retina display. Since I have quite a few image files, I prefer not to rename them all, and it all works fine in the Apple Adventure app without the @2x postfix anyway.

It also works fine if I explicitly define the size of the texture using this method on SKSpriteNode :

+ (instancetype)spriteNodeWithTexture:(SKTexture *)texture size:(CGSize)size

I cleaned the project and deleted the app from the simulator between each run, so I assume this is not an issue with stuck or missing image files.

I also tried searching the Internet to see how Sprite Kit texture file naming conventions work, but I couldn't find any documentation regarding the issue.

Can someone please explain briefly how it works, or at least show me a solution that doesn't involve renaming all my image files, or defining the size explicitly for all my sprites?

Thanks, utr

like image 552
utrutr Avatar asked Nov 12 '22 18:11

utrutr


1 Answers

If you use Asset Catalog, you do not need to rename all your files. Just click on the Images.xcassets if you created the project using the Xcode SpriteKit template. Then drag and drop images to appropriate places. You can also create an Asset Catalog manually by doing New File -> Resources -> Asset Catalog.

like image 189
quaertym Avatar answered Nov 15 '22 07:11

quaertym