How to code the accelerometer to have a fixed x position - swift

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

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 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)

How to rotate SKNode in 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)

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 make joints not rotate using sprite kit or another way of making dynamic 2d water

I want to creat a dynamic 2D water, i', following this unity tutorials
http://gamedevelopment.tutsplus.com/tutorials/creating-dynamic-2d-water-effects-in-unity--gamedev-14143
http://gamedevelopment.tutsplus.com/tutorials/make-a-splash-with-2d-water-effects--gamedev-236
And also this one
http://blog.prime31.com/water2d-part1/
Here is my code
class GameScene: SKScene, SKPhysicsContactDelegate {
var box: SKSpriteNode!
var nodes:[SKNode] = []
override func didMoveToView(view: SKView) {
/* Setup your scene here */
self.anchorPoint = CGPointMake(0.5, 0.5)
// Set physics body
let borderBody: SKPhysicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame);
borderBody.friction = 0.0;
self.physicsBody = borderBody;
// Set contact delegate
self.physicsWorld.contactDelegate = self;
box = SKSpriteNode(color: UIColor.blackColor(), size: CGSize(width: 200, height: 20))
box.position = CGPointMake(0, 0)
box.physicsBody = SKPhysicsBody(rectangleOfSize: box.size)
box.physicsBody!.dynamic = false
self.addChild(box)
let one = SKSpriteNode(color: UIColor.greenColor(), size: CGSize(width: 20, height: 20))
one.position = CGPointMake(box.position.x - box.frame.size.width/2, box.position.y + box.frame.size.height * 2)
one.physicsBody = SKPhysicsBody(rectangleOfSize: one.size)
one.physicsBody!.allowsRotation = false
self.addChild(one)
nodes.append(one)
self.attachPoint(box, point2: one, box: true)
let two = SKSpriteNode(color: UIColor.greenColor(), size: CGSize(width: 20, height: 20))
two.position = CGPointMake(box.position.x, box.position.y + box.frame.size.height * 2)
two.physicsBody = SKPhysicsBody(rectangleOfSize: two.size)
two.physicsBody!.allowsRotation = false
self.addChild(two)
nodes.append(two)
self.attachPoint(box, point2: two, box: true)
let three = SKSpriteNode(color: UIColor.greenColor(), size: CGSize(width: 20, height: 20))
three.position = CGPointMake(box.position.x + box.frame.size.width/2, box.position.y + box.frame.size.height * 2)
three.physicsBody = SKPhysicsBody(rectangleOfSize: three.size)
three.physicsBody!.allowsRotation = false
self.addChild(three)
nodes.append(three)
self.attachPoint(box, point2: three, box: true)
self.attachPoint(one, point2: two, box: false)
self.attachPoint(two, point2: three, box: false)
}
func attachPoint(point1: SKSpriteNode, point2: SKSpriteNode, box: Bool){
if(box == true){
let newPoint1 = CGPointMake(self.frame.size.width/2 + point2.position.x, self.frame.size.height/2 + point1.position.y)
let newPoint2 = CGPointMake(self.frame.size.width/2 + point2.position.x, self.frame.size.height/2 + point2.position.y)
// create a joint between two bodies
let joint: SKPhysicsJointSpring = SKPhysicsJointSpring.jointWithBodyA(point1.physicsBody, bodyB: point2.physicsBody, anchorA: newPoint1, anchorB: newPoint2)
joint.damping = 2.0
joint.frequency = 9.0;
self.physicsWorld.addJoint(joint)
} else {
let newPoint1 = CGPointMake(self.frame.size.width/2 + point1.position.x, self.frame.size.height/2 + point1.position.y)
let newPoint2 = CGPointMake(self.frame.size.width/2 + point2.position.x, self.frame.size.height/2 + point2.position.y)
// create a joint between two bodies
let joint: SKPhysicsJointSpring = SKPhysicsJointSpring.jointWithBodyA(point1.physicsBody, bodyB: point2.physicsBody, anchorA: newPoint1, anchorB: newPoint2)
joint.damping = 2.0
joint.frequency = 9.0;
self.physicsWorld.addJoint(joint)
}
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */
let node = nodes[2]
node.physicsBody?.velocity.dy = 20000
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
Here is the result
This result without changes
After touch, execute code in touchesBegan method
After step 2, the nodes start to rotate and fall bellow the node
If they don't rate, following the the tutorials I will have to make SKShapeNode with the color of the node. But SKShapeNode is expensive, is there another way this effect can be accomplished?
The easiest way is to use image for the top part of the water and make it move back and forth, but it won't be dynamic..
If you have ever play Tiny Wings, the water there is implemented exactly the same way in the tutorials.
I don't know, maybe this can't be made in SpriteKit or I just don't know how