How to rotate SKNode in swift - swift

I want my SKNode To rotate like Image below :)
Image Here
Instead it is rotating around the bottom left corner of the screen!
Click Here To View Video of what is happening that I do not want
How do i get it to rotate counterClockWise or Clockwise in one position like image show above?
Thank you ahead of time for someone who can help me out. not sure if i have to change anchor points or what... thank you
Here is my code below in swift.
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var top = SKSpriteNode()
var bottom = SKSpriteNode()
var line = SKSpriteNode()
var RightSide = SKSpriteNode()
var LeftSide = SKSpriteNode()
var pointBar = SKSpriteNode()
var Second_point_Bar_For_First_Hoop = SKSpriteNode()
override func didMove(to view: SKView) {
physicsWorld.contactDelegate = self
createHoop()
}
func createHoop() {
top = SKSpriteNode(imageNamed: "top")
top.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2 + 15)
top.size = CGSize(width: 100, height: 60)
top.zPosition = 0
bottom = SKSpriteNode(imageNamed: "bottom")
bottom.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2 - 45)
bottom.size = CGSize(width: 100, height: 60)
bottom.zPosition = 2
LeftSide = SKSpriteNode()
LeftSide.position = CGPoint(x: bottom.position.x - 40, y: bottom.position.y)
LeftSide.size = CGSize(width: 10, height: 10)
LeftSide.zPosition = 0
LeftSide.color = UIColor.blue
RightSide = SKSpriteNode()
RightSide.position = CGPoint(x: bottom.position.x + 40, y: bottom.position.y)
RightSide.size = CGSize(width: 5, height: 10)
RightSide.zPosition = 0
RightSide.color = UIColor.blue
pointBar = SKSpriteNode()
pointBar.position = CGPoint(x: bottom.position.x, y: bottom.position.y + 10)
pointBar.size = CGSize(width: 90, height: 2)
pointBar.zPosition = 100
pointBar.color = UIColor.green
pointBar.zPosition = 100
Second_point_Bar_For_First_Hoop = SKSpriteNode()
Second_point_Bar_For_First_Hoop.position = CGPoint(x: top.position.x, y: top.position.y - 10)
Second_point_Bar_For_First_Hoop.size = CGSize(width: 90, height: 2)
Second_point_Bar_For_First_Hoop.zPosition = 100
Second_point_Bar_For_First_Hoop.color = UIColor.green
Second_point_Bar_For_First_Hoop.zPosition = 100
let hoopPair = SKNode()
hoopPair.addChild(top)
hoopPair.addChild(pointBar)
hoopPair.addChild(Second_point_Bar_For_First_Hoop)
hoopPair.addChild(bottom)
hoopPair.addChild(LeftSide)
hoopPair.addChild(RightSide)
let rotate = SKAction.rotate(byAngle: 1, duration: 5)
let repeatRotation = SKAction.repeatForever(rotate)
hoopPair.run(repeatRotation)
self.addChild(hoopPair)
}
override func update(_ currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}

By default, SKNode anchor point is always 0.5, 0.5. This means you need to work the positions so that everything goes off of the center of the node.
Now everything is going to be relative, so your top and bottom are relative to your hoop node.
Then you need to move the hoopnode position so that it is where you want it.
Here is the code to do that:
(Note I took out all needless code to get your image to rotate on center)
(Another Node: if size does not work, use frame.size)
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var top = SKSpriteNode() var bottom = SKSpriteNode() var line = SKSpriteNode() var RightSide = SKSpriteNode() var LeftSide = SKSpriteNode() var pointBar = SKSpriteNode() var Second_point_Bar_For_First_Hoop = SKSpriteNode()
override func didMove(to view: SKView) {
physicsWorld.contactDelegate = self
createHoop()
}
func createHoop() {
top = SKSpriteNode(imageNamed: "top")
top.size = CGSize(width: 100, height: 60)
top.position = CGPoint(x: 0, y: top.size.height/2)
top.zPosition = 0
bottom = SKSpriteNode(imageNamed: "bottom")
bottom.size = CGSize(width: 100, height: 60)
bottom.position = CGPoint(x: 0, y: -bottom.size.height/2)
bottom.zPosition = 2
let hoopPair = SKNode()
hoopPair.addChild(top)
hoopPair.addChild(bottom)
let rotate = SKAction.rotate(byAngle: 1, duration: 5)
let repeatRotation = SKAction.repeatForever(rotate)
hoopPair.position = CGPoint(x:self.size.width/2,self.size.height/2)
hoopPair.run(repeatRotation)
self.addChild(hoopPair) }
override func update(_ currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
} }

