Reputation: 151
I am working on a spritekit game and it all is working correctly, yet my ground is in the middle of the simulators screen. I have tried everything to make it be at the bottom of the screen. one function.
let groundTexture = SKTexture(imageNamed: "Ground")
groundTexture.filteringMode = .nearest
for i in stride(from: 0, to: 2 + self.frame.size.width / groundTexture.size().width, by: 1) {
let ground = SKSpriteNode(texture: groundTexture)
ground.zPosition = 1.0 //-10
//ground.position = CGPoint(x: (groundTexture.size().width / 2.0 + (groundTexture.size().width * CGFloat(i))), y: groundTexture.size().height / 4.0) //original position
ground.position = CGPoint(x: 0, y: groundTexture.size().height / +0) //tried this from a tutorial
ground.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: self.frame.size.width * 2.0, height: groundTexture.size().height / 4.0)) //erase * 4 test
ground.setScale(1.2)
ground.physicsBody?.isDynamic = false
ground.physicsBody?.allowsRotation = false
ground.physicsBody?.affectedByGravity = false
ground.physicsBody?.categoryBitMask = groundCategory
//contact and collision bitmask
ground.physicsBody?.contactTestBitMask = playerCategory | enemy1Category | enemy2Category | enemy3Category | enemy4Category | obstacleCategory | coinCatergory
ground.physicsBody?.collisionBitMask = playerCategory | enemy1Category | enemy2Category | enemy3Category | enemy4Category | obstacleCategory | coinCatergory
ground.physicsBody?.restitution = 0.0
self.addChild(ground)
let moveLeft = SKAction.moveBy(x: -groundTexture.size().width, y: 0, duration: 5)
let moveReset = SKAction.moveBy(x: groundTexture.size().width, y: 0, duration: 0)
let moveLoop = SKAction.sequence([moveLeft, moveReset])
let moveForever = SKAction.repeatForever(moveLoop)
ground.run(moveForever)
}
Upvotes: 2
Views: 112
Reputation: 6061
By default a scene's anchorPoint
coordinates are 0, 0 unless otherwise specified. anchorPoint(x: 0, y: 0)
is the center of the screen. You are not specifying a position for your ground so it gets automatically added to the scenes anchorPoint (which is the middle of the screen).
You need to either change the scenes anchorPoints to the bottom of the screen or adjust the ground position accordingly such as...
ground.position = CGPoint(x: 0 - self.size.width / 2 + ground.size.width / 2, y: 0 - self.size.height / 2 + ground.size.height / 2)
(the above example assumes you are adding the ground in the scene and self = scene)
for your reference...
Upvotes: 1