Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get SKView size inside custom class in iOS

Tags:

ios

sprite-kit

Im trying to make a game with Sprite Kit on iOS. I have a UIViewController with an SKView, and I am using one SKScene.

Now in my scene, I am initializing a 'Player' class, which is just a custom class I wrote, it's a subclass of NSObject. The problem is that I need to know what the screen size is inside my custom class. From what I can tell, the only way to know the screen size inside my custom class is to pass it in from the scene in the Player's initializer.

Is there a more elegant way to do this? Thank you.

like image 455
Flubadoodoo Avatar asked Jan 27 '26 03:01

Flubadoodoo


1 Answers

You could just use [UIScreen mainScreen].bounds within your Player class. Check out the applicationFrame and scale properties of a UIScreen as well.

You might argue that having a Player know the screen size isn't elegant in the first place, but nonetheless this is a way to do it without passing the information into a Player.

like image 189
Jonathan Arbogast Avatar answered Jan 28 '26 17:01

Jonathan Arbogast