What is the anchor point of the sprite? Sprites rotate about their anchor point and yours appears to be set to (0,0) i.e. the bottom-left corner. If so, try changing it to (0.5,0.5)

Related

Trail of nodes along a path as in hello world

hi I have a node traveling back and forth along a path. I am trying to integrate the moving shapeNode as in the hello world example to trail my moving node. I'm using a trigger from frame update to trigger the copying of the shape node. And using the position of the travelling node.
The problem is that the trail has some sort of offset and i don't know where its coming from. I have tried to compensate but to no avail. I'm wondering if it might have anything to do with the actions. Thanks for reading.
I have tried looking at this link but i cannot translate
Here is my code so far
spriteKit xcode 9 swift 4 iPad Air
// GameScene.swift
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var borderBody = SKPhysicsBody()
var myMovingNode = SKSpriteNode()
var trailNode : SKShapeNode?
override func didMove(to view: SKView) {
//Define Border
borderBody = SKPhysicsBody(edgeLoopFrom: self.frame)
borderBody.friction = 0
self.physicsBody = borderBody
physicsWorld.gravity = CGVector(dx: 0.0, dy: 0.0)
physicsWorld.contactDelegate = self
//Define myMovingNode
myMovingNode = SKSpriteNode(imageNamed: "greenball")
myMovingNode.size.width = 50
myMovingNode.size.height = 50
let myMovingNodeBody:CGSize = CGSize(width: 50, height: 50)
myMovingNode.physicsBody = SKPhysicsBody.init(rectangleOf: myMovingNodeBody)
myMovingNode.zPosition = 2
addChild(myMovingNode)
//Make Path for myMovingNode to travel
let myPath = CGMutablePath()
myPath.move(to: CGPoint(x: 30, y: 30))
myPath.addLine(to: CGPoint(x: 500, y: 500))
/*This is another path to try*/
// myPath.addCurve(to: CGPoint(x: 800, y: 700), control1: CGPoint(x: 500, y: 30), control2: CGPoint(x: 50, y: 500))
/*This draws a line along myPath*/
let myLine = SKShapeNode(path: myPath)
myLine.lineWidth = 10
myLine.strokeColor = .green
myLine.glowWidth = 0.5
myLine.zPosition = 2
addChild(myLine)
/*This sets myMovingNode running along myPath*/
let actionForward = SKAction.follow(myPath, asOffset: false, orientToPath: true, duration: 10)
let actionReverse = actionForward.reversed()
let wait = SKAction.wait(forDuration: 1)
let actionSequence = SKAction.sequence([actionForward, wait, actionReverse, wait])
myMovingNode.run(SKAction.repeatForever(actionSequence))
/*This defines TrailNode and its actions*/
trailNode = SKShapeNode.init(rectOf: CGSize.init(width: 20, height: 20), cornerRadius: 10)
if let trailNode = trailNode {
trailNode.lineWidth = 1
trailNode.fillColor = .cyan
trailNode.run(SKAction.sequence([SKAction.wait(forDuration: 5),
SKAction.fadeOut(withDuration: 3),
SKAction.removeFromParent()]))
}//Eo if Let
}//eo overdrive
func timeFunction (){/*This is controlled by func update*/
let n = trailNode?.copy() as! SKShapeNode?
/*this is where i'm trying to compensate*/
let movingNodeX = myMovingNode.position.x
let movingNodeY = myMovingNode.position.y
let movingNodeOffSet = CGPoint(x: movingNodeX - 0, y: movingNodeY - 0)
n?.position = movingNodeOffSet
myMovingNode.addChild(n!)
}
var frameCounter = 0
override func update(_ currentTime: TimeInterval) {
// print( "Called before each frame is rendered")
if frameCounter == 10{frameCounter = 0; timeFunction()}
frameCounter = frameCounter + 1
}
}//class GameScene
hi in answer to my own question. It was the simplest. On the last line change from
myMovingNode.addChild(n!) to addChild(n!)

