Changing the angle of a SKSpriteNode during a rotation - swift

I am trying to make a drifting game. In order for the car to drift, the car (when turning) needs to be at an angle. I have tried rotation, however this conflicts with the code I already have for turning the car. Here is my code, any help?
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let location = touch.previousLocation(in: self)
let position = touch.location(in: self)
let node = self.nodes(at: location).first
if position.x < 0 {
let rotate = SKAction.repeatForever(SKAction.rotate(byAngle: CGFloat(M_PI), duration: 0.8))
car.run(rotate, withKey: "rotating")
} else {
let rotate = SKAction.repeatForever(SKAction.rotate(byAngle: CGFloat(-M_PI), duration: 0.8))
car.run(rotate, withKey: "rotating")
}
}
}
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
car.position = CGPoint(x:car.position.x + cos(car.zRotation) * 3.0,y:car.position.y + sin(car.zRotation) * 3.0)
}
}
This code currently turns the car without adding any angle, or "drifting" effect.

one approach is you could nest your car inside another SKNode as a container. apply your steering rotation to the car node, as you're doing it now. then apply the drift rotation to the container node. the result effect will be the sum of the two.
//embed car inside a SKNode container so you can apply different rotations to each
let car = SKShapeNode(ellipseOf: CGSize(width: 20, height: 40)) //change to how you draw your car
let drift_container = SKNode()
drift_container.addChild(car) //embed car inside the container
self.addChild(drift_container) //`self` here is a SKScene
//apply angle rotation to the container
func drift(byAngle angle:CGFloat) {
let rotate = SKAction.rotate(toAngle: angle, duration: 0.2)
drift_container.run(rotate)
}
then in update be sure to update drift_container.position instead of the car's position

Related

Is there a way I can add more than one sprite in SpriteKit globally?

I am struggling with one issue. Global declaration of my sprite so that I can interact with it. In this game, I have created a local sprite called enemy featured below:
func spawnEnemy() {
let enemy = SKSpriteNode(imageNamed: "as")
let yPosition = CGFloat(frame.maxY - enemy.size.height)
let getXvalue = GKRandomDistribution(lowestValue: Int(frame.minX + enemy.size.width), highestValue: Int(frame.maxX - enemy.size.width))
let xPosition = CGFloat(getXvalue.nextInt())
enemy.position = CGPoint(x: xPosition, y: yPosition)
enemy.name = "asteroid"
enemy.zPosition = 100
addChild(enemy)
let animationDuration:TimeInterval = 6
var actionArray = [SKAction]()
actionArray.append(SKAction.move(to: CGPoint(x: xPosition, y: 0), duration: animationDuration))
actionArray.append(SKAction.self.removeFromParent())
enemy.run(SKAction.sequence(actionArray))
}
I want to tap the enemy to make it disappear from the screen. The variable is declared locally and not globally so the touchesBegan function does not "see" enemy. However, when I move the statement:
let enemy = SKSpriteNode(imageNamed: "as")
outside of local declaration and into global. It works until the code tries to spawn in another enemy and i get an error of "Tried to add an SKNode who already has a parent" This is the code I have running in my view did load:
run(SKAction.repeatForever(SKAction.sequence([SKAction.run{self.spawnEnemy()
}, SKAction.wait(forDuration: 1.0)])))
Every time it spawns a new enemy it crashes and says that the SKNode already has a parent which i understand. However, for my game to function I need the player to be able to touch the individual instance of that enemy and remove it. Hence my code for
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
if let location = touch?.location(in:self) {
let nodesArray = self.nodes(at:location)
if nodesArray.first?.name == "asteroid" {
print("Test")
enemy.removeFromParent()
print("Test Completed")
}
}
}
Now the error says unresolved use of "enemy" because the enemy is not global. I have been going back and forth on this issue for quite some time. If anyone has any potential solution or work around I would be very grateful, and thank you for your help.
Move your enemies to their own class and handle the touch for each of those enemies in their own class. This cleans up your GameScene and keeps your code more organized. You can now add as many instances of enemy as you want.
FYI not related to this question but somethings to consider after you get this working
when game over or level change or win make sure you have a clean up function to remove all enemies
you should strongly consider recycling your objects vs creating them on the fly...better performance
try to separate as much code to your objects class as possible
class enemy: SKSpriteNode {
init() {
super.init(texture: nil, color: .clear, size: CGSize.zero)
setup()
}
func setup() {
isUserInteractionEnabled = true
name = "asteroid"
zPosition = 100
let image = SKSpriteNode(imageNamed: "as")
imagine.zPosition = 1
addChild(image)
self.size = image.size
animate()
}
func animate() {
let animationDuration: TimeInterval = 6
let move = SKAction.move(to: CGPoint(x: xPosition, y: 0), duration: animationDuration)
let remover = SKAction.self.removeFromParent()
run(SKAction.sequence(move, remover))
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
removeFromParent()
}
}
class GameScene: SKScene {
override func didMove(to view: SKView) {
let sequence = SKAction.sequence([SKAction.run{ self.spawnEnemy()
}, SKAction.wait(forDuration: 1.0)])
run(SKAction.repeatForever(sequence))
}
func spawnEnemy() {
let enemy = Enemy()
let yPosition = CGFloat(frame.maxY - enemy.size.height)
let getXvalue = GKRandomDistribution(lowestValue: Int(frame.minX + enemy.size.width), highestValue: Int(frame.maxX - enemy.size.width))
let xPosition = CGFloat(getXvalue.nextInt())
enemy.position = CGPoint(x: xPosition, y: yPosition)
addChild(enemy)
}
}

