Flubadoodoo
Flubadoodoo

Reputation: 127

Get SKView size inside custom class in iOS

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.

Upvotes: 3

Views: 1694

Answers (2)

CodeSmile
CodeSmile

Reputation: 64477

You could simply derive the player class from SKNode. You can then add it to the scene hierarchy and then you have a reference to the scene.

You can still have a "model" class containing player data and methods stored inside the custom player node class.

From a MVC perspective and "player" being the model it should in fact not need to know about the screen size because that's a view property. Maybe you simply have this backwards and you actually want to do something on the view side with the information in the player class?

Upvotes: 0

Jonathan Arbogast
Jonathan Arbogast

Reputation: 9650

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.

Upvotes: 2

Related Questions