How to rotate sprites around a joint

I need to make the arms and hands rotate around the center of the hook, as shown in the image below without them separating or changing their shape (no changes in the angles between arms and hands just rotation at A), as in the image below:
I tried rotating the arms but this made them separate and change form. You can check out my code below:
let hookCategoryName = "hook"
let leftArmCategoryName = "leftArm"
let rightArmCategoryName = "rightArm"
let leftHandCategoryName = "leftHand"
let rightHandCategoryName = "rightHand"
let hookCategory : UInt32 = 0x1 << 0
let leftArmCategory : UInt32 = 0x1 << 1
let rightArmCategory : UInt32 = 0x1 << 2
let leftHandCategory : UInt32 = 0x1 << 3
let rightHandCategory : UInt32 = 0x1 << 4
extension Int {
var degreesToRadians: Double { return Double(self) * .pi / 180 }
}
extension FloatingPoint {
var degreesToRadians: Self { return self * .pi / 180 }
var radiansToDegrees: Self { return self * 180 / .pi }
}
class GameScene: SKScene, SKPhysicsContactDelegate {
var hook = SKSpriteNode(imageNamed: "hook")
var leftArm = SKSpriteNode(imageNamed: "arm")
var rightArm = SKSpriteNode(imageNamed: "arm")
var leftHand = SKSpriteNode(imageNamed: "leftHand")
var rightHand = SKSpriteNode(imageNamed: "rightHand")
override func didMove(to view: SKView) {
self.physicsWorld.gravity = CGVector(dx: 0, dy: 0)
self.physicsWorld.contactDelegate = self
var yellowBg = SKSpriteNode(imageNamed: "yellowBg")
yellowBg.position = CGPoint(x: frame.midX, y: frame.midY)
yellowBg.zPosition = 2
addChild(yellowBg)
hook.position = CGPoint(x: frame.midX, y: frame.midY + frame.midY/2)
hook.zPosition = 5
hook.name = hookCategoryName
hook.physicsBody = SKPhysicsBody(rectangleOf: hook.frame.size)
hook.physicsBody?.categoryBitMask = hookCategory
hook.physicsBody?.isDynamic = false
addChild(hook)
rightArm.anchorPoint = CGPoint(x: 0.5, y: 1)
rightArm.position = hook.position
rightArm.zPosition = 5
rightArm.name = rightArmCategoryName
rightArm.physicsBody = SKPhysicsBody(rectangleOf: rightArm.frame.size)
rightArm.physicsBody?.categoryBitMask = rightArmCategory
rightArm.physicsBody!.isDynamic = true
addChild(rightArm)
leftArm.anchorPoint = CGPoint(x: 0.5, y: 1)
leftArm.position = hook.position
leftArm.zPosition = 5
leftArm.name = leftArmCategoryName
leftArm.physicsBody = SKPhysicsBody(rectangleOf: leftArm.frame.size)
leftArm.physicsBody?.categoryBitMask = leftArmCategory
leftArm.physicsBody!.isDynamic = true
addChild(leftArm)
// leftHand
leftHand.anchorPoint = CGPoint(x: 0.5, y: 0.5)
leftHand.position = CGPoint(x: leftArm.frame.minX - 22, y: leftArm.frame.minY + 7) //CGPoint(x: armLeft.position.x, y: armLeft.position.y)
leftHand.zPosition = 5
leftHand.name = leftHandCategoryName
leftHand.physicsBody = SKPhysicsBody(rectangleOf: leftHand.frame.size)
leftHand.physicsBody?.categoryBitMask = leftHandCategory
leftHand.zRotation = CGFloat(Double(-30).degreesToRadians)//CGFloat(-Double.pi/6)
//armLeft.physicsBody?.categoryBitMask = armCategory
leftHand.physicsBody!.isDynamic = true
addChild(leftHand)
// rightHand
rightHand.anchorPoint = CGPoint(x: 0.5, y: 0.5)
rightHand.position = CGPoint(x: rightArm.frame.minX + 30, y: rightArm.frame.minY + 7) //CGPoint(x: armLeft.position.x, y: armLeft.position.y)
rightHand.zPosition = 5
rightHand.name = rightHandCategoryName
rightHand.physicsBody = SKPhysicsBody(rectangleOf: rightHand.frame.size)
rightHand.physicsBody?.categoryBitMask = rightHandCategory
rightHand.zRotation = CGFloat(Double(30).degreesToRadians)//CGFloat(-Double.pi/6)
//armLeft.physicsBody?.categoryBitMask = armCategory
rightHand.physicsBody!.isDynamic = true
addChild(rightHand)
leftArm.zRotation = CGFloat(Double(-45).degreesToRadians)
rightArm.zRotation = CGFloat(Double(45).degreesToRadians)
rightHand.physicsBody?.contactTestBitMask = rightHandCategory
leftHand.physicsBody?.contactTestBitMask = leftHandCategory
rightHand.physicsBody?.collisionBitMask = rightHandCategory
leftHand.physicsBody?.collisionBitMask = leftHandCategory
let hookAndRightArmJoint = SKPhysicsJointPin.joint(withBodyA: hook.physicsBody!, bodyB: rightArm.physicsBody!, anchor: CGPoint(x: hook.position.x, y: self.rightArm.frame.maxY))
self.physicsWorld.add(hookAndRightArmJoint)
let hookAndLeftArmJoint = SKPhysicsJointPin.joint(withBodyA: hook.physicsBody!, bodyB: leftArm.physicsBody!, anchor: CGPoint(x: hook.position.x, y: self.leftArm.frame.maxY))
self.physicsWorld.add(hookAndLeftArmJoint)
let armsFixedJoint = SKPhysicsJointFixed.joint(withBodyA: leftArm.physicsBody!, bodyB: rightArm.physicsBody!, anchor: CGPoint.zero)
self.physicsWorld.add(armsFixedJoint)
//left arm and hand joint
let leftArmAndHandJoint = SKPhysicsJointPin.joint(withBodyA: leftArm.physicsBody!, bodyB: leftHand.physicsBody!, anchor: CGPoint(x: self.leftArm.frame.minX, y: self.leftArm.frame.minY)/*CGPoint(x: armLeft.position.x, y: self.armLeft.frame.minY)*/)
self.physicsWorld.add(leftArmAndHandJoint)
//right arm and hand joint
let rightArmAndHandJoint = SKPhysicsJointPin.joint(withBodyA: rightArm.physicsBody!, bodyB: rightHand.physicsBody!, anchor: CGPoint(x: self.rightArm.frame.maxX, y: self.rightArm.frame.minY)/*CGPoint(x: armLeft.position.x, y: self.armLeft.frame.minY)*/)
self.physicsWorld.add(rightArmAndHandJoint)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
hook.run(SKAction.rotate(byAngle: CGFloat(Double(60).degreesToRadians), duration: 0.5))
}
}
Also rotating the hook has no effect on the arms and hands as seen in the image below when the above code is run:
How can I get the rotation in the image first image?
You can make the arms and hands children of a common parent node. You could create a blank SKNode for just this purpose like so:
let armsParent = SKNode()
Then instead of adding the arms and hands as children to the scene directly, add them as children of armsParent like so:
armsParent.addChild(leftArm)
armsParent.addChild(rightArm) // same for hands...
Then you can simply rotate armsParent with an SKAction to achieve what you want.
OR, to make it even simpler, you could just add the arms and hands as children to hook directly like this:
hook.addChild(leftArm) // same for other arm and hands...
Since the arms and hands are children of hook or armParent their positions will now be determined relative to their parent. So you might have to change all your .position = initialization code to accommodate this.

