E. Huckabee
E. Huckabee

Reputation: 1808

Prevent SKSpriteNode from "overflowing" outside SKSpriteNode

I am making a minimap in my game and I need two things to happen.

  1. my minimap doesnt "overflow" outside its designated section like so

image 1

  1. I am making a red "arrow" appear on the minimap representing the players position and I need the minimap to move with the player so that the minimap is always centered on the players position (even if he goes to the edge of the map)

how can I achieve these things in swift?

Upvotes: 0

Views: 60

Answers (1)

Knight0fDragon
Knight0fDragon

Reputation: 16827

Due to no code provided I am going to just talk about the techniques you need to use

To avoid overlaps, you want to useSKCropNode to create a cropped region.

The idea behind it is really simple.

let cropNode = SKCropNode()
cropNode.maskNode = maskNode <---This is a black and white representation of what you expect the node to draw, where if alpha < .5 means do not draw, and alpha >= .5 means draw.  I would start with an SKShapeNode to achieve said effect
cropNode.addChild(miniMap)

To keep the minimap relative to the player is also simple.

Lets say your map is 1024x1024 tiles, so your minimap is 1024x1024 points

Important, both anchor points need to be the same

You take the players position in the tile world, and divide the x by tile width, and the y by tile height, and that gets you the position in minimap world.

Just move the mini map to the negative position you just created (that is both -x and -y) and that will align your mini map to where your player is located.

Upvotes: 1

Related Questions