How to call a colored sprite to a specific position (Swift 3)

So, as part of my project, I'm basically trying to create a randomly generated maze with random inputs and outputs. I have three classes (maze class, GameScene class and Grid class), so far I have successfully been able to code a randomly generated maze.

However, my problem is that the Maze class (where the random generation code happens) only contains the data for the maze, but not the current graphics connection. So when I run the application, a blank screen appears.

What I'm trying (but I'm not sure how) to do is basically place a line (aka a colored sprite in a shape) where the maze wall is.

So the way my maze works is a grid (array) where each "cell" of a cell (up, down, left, or right) is defined as true or false. If the cell wall is false, then there is no labyrinth wall; but if this is true, then there is a labyrinthine wall. The state of each random wall creates a maze.

I was able to connect the code to a colored sprite named "mazeWall" in the GameScene class (using this code :)

var mazeWall: SKNode!

      

[...]

mazeWall = self.childNode(withName: "mazeWall")

      

Also, I was able to access the data of the Maze class in the GameScene class using this code to create an object of the Maze class:

var maze = Maze()

      

However, now I'm not sure how to call the color sprite to the position of the true walls in the grid.

This is the code I tried to use in the GameScene class to access the data of the Maze class and to scroll through the maze cells and check which walls are true:

class GameScene: SKScene {
var mazeWall: SKNode!
var maze = Maze()
var backgroundColorCustom = UIColor(red: 255, green: 244, blue: 231,
                                    alpha: 4.0)



override func didMove(to view: SKView) {
    /* Set reference to obstacle layer node */
    mazeWall = self.childNode(withName: "mazeWall")

    //going through each cell and checking if it is true in order to place a wall
    for x in 0..<self.maze.gridSize{
        for y in 0..<self.maze.gridSize{
            if (self.maze.down[x,y]) == true {
                //code
            }

            if (self.maze.up[x,y]) == true {
                //code
            }

            if (self.maze.left[x,y]) == true{
                //code
            }
            if (self.maze.right[x,y]) == true {
                //code
            }

        }//end of y forloop
    }//end of x for loop

}//end of func didMove

}//end of GameScene class

      

Please let me know how I can name the color sprite maze to the position of the "true" maze walls within the maze. Also let me know if you want me to include more of my code (my maze class, grid class, maze generation, etc) ...

Thanks in advance!

+3


source to share


2 answers


What you need to do is use SKTexture

to cache the same wall that you will use for all the ones created SKSpriteNode

.

So you load your wall asset into let wallTexture = SKTexture(imageNamed: <NameOfYourWall>)

and then into each of your 4 if you create SKSpriteNode(texture: wallTexture)

.

Now comes the math where you have to set the position of each of these SKSpriteNodes.

Let's say you want the maze to fit the screen, so you need to get the maximum tile size doing this pseudocode:

let maxWidth = SCREEN_SIZE / maze.gridSize
let maxHeight = SCREEN_SIZE / maze.gridSize
let tileSize = max(maxWidth, maxHeight)

      

And you can finally put walls in your logic. Don't forget that by default the anchorPoint SKSpriteKit is its center and that the geometry is as follows:



^ y
|
|-->x

      

`

if self.maze.down[x,y] == true {
    let wall = SKSpriteNode(texture: wallTexture)
    let centerTile = CGPoint(x: (x * tileSize + tileSize/2), y: (y * tileSize + tileSize/2))
    let xPos = centerTile.x - wall.width /2
    let yPos = centerTile.y - tileSize / 2 + wall.height
    wall.position = CGPoint(x: xPos, y: yPos)
    yourScene.addChild(wall)
}

      

You may need to rotate the wall to a valid and correct size (if your original texture is already vertical), or you just need to create another object with a vertical version and load that new asset into a different texture.

Just correct the math and you will be done.

+1


source


You might consider adding a function that converts the x, y of your grid point to a maze in CGPoint where the maze wall should be placed. Once you get the cgpoint for the x, y grid point, it's just a matter of creating a new sprite with the wall texture and adding it as a child at that cgpoint position.

First, define as a constant how large each grid point is. You will most likely need to refer to this constant outside of the function below as well (for example, when adjusting the size of a sprite in a wall maze), so give it a higher volume (for example, as a property of your scene).

let sizeOfEachGridPoint = CGSize(width: 32, height: 32) // 32 just an example

      

To write the required function, I made some assumptions; you can easily adapt this function if the assumptions are wrong.



  • x, y == (0,0) is the bottom left grid point of your maze.
  • The bottom left corner of the bottom left grid point will be in CGPoint.zero.
  • The anchor points of the sprites will be 0.5,0.5

Based on these assumptions, you can:

func cgPointForGridPoint(x: Int, y: Int) -> CGPoint    
{
 // Note that we are also adding half of the width and half of the 
 // height since the anchor point of the grid point is at its center

    let xOfPoint = CGFloat(x) * sizeOfEachGridPoint.width
                   + sizeOfEachGridPoint.width / 2 

    let yOfPoint = CGFloat(y) * sizeOfEachGridPoint.height
                   + sizeOfEachGridPoint.height / 2  

    return CGPoint(x: xOfPoint, y: yOfPoint)
}

      

Hope this helps!

+1


source







All Articles