1

I am setting up a game using sprite kit.

I want to make sure a dragable node called background ( skspritenode size 2000 x 2000) stays visible on the screen without showing the edges.

So if the screen is represented by one CGRect called screenRect and the background is represented by another CGRect called backgroundRect I can test if the screen is inside the background.

I am using the following code to test this. It is working as expected apart from trying to make sure my coordinates for the origin point of the background sknode, CGRect, the game and iOS are allowed for.

I think I am nearly there but can't quite get the rectangles where they should be so I can properly test whether they overlap. e.g. the screenRect should line up with the edges of the screen.

Ive tried various options, setting the origin at the centre, at the top left, at the bottom left. and am just getting close sometimes, but not the correct solution.

var screenRect = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)


var backgroundRect = CGRect(x: background.position.x-(background.size.width/2.0), y: background.position.y+(background.size.height/2.0), width: background.size.width*scaleNum, height: background.size.height*scaleNum)


        if backgroundRect.contains(screenRect)

        {
            print ("contained")
            print (screenRect.size)
            print (screenRect.minY)
            print (backgroundRect.size)
            print (backgroundRect.minY)
            }
        else {
            print ("uncontained")
            print (screenRect.size)
            print (screenRect.minY)
            print (backgroundRect.size)
            print (backgroundRect.minY)
}

update I worked it out, I wasn't taking the scale factor or retina screens into account. I haven't worked out the code for that yet but its a matter of multiplying the sizes by the scale factor each time.

carl
  • 121
  • 1
  • 8

0 Answers0