How to keep a 30˚ distance between two lines anchored at a point

I am trying to create two lines that are anchored at a certain point (sprite) and rotate to form a 30 degree angle between them. Below is an image what I want to achieve.
This is what I've done so far:
extension Int {
var degreesToRadians: Double { return Double(self) * .pi / 180 }
}
extension FloatingPoint {
var degreesToRadians: Self { return self * .pi / 180 }
var radiansToDegrees: Self { return self * 180 / .pi }
}
class GameScene: SKScene, SKPhysicsContactDelegate {
var anchorSprite = SKSpriteNode(imageNamed: "anchorSprite")
var armLeft = SKSpriteNode(imageNamed: "lineSprite")
var armRight = SKSpriteNode(imageNamed: "lineSprite")
override func didMove(to view: SKView) {
self.physicsWorld.gravity = CGVector(dx: 0, dy: -1.8)
self.physicsWorld.contactDelegate = self
var tealBg = SKSpriteNode(imageNamed: "tealBg")
tealBg.position = CGPoint(x: frame.midX, y: frame.midY)
tealBg.zPosition = 10
addChild(tealBg)
anchorSprite.position = CGPoint(x: frame.midX, y: frame.midY + frame.midY/2)
anchorSprite.zPosition = 20
anchorSprite.physicsBody = SKPhysicsBody(rectangleOf: anchorSprite.frame.size)
anchorSprite.physicsBody?.categoryBitMask = pinCategory
anchorSprite.physicsBody?.isDynamic = false
addChild(anchorSprite)
armRight.anchorPoint = CGPoint(x: 0.5, y: 1)
armRight.position = anchorSprite.position
armRight.zPosition = 20
armRight.physicsBody = SKPhysicsBody(rectangleOf: armRight.frame.size)
armRight.zRotation = CGFloat(Double(15).degreesToRadians)//CGFloat(Double.pi/6)
armRight.physicsBody!.isDynamic = true
addChild(armRight)
armLeft.anchorPoint = CGPoint(x: 0.5, y: 1)
armLeft.position = anchorSprite.position
armLeft.zPosition = 20
armLeft.physicsBody = SKPhysicsBody(rectangleOf: armRight.frame.size)
armLeft.zRotation = CGFloat(Double(-15).degreesToRadians)//CGFloat(-Double.pi/6)
armLeft.physicsBody!.isDynamic = true
addChild(armLeft)
//Pin joints
var pinAndRightArmJoint = SKPhysicsJointPin.joint(withBodyA: anchorSprite.physicsBody!, bodyB: armRight.physicsBody!, anchor: CGPoint(x: anchorSprite.position.x, y: self.armRight.frame.maxY))
self.physicsWorld.add(pinAndRightArmJoint)
var pinAndLeftArmJoint = SKPhysicsJointPin.joint(withBodyA: anchorSprite.physicsBody!, bodyB: armLeft.physicsBody!, anchor: CGPoint(x: anchorSprite.position.x, y: self.armLeft.frame.maxY))
self.physicsWorld.add(pinAndLeftArmJoint)
}
Below is an image from running the above code (they are close together).
How can I make sure the lines are always 30˚ apart and maintain 30˚ apart even when rotated?
To keep your two lines separated by exactly 30°, you can use an SKPhysicsJointFixed, which is just what it sounds like: it pins two physicsBodies together in a fixed position. Since you already have them positioned the way you want, just add this code where you have the other SKPhysicsJoints to hold them that way:
let fixArms = SKPhysicsJointFixed.joint(withBodyA: armLeft.physicsBody!, bodyB: armRight.physicsBody!, anchor: CGPoint.zero)
self.physicsWorld.add(fixArms)
Result:
If you make the line nodes children of the anchor sprite (instead of the scene), rotating the anchor sprite node will rotate all the lines along with it without doing anything special with physics. You just need to mind the anchor points so that they align properly (i.e. line's anchor at its extremity rather than center)

Swift 3 Expect Argument Error

I am creating some functions in order to make a game similar to Flappy Bird. I am a complete beginner and am trying to understand everything fully as I go before moving on. I have been able to get my obstacle to move but when I attempt to put it into a function to allow me more flexibility later on with multiple obstacles I receive an error.
'Cannot convert type '()' to expected argument type 'SKAction'
class GameScene : SKScene, SKPhysicsContactDelegate {
var Player = SKSpriteNode()
var Ground = SKSpriteNode()
var Roof = SKSpriteNode()
var Background = SKSpriteNode()
let Obstacle1 = SKSpriteNode(imageNamed: "Fire Barrel 1")
override func didMove(to view: SKView) {
// Create Background Color
backgroundColor = bgColor
// Set World Gravity
self.physicsWorld.gravity = CGVector(dx: 0.0, dy: -4.0)
// Create Player
Player = SKSpriteNode(imageNamed: "Player")
Player.setScale(0.5)
Player.position = CGPoint(x: -self.frame.width / 2 + 100, y: -Player.frame.height / 2)
self.addChild(Player)
// Create Ground
Ground = SKSpriteNode(imageNamed: "BGTileBtm")
Ground.anchorPoint = CGPoint(x: 0,y: 0.5)
Ground.position = CGPoint(x: -self.frame.width / 2, y: -self.frame.height / 2)
self.addChild(Ground)
// Create Roof
Roof = SKSpriteNode(imageNamed: "BGTileTop")
Roof.anchorPoint = CGPoint(x: 1,y: 1)
Roof.position = CGPoint(x: -self.frame.width / 2, y: self.frame.height / 2 - Roof.frame.height)
Roof.zRotation = CGFloat(M_PI)
self.addChild(Roof)
// Set Physics Rules
Player.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "Player"), size: Player.size)
Player.physicsBody!.affectedByGravity = true
Player.physicsBody!.allowsRotation = false
Ground.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "Ground"), size: Ground.size)
Ground.physicsBody!.affectedByGravity = false
Ground.physicsBody!.isDynamic = false
// Obstacle
func addObstacle1(){
Obstacle1.position = CGPoint(x: self.frame.width / 2, y: -self.frame.height / 2 + Obstacle1.frame.height)
Obstacle1.zPosition = 1
addChild(Obstacle1)
}
func moveObstacle1(){
let distance = CGVector(dx: -self.frame.width, dy: 0)
let moveDistance = SKAction.move(by: distance, duration: 5)
run(moveDistance)
}
addObstacle1()
Obstacle1.run(moveObstacle1())
}
Change the declaration of moveObstacle1 to this:
func moveObstacle1() -> SKAction{
let distance = CGVector(dx: -self.frame.width, dy: 0)
let moveDistance = SKAction.move(by: distance, duration: 5)
return moveDistance
}
EDIT:
Regarding your comment,
run is a method. When you call it, it runs the SKAction you passed in. That's it! What you are trying to do is run(moveObstacle1()). What does that mean exactly? How can you pass a method call as a parameter? At runtime, the return value of moveObstacle1() is passed to run. In other words, for run(moveObstacle1()) to compile, moveObstacle1() must return a value using the return statement. And that value must be of type SKAction, since that's the thing you're passing to run.
return is used to return a value from a moveObstacle1(), so that you can call run(moveObstacle1()).
run is just a regular old method.
Change:
Obstacle1.run()
To:
Obstacle1.run(SKAction(moveObstacle1()))
The error message is pretty clear, you need to pass a SKAction to your SKSpriteNode.
Edit
import SpriteKit
import GameplayKit
class GameScene : SKScene, SKPhysicsContactDelegate {
var Player = SKSpriteNode()
var Ground = SKSpriteNode()
var Roof = SKSpriteNode()
var Background = SKSpriteNode()
let Obstacle1 = SKSpriteNode(imageNamed: "Spaceship")
let sprite = SKSpriteNode(imageNamed:"Spaceship")
override func didMove(to view: SKView) {
// Create Background Color
backgroundColor = UIColor.green
// Set World Gravity
self.physicsWorld.gravity = CGVector(dx: 0.0, dy: -4.0)
// Create Player
Player = SKSpriteNode(imageNamed: "Player")
Player.setScale(0.5)
Player.position = CGPoint(x: -self.frame.width / 2 + 100, y: -Player.frame.height / 2)
self.addChild(Player)
// Create Ground
Ground = SKSpriteNode(imageNamed: "BGTileBtm")
Ground.anchorPoint = CGPoint(x: 0,y: 0.5)
Ground.position = CGPoint(x: -self.frame.width / 2, y: -self.frame.height / 2)
self.addChild(Ground)
// Create Roof
Roof = SKSpriteNode(imageNamed: "BGTileTop")
Roof.anchorPoint = CGPoint(x: 1,y: 1)
Roof.position = CGPoint(x: -self.frame.width / 2, y: self.frame.height / 2 - Roof.frame.height)
Roof.zRotation = CGFloat(M_PI)
self.addChild(Roof)
// Set Physics Rules
Player.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "Player"), size: Player.size)
Player.physicsBody!.affectedByGravity = true
Player.physicsBody!.allowsRotation = false
Ground.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "Ground"), size: Ground.size)
Ground.physicsBody!.affectedByGravity = false
Ground.physicsBody!.isDynamic = false
sprite.size = CGSize(width:50, height: 50)
sprite.position = CGPoint(x:self.frame.midX, y:self.frame.midY);
self.addChild(sprite)
Obstacle1.run(SKAction(moveObstacle1()))
}
func moveObstacle1(){
let action = SKAction.moveTo(x: self.frame.size.width * 2, duration: 20)
sprite.run(action)
}
}

