Trail of nodes along a path as in hello world - swift

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

Related

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

Swift 3 Repeat Spawning of SKNode

I have a SKNode that spawns and moves across the screen. It all works as intended. I am, however, unsure how to make this occur multiple times. My desire is for the 'barrels' to spawn every 2 seconds. You can see my attempt in the code below.
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)
let distance = CGVector(dx: -self.frame.width, dy: 0)
let moveDistance = SKAction.move(by: distance, duration: 5)
Obstacle1.run(moveDistance)
}
let spawnObstacles = SKAction(addObstacle1())
let delay = SKAction.wait(forDuration: 2)
let sequence = SKAction.sequence([delay, spawnObstacles])
run(SKAction.repeatForever(sequence))
}
Begin with putting the addObstacle outside of didMove method. Then put the following in your didMove method:
_ = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(addObstacle1), userInfo: nil, repeats: true)
It should repeat your addObstacle every two seconds :)

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

SpriteKit game lag when the function touchesBegan is called

I'm creating my first game, similar to flappy bird. I want it to start when the screen is touched like the real game. But it lags for about a second and an half, the result is that you die without even playing. Here is my code:
override func didMoveToView(view: SKView) {
/* Setup your scene here */
//Here i init some stuff
distanceToMove = CGFloat(self.frame.size.width + 140)
movePipes = SKAction.repeatActionForever(SKAction.moveByX(-distanceToMove, y: 0, duration: NSTimeInterval(1.2)))
removePipes = SKAction.removeFromParent()
moveAndRemove = SKAction.sequence([movePipes,removePipes])
let delay = SKAction.waitForDuration(NSTimeInterval(0.6))
let spawn = SKAction.runBlock({() in self.initPipes()})
let spawnAndDelay = SKAction.sequence([spawn,delay])
spawnAndDelayForever = SKAction.repeatActionForever(spawnAndDelay)
}
TouchesBegan:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
let touch = touches.first! as UITouch!
let touchLocation = touch.locationInNode(self)
if state == GameState.Starting {
state = GameState.Playing
instNode.hidden = true
if state == GameState.Playing {
runAction(spawnAndDelayForever)
addChild(pipes)
initScore()
}
}
Pipes init:
func initPipes() {
let upper = UInt32(self.size.height - 250)
let pY = arc4random_uniform(upper) + 200
let pipePair = SKNode()
pipePair.position = CGPoint(x: self.frame.size.width + 70, y: 0)
//PIPE 1
let pipe1 = SKSpriteNode(color: SKColor.whiteColor(), size: CGSizeMake(70, 700))
pipe1.anchorPoint = CGPointMake(0, 0)
pipe1.position = CGPoint(x: 0, y: Int(pY))
pipe1.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(70, 700), center: CGPointMake(70/2, 700/2))
pipe1.physicsBody?.dynamic = false
pipe1.physicsBody?.affectedByGravity = false
pipe1.physicsBody?.categoryBitMask = PipeCategory
pipe1.physicsBody?.contactTestBitMask = PlayerCategory
pipe1.physicsBody?.collisionBitMask = PlayerCategory
pipePair.addChild(pipe1)
//PIPE 2
let pipe2 = SKSpriteNode(color: SKColor.whiteColor(), size: CGSizeMake(70, 700))
pipe2.anchorPoint = CGPointMake(0,1)
pipe2.position = CGPoint(x: 0, y: pipe1.position.y - 150)
pipe2.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(70, 700), center: CGPointMake(35, -700/2))
pipe2.physicsBody?.dynamic = false
pipe2.physicsBody?.affectedByGravity = false
pipe2.physicsBody?.categoryBitMask = PipeCategory
pipe2.physicsBody?.contactTestBitMask = PlayerCategory
pipe2.physicsBody?.collisionBitMask = PlayerCategory
pipePair.addChild(pipe2)
//SCORE
let scoreSprite = SKSpriteNode(color: SKColor.clearColor(), size: CGSize(width: 50, height: 150))
scoreSprite.anchorPoint = CGPointMake(0, 1)
scoreSprite.position = CGPointMake(pipe1.position.x + 10, pipe1.position.y)
scoreSprite.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: 50, height: 150), center: CGPointMake(25, -75))
scoreSprite.physicsBody?.categoryBitMask = GapCategory
scoreSprite.physicsBody?.contactTestBitMask = PlayerCategory
scoreSprite.physicsBody?.collisionBitMask = 0
scoreSprite.physicsBody?.dynamic = false
pipePair.addChild(scoreSprite)
pipePair.runAction(moveAndRemove)
pipes.addChild(pipePair)
}
It's pretty simple: in initPipes() i create the pipes and i run the action of moving and removing. In touchesBegan i call the action of spawning them.. But it's laggy when i touch the screen.
Run your app through the Time Profiler Instrument to find out where the lag is coming from. It'll give you detailed results (down to individual lines of code) that'll let you know where your issues are.
That'll be much more accurate than people on here guessing.