Bottom left coordinates of SKScene
I am new to Swift SpriteKit programming and the coordinate system is driving me crazy. I am creating a sprite and I want to move it to the four corners of the screen. So I set the position to (0,0). It's in the lower left corner of the screen. After some manual testing, I developed the diagram below. Bottom-left and top-right are what the iOS simulator says when I touch the screen.
I have 2 questions:
1: Is there a way to determine the coordinates of the bottom left corner of the view? Perhaps I could build a dictionary with coordinates and determine the type of car and then set the offsets. But that is a lot of work and may not be accurate for newer devices. It seems like there should be a scene or frame property that I can use to position the object in the lower left corner of the window.
2: Math doesn't work. On iPhone5, 300 (bottom left x) + 320 (width) = 620, not the reported 727. A similar problem is true with y coordinates. How it works?
I have set as many parameters as possible. I have not changed the anchor point or scene position.
Device Size LL UR iPhone4s (320,480) (260.0) (766,764) iPhone5 (320,568) (300.0) (727,764) iPhone5s (320,568) (298.0) (727,764) iPhone6 ββ(375,667) (297.1) (728,765) iPhone6plus (414,736) (298.0) (728,766) iPad2 (768.1024) (226.0) (800.768) iPad Air (768.1024) (224.0) (800.767) iPad Retina (768,1024) (225.0) (800,768)
source to share
Ok I think I figured it out. Setting the scene! .ScaleMode = SKSceneScaleMode.ResizeFill allows me to identify the four corners of the screen. So now I can tell when the sprite is crossing the edge of the screen. It doesn't seem to distort my images. I haven't been able to test it on a reader yet, but it leaves a blank area around iPad2.
source to share
Applause for the hard work! haha If I was going to get the values ββfor the bottom coordinates, I would use CGRectGetMinX
to get the x coordinate and CGRectGetMinY
to get the y coordinate in a similar way:
CGPoint minimum = CGPointMake(CGRectGetMinX(self.frame),CGRectGetMinY(self.frame));
Then, if you want to get the top coordinates, use the same things, but say MaxX or MaxY. Yes, the coordinates are a little confusing, but if you use them then it will be a breeze.
EDIT: If you need to find that the body is out of visible space, so far what worked for me is creating a physical body to detect contact with it at the edge.
[SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame]
maybe another option you can try is to see the boundaries of the UIScreen object.
source to share