How to code the accelerometer to have a fixed x position

I have a ship that moves based on the direction of gravity which is changed by the accelerometer, but I want it to only move along the width of the screen (I already know how to do that), but I do not know how to keep it on a fixed horizontal line. Here's my code
class GameScene: SKScene {
var manager = CMMotionManager()
var ship = SKSpriteNode()
override func didMoveToView(view: SKView) {
/* Setup your scene here */
let shipTexture = SKTexture(imageNamed: "EvadersShipVert2.png")
ship = SKSpriteNode(texture: shipTexture)
ship.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame) - 250)
ship.size = CGSize(width: 90, height: 115)
shipTexture.filteringMode = SKTextureFilteringMode.Nearest
ship.zPosition = 2
ship.physicsBody = SKPhysicsBody(texture: shipTexture, size: CGSize(width: 90, height: 115))
ship.physicsBody?.dynamic = true
self.addChild(ship)
manager.startAccelerometerUpdates()
manager.accelerometerUpdateInterval = 0.1
manager.startAccelerometerUpdatesToQueue(NSOperationQueue.mainQueue()) {
(data, error) in
self.physicsWorld.gravity = CGVectorMake(CGFloat((data?.acceleration.x)!), CGFloat((data?.acceleration.y)!))
}
}
thanks in advance!
Replace
self.physicsWorld.gravity = CGVectorMake(CGFloat((data?.acceleration.x)!), CGFloat((data?.acceleration.y)!))
With
self.physicsWorld.gravity = CGVectorMake(0.0, CGFloat((data?.acceleration.y)!)
So you're not affecting the x position