Changing x position of SKSpriteNode

Cyril

I am running a for loop in my game that would generate SKSpriteNodes.

I want my first block to start at (20, 40) and add 20 to the x position and 40 to the y position for every new block.

First block (20, 40) Second block (40, 80) Third Block (60, 120) And so on.

The y position is behaving the way I want to, however the x position would randomly start anywhere on my screen then add 20 for every new block generated.

Below is my code.

       func generateBlocks() {

            for _ in 1..<5 {

                xP += 20
                yP += 40

                let xx = CGFloat (xP)
                let yy = CGFloat (yP)

                print("x position: \(xx)")
                print("y position: \(yy)")

                let resourcePath = NSBundle.mainBundle().pathForResource("Blocks", ofType: "sks")
                let blocks = MSReferenceNode(URL: NSURL (fileURLWithPath: resourcePath!))

                blocks.goals.position = CGPoint (x: xx,y: yy)
                addChild(blocks)
    }

    }

I also added the print statement of how it would look like when I run the code.

Below is the print statement of the x and y positions of the blocks. enter image description here

I tried switching the order of the xP and yP initializing. That didn't work.

Any suggestions?

Alessandro Ornano

I would prefer to use while loops for this kind of operations where you must impose a condition from start, for example limits:

class GameScene: SKScene {
    let deltaX:CGFloat = 20.0
    let deltaY:CGFloat = 40.0
    override func didMoveToView(view: SKView) {
        let widthLimit = self.frame.width-40
        let heightLimit = self.frame.height-140
        let startX:CGFloat = 80.0
        drawStair(startX,limits:CGSizeMake(widthLimit,heightLimit))
    }
    func drawStair(startFromX:CGFloat,limits:CGSize) {
        var currentX:CGFloat = startFromX
        var currentY:CGFloat = startFromX+deltaY
        while currentX<limits.width && currentY<limits.height {
            drawBlock(CGPointMake(currentX,currentY))
            currentX += deltaX
            currentY += deltaY
        }
    }
    func drawBlock(position: CGPoint,size:CGSize = CGSizeMake(40,40)) {
        let block = SKSpriteNode(color: UIColor.redColor(), size: size)
        block.position = position
        block.name = "block"
        addChild(block)
    }
}

Output:

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Changing x position of SKSpriteNode

From Dev

SKSpriteNode position

From Dev

SKSpriteNode position

From Dev

Changing X-axis position in R barplot

From Dev

Javascript for loop changing X position using JSkinetic

From Dev

Set X and Y of a view onclick not changing position

From Dev

SkSpriteNode position in universal game

From Dev

Getting child SKSpriteNode Position

From Dev

Changing SKSpriteNode property via touchesBegan

From Dev

changing texture of a skspritenode without changing size

From Dev

changing texture of a skspritenode without changing size

From Dev

Changing the color property of an SKSpriteNode is not changing its color

From Dev

Position SKSpriteNode at 0,0?

From Dev

Is there a way to test with if for a position field of a skspritenode?

From Dev

How to set mouse position in X coordinate without changing the Y coordinate

From Dev

Autolayout animate x/y position of UIView without changing width/height

From Dev

How to prevent physics body from changing x position in Sprite Kit

From Dev

SKSPriteNode position changes to 0,0 for no reason

From Dev

SKSpriteNode's position changes after placement

From Dev

Track the position of SKSpriteNode while doing a SKAction moveTo

From Dev

Move SKSpriteNode position across multiple scenes

From Dev

SKSpriteNode won't position after didBeginContact

From Dev

Track the position of SKSpriteNode while doing a SKAction moveTo

From Dev

Weird Spritekit Collision Bug depending on SKSpriteNode position

From Dev

Swift 4: SKSpriteNode won't stop on a position

From Dev

changing the position of headings

From Dev

Default camera position not changing?

From Dev

Changing position during animation?

From Dev

Parameter ValueFromPipeline and changing position

Related Related

  1. 1

    Changing x position of SKSpriteNode

  2. 2

    SKSpriteNode position

  3. 3

    SKSpriteNode position

  4. 4

    Changing X-axis position in R barplot

  5. 5

    Javascript for loop changing X position using JSkinetic

  6. 6

    Set X and Y of a view onclick not changing position

  7. 7

    SkSpriteNode position in universal game

  8. 8

    Getting child SKSpriteNode Position

  9. 9

    Changing SKSpriteNode property via touchesBegan

  10. 10

    changing texture of a skspritenode without changing size

  11. 11

    changing texture of a skspritenode without changing size

  12. 12

    Changing the color property of an SKSpriteNode is not changing its color

  13. 13

    Position SKSpriteNode at 0,0?

  14. 14

    Is there a way to test with if for a position field of a skspritenode?

  15. 15

    How to set mouse position in X coordinate without changing the Y coordinate

  16. 16

    Autolayout animate x/y position of UIView without changing width/height

  17. 17

    How to prevent physics body from changing x position in Sprite Kit

  18. 18

    SKSPriteNode position changes to 0,0 for no reason

  19. 19

    SKSpriteNode's position changes after placement

  20. 20

    Track the position of SKSpriteNode while doing a SKAction moveTo

  21. 21

    Move SKSpriteNode position across multiple scenes

  22. 22

    SKSpriteNode won't position after didBeginContact

  23. 23

    Track the position of SKSpriteNode while doing a SKAction moveTo

  24. 24

    Weird Spritekit Collision Bug depending on SKSpriteNode position

  25. 25

    Swift 4: SKSpriteNode won't stop on a position

  26. 26

    changing the position of headings

  27. 27

    Default camera position not changing?

  28. 28

    Changing position during animation?

  29. 29

    Parameter ValueFromPipeline and changing position

HotTag

Archive