SKCameraNode working weird

I'm trying to create a simple SpriteKit game. I have a player that starts from the bottom-center of the screen.
I added to my GameScene a camera to follow the player but it does not work. when I run it with this code:
class GameScene: SKScene {
let mPlayer = Player(circleOfRadius: 45, fillColor: myColors.blue, strokeColor: myColors.red)
let cameraNode = SKCameraNode()
override func didMove(to view: SKView) {
setup()
}
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
moveCamera()
Player.movePlayerUp(mPlayer: mPlayer)
}
func setup(){
mPlayer.position = CGPoint(x: self.frame.midX - PLAYER_WIDTH, y: self.frame.minY + PLAYER_HEIGHT)
self.camera?.position = mPlayer.position
self.camera = cameraNode
self.addChild(cameraNode)
self.addChild(mPlayer)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches{
let location = touch.location(in: self)
mPlayer.run(SKAction.moveTo(x: location.x, duration: 0.1))
}
}
func moveCamera(){
let moveAction = SKAction.moveTo(y: mPlayer.position.y, duration: 0.1)
cameraNode.run(moveAction)
}
}
the player is starting from the bottom and immediately moves to the center of the screen like in this picture:
How can I make the camera to follow the player from the start and move with it to the top?
Hope my question is clear and sorry for the size of the picture.
This code works for me in a empty Xcode project. You are probably thinking its not because your background image is 1 color and you are only moving the player briefly.
Also in your update method you set to move the player constantly to the middle, which is not necessary.
Try adding some stripes to the background to make it easier to see if you move and than add this code to the update method do slowly move the player up constantly.
let action = SKAction.move(by: CGVector(dx: 0, dy: 5), duration: 1)
mPlayer.run(action)
You will see the camera is following the player correctly.
If you need to center your camera differently to the player e.g on the y axis than you just have to increase/decrease the y position of the camera.
let moveAction = SKAction.moveTo(y: mPlayer.position.y + 200, duration: 0.1)
cameraNode.run(moveAction)
Hope this helps

Rotate SKSpritenode by a changing number of degrees

How can I define an SKAction, and then update the number of degrees my node will rotate? I am trying to define it with variables, but when I update the variable values the action doesn't update.
var degreesToRotate = 4
var direction = 1
let action = SKAction.rotate(byAngle: CGFloat(degreesToRotate * direction), duration: TimeInterval(2))
charector.run(SKAction.repeatForever(action))
direction = -1
import SpriteKit
import GameplayKit
//Extensions borrowed from here : http://stackoverflow.com/a/29179878/3402095
extension Int {
var degreesToRadians: Double { return Double(self) * .pi / 180 }
var radiansToDegrees: Double { return Double(self) * 180 / .pi }
}
extension FloatingPoint {
var degreesToRadians: Self { return self * .pi / 180 }
var radiansToDegrees: Self { return self * 180 / .pi }
}
let kActionKey = "rotate"
class GameScene:SKScene {
let purpleCube = SKSpriteNode(color: .purple, size: CGSize(width: 150, height: 150))
let yellowCube = SKSpriteNode(color: .yellow, size: CGSize(width: 150, height: 150))
override func didMove(to view: SKView) {
addChild(purpleCube)
purpleCube.position.y = purpleCube.size.height
purpleCube.name = "purple"
addChild(yellowCube)
yellowCube.position.y = -yellowCube.size.height
yellowCube.name = "yellow"
let rotate = SKAction.rotate(byAngle: CGFloat(-M_PI * 2.0), duration: 5)
let loop = SKAction.repeatForever(rotate)
purpleCube.run(loop, withKey: kActionKey)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
if let touch = touches.first {
let location = touch.location(in: self)
if let cube = atPoint(location) as? SKSpriteNode {
if let name = cube.name {
switch name {
case "purple":
if let action = purpleCube.action(forKey: kActionKey){
purpleCube.run(action.reversed(), withKey: kActionKey)
}
case "yellow":
if action(forKey: "rotating") == nil{
yellowCube.run(SKAction.rotate(byAngle: CGFloat(4.degreesToRadians), duration: 0.1), withKey: kActionKey)
}
default:
break
}
}
}
}
}
}
In this example, there are two nodes that have been rotated in a two different ways. Purple node is rotated constantly at certain speed in clockwise direction. To achieve this, I've created an action that rotates a sprite by 360 degrees... That would be one revolution, which will be repeated forever, thus the sprite will be rotated forever.
About the yellow node...It will be rotated by 4 degrees every time you tap on it. Currently you have to wait that sprite stop rotating so you can rotate it more. This is optional of course, I just wanted to show you the usefulness of action keys.
Rotation Direction
Since in SpriteKit 0 degrees specifies positive x-axis and a positive angle is in counterclockwise direction, I rotated purple cube by -360 degrees, which rotates the sprite in clockwise direction. To find out more about SpriteKit coordinate system, read this documentation section.
Radians Vs Degrees
As you can see, I am talking in degrees, rather than in radians... That is because it would be really hard to say, I rotated the sprite by 6.2831853072 radians :) That is why I used extensions which does conversion from degrees to radians and vice-versa. You might use this often so I added them for you.

(Xcode Swift SpriteKit) How do I move a sprite in the direction it is facing

I have a sprite in the centre of the screen which can be rotated left or right which is determined by touching the left or right part of the screen.
What I would like to do is have the sprite continuously moving forwards but always in the direction it is facing. I know how to do basic movement with SKActions etc... but have no idea how I calculate the movement to be continuously in the direction that the sprite has rotated to?
Maths has never been my strong point so would very much appreciate some sample code to help me along.
var player = SKSpriteNode()
override func didMove(to view: SKView) {
player = SKSpriteNode(imageNamed: "4B.png")
player.setScale(0.3)
player.zPosition = 100
self.addChild(player)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let position = touch.location(in: self)
if position.x < 0 {
let rotate = SKAction.repeatForever(SKAction.rotate(byAngle: CGFloat(M_PI), duration: 2))
player.run(rotate, withKey: "rotating")
} else {
let rotate = SKAction.repeatForever(SKAction.rotate(byAngle: CGFloat(-M_PI), duration: 2))
player.run(rotate, withKey: "rotating")
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
player.removeAction(forKey: "rotating")
}
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
}
}
You will need to use sin and cos. An SKAction may not be the best thing for you, so I would just do this in the update method for now till you find a better spot:
sprite.position = CGPoint(x:sprite.position.x + cos(sprite.zRotation) * 10,y:sprite.position.y + sin(sprite.zRotation) * 10)
Where 10 is the magnitude that you want the sprite to move (aka move 10 pixels)
This assumes that angle 0 means the sprite is looking right, angle 90 (PI/2) is looking up, angle 180 (PI) is looking left, and angle 270 (3PI/2) is looking down.

Rotate SkSpriteNode around internal point that is not .anchorPoint

So I'm using this code to rotate an object either clockwise or anti-clockwise.
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first as UITouch!
let touchPosition = touch.locationInNode(self)
let newRotationDirection : rotationDirection = touchPosition.x < CGRectGetMidX(self.frame) ? .clockwise : .counterClockwise
if currentRotationDirection != newRotationDirection && currentRotationDirection != .none {
reverseRotation()
currentRotationDirection = newRotationDirection
}
else if (currentRotationDirection == .none) {
setupRotationWith(direction: newRotationDirection)
currentRotationDirection = newRotationDirection
}
}
func reverseRotation(){
let oldRotateAction = ship.actionForKey("rotate")
let newRotateAction = SKAction.reversedAction(oldRotateAction!)
ship.runAction(newRotateAction(), withKey: "rotate")
}
func stopRotation(){
ship.removeActionForKey("rotate")
}
func setupRotationWith(direction direction: rotationDirection){
let angle : CGFloat = (direction == .clockwise) ? CGFloat(M_PI) : -CGFloat(M_PI)
let rotate = SKAction.rotateByAngle(angle, duration: 2)
let repeatAction = SKAction.repeatActionForever(rotate)
ship.runAction(repeatAction, withKey: "rotate")
}
My question is, this creates clockwise or anticlockwise rotation around the SKSpriteNode's anchor point.
However I would like the object to rotate about another point inside of the SKSprite (something like 4/5's of the way up from the base of the sprite image). I assume I cannot use a set point defined through CGPointMake as I would like the sprite to independently move around the screen and this would not work?
Any suggestions anyone?
Have you tried setting the anchorPoint property of your sprite to the point around which you want the sprite to rotate?
ship.anchorPoint = CGPoint(x: 0, y: 4/5)
Is this what you were looking for?