how do i make a node go from one side of the screen to the other? - swift

I'm just starting in sprite kit and have hit a road block. I'm trying to recreate the old atari game asteroids. I'm currently trying to find out how to move the node "ship" from one side of the screen and come out the opposite side. An example of this would be pacman going from the right side of the screen and out the left side of the screen. Help is greatly appreciated.
Thanks in advance,
Jared
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate{
let base = SKSpriteNode(imageNamed: "Base")
let ball = SKSpriteNode(imageNamed: "Ball")
let ship = SKSpriteNode(imageNamed: "Ship")
let shoot = SKSpriteNode(imageNamed: "shootButton")
override func didMoveToView(view: SKView){
// var DynamicView=UIView(frame: CGRectMake(100, 200, 100, 100))
// DynamicView.backgroundColor=UIColor.greenColor()
// DynamicView.layer.cornerRadius=2
// DynamicView.layer.borderWidth=2
// self.view!.addSubview(DynamicView)
self.anchorPoint = CGPointMake(0.5, 0.5)
self.physicsWorld.contactDelegate = self
self.physicsWorld.gravity = CGVectorMake(0.0, 0.0)
self.addChild(base)
base.position = CGPointMake(-350, -200)
self.addChild(shoot)
shoot.position = CGPointMake(350, -200)
self.addChild(ball)
ball.position = base.position
self.addChild(ship)
ship.position = CGPointMake(20, 47)
ship.xScale = 0.7
ship.yScale = 0.7
ship.physicsBody?.dynamic = true
ship.physicsBody?.allowsRotation = true
ship.physicsBody?.affectedByGravity = true
ship.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "Ship"), size: ship.size)
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
ball.alpha = 0.4
base.alpha = 0.4
}
// func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
// /* Called when a touch begins */
//
// for touch in (touches as! Set<UITouch>) {
// let location = touch.locationInNode(self)
//
//
// }
// }
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in (touches ) {
let location = touch.locationInNode(self)
let v = CGVector(dx: location.x - base.position.x, dy: location.y - base.position.y)
let angle = atan2(v.dy, v.dx)
let deg = angle * CGFloat(180 / M_PI)
// print( deg + 180)
let length:CGFloat = base.frame.size.height / 2
let xDist:CGFloat = sin(angle - 1.57079633) * length
let yDist:CGFloat = cos(angle - 1.57079633) * length
ball.position = CGPointMake(base.position.x - xDist, base.position.y + yDist)
if CGRectContainsPoint(base.frame, location) {
ball.position = location
}
else{
ball.position = CGPointMake(base.position.x - xDist, base.position.y + yDist)
}
ship.zRotation = angle - 1.57079633
ship.physicsBody?.mass = 2
var shipRotation : CGFloat = ship.zRotation
var calcRotation : Float = Float(angle - 1.57079633) + Float(M_PI_2);
let intensity : CGFloat = 2000.0 // put your value
let xVelocity = intensity * CGFloat(cosf(calcRotation))
let yVelocity = intensity * CGFloat(sinf(calcRotation))
let vector : CGVector = CGVectorMake(xVelocity, yVelocity)
//Apply force to spaceship
ship.physicsBody?.applyForce(vector)
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
let move:SKAction = SKAction.moveTo(base.position, duration: 0.2)
move.timingMode = .EaseOut
ball.runAction(move)
}
}
// overridefunc update(currentTime: CFTimeInterval) {
// /* Called before each frame is rendered */
//
//}

In func update(currentTime) check if ship.position.x < 0 or ship.position.x > scene.width. If true, set ship.position.x to the opposite side.

It looks like you're using physics to move the ship (as opposed to updating its position by a certain amount in the update() method)
So what you could do is to override the didiSimulatePhysics() method (which is called after the SpriteKit game engine has done all the physics calculations and moved all the nodes) and if your ship is off screen (which you could do by seeing if it's position is outside the screne's x & y boundaries, or by using the intersectsRect method to see if. The ship's frame no longer overlaps the screen frame) and if it is, simply wrap it to the other side of the screen.

Related

Move Sprite Based Off of Degrees

I've been able to detect degrees, but I'm unsure on how to move the sprite in 360 degrees.
I don't want the sprite to be able to only move in certain sections, as shown in the picture above, but rather have it be able to move in a full circle.
Code:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
if (ball.frame.contains(location)) {
stickActive = true
}else {
stickActive = false
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
if (stickActive == true) {
var v = CGVector(dx: location.x - base.position.x, dy: location.y - base.position.y)
let angle = atan2(v.dy, v.dx)
var deg = angle * CGFloat(180 / M_PI)
print(deg + 180)
let lenght:CGFloat = base.frame.size.height / 2 - 20
let xDist: CGFloat = sin(angle - 1.57079633) * lenght
let yDist: CGFloat = cos(angle - 1.57079633) * lenght
ball.position = CGPoint(x: base.position.x - xDist, y: base.position.y + yDist)
if (base.frame.contains(location)) {
ball.position = location
}else {
ball.position = CGPoint(x: base.position.x - xDist, y: base.position.y + yDist)
}
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if (stickActive == true) {
let move: SKAction = SKAction.move(to: base.position, duration: 0.2)
move.timingMode = .easeOut
ball.run(move)
}
}
This is super simple, although it took me quite awhile to figure out how to do it.
TouchesBegan method:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
if isTracking == false && DPad.contains(location) {
isTracking = true
}
}
}
TouchesMoved method:
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location: CGPoint = touch.location(in: self)
if isTracking == true {
v = CGVector(dx: location.x - DPad.position.x, dy: location.y - DPad.position.y)
let angle = atan2(v.dy, v.dx)
let deg = angle * CGFloat(180 / Double.pi)
let Length:CGFloat = DPad.frame.size.height / 2
let xDist: CGFloat = sin(angle - 1.57079633) * Length
let yDist: CGFloat = cos(angle - 1.57079633) * Length
xJoystickDelta = location.x - DPad.position.x
yJoystickDelta = location.y - DPad.position.y
if DPad.contains(location) {
thumbNode.position = location
} else {
thumbNode.position = CGPoint(x: DPad.position.x - xDist, y: DPad.position.y + yDist)
}
}
}
}
TouchesEnded method:
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
isTracking = false
thumbNode.run(SKAction.move(to: DPad.position, duration: 0.01))
xJoystickDelta = 0
yJoystickDelta = 0
}
The update(_ currentTime:) method:
if v.dx > abs(v.dy) {
yourPlayer.texture = SKTexture(imageNamed: "rightTexture")
} else if v.dx < -abs(v.dy) {
player.texture = SKTexture(imageNamed: "leftTexture")
} else if v.dy < 0 {
yourPlayer.texture = SKTexture(imageNamed: "frontTexture")
} else if v.dy > 0 {
yourPlayer.texture = SKTexture(imageNamed: "backTexture")
}
//This code moves your character where-ever you want it too
let xScale = CGFloat(4) //adjust to your preference. Higher means slower, lower means faster
let yScale = CGFloat(4) //adjust to your preference. Higher means slower, lower means faster
let xAdd = xScale * self.xJoystickDelta
let yAdd = yScale * self.yJoystickDelta
yourPlayerNode.position.x += xAdd
yourPlayerNode.position.y += yAdd
These things need to be outside your didMove(toView:) method:
var xJoystickDelta:CGFloat = 0
var yJoystickDelta:CGFloat = 0
var v = CGVector()
var isTracking:Bool = false
var DPad = SKSpriteNode()
var thumbNode = SKSpriteNode()
-Explanation-
In the touchesBegan method, the if-statement is testing to see if you are not controlling the thumbNode and if your touch is inside the DPad Node. Then it starts the tracking.
In the touchesMoved once isTracking == true it starts calculating the neccessary math and then adjusting the various things required. (its complicated, what matters most is that it works.)
In the touchesEnded method, it is testing to see when you lift your finger off of the screen, then it resets everything for the next usage.
In the update(_ current:) method, the code is calculating the angle of the CGVector and then setting a texture (or whatever you want to do) inside of the various cases. Then its calculating the position of the thumbNode inside the DPad and moving your player (or whatever you need to move) around in the scene. Adjust the xScale and yScale floats higher to slow down the movement, and lower to increase the movement of whatever you are trying to move.
-Extra neccessary stuff-
You need to set up the DPad and thumbNode inside your didMove(toView:) method:
thumbNode.size = CGSize(width: 50, height: 50)
DPad.size = CGSize(width: 150, height: 150)
DPad.position = CGPoint(x: 0, y: 0)
thumbNode.position = DPad.position
DPad.zPosition = 3
thumbNode.zPosition = 4
DPad.texture = SKTexture(imageNamed: "yourBaseTexture")
thumbNode.texture = SKTexture(imageNamed: "yourStickTexture")
self.addChild(thumbNode)
self.addChild(DPad)
You only have to move the DPad.position to wherever you want. The thumbNode will move along with it. Also, if you have any problems be sure to ask me so I can help you.

can't rotate a circle node around a fix point

I'm trying to set a SKShapeNode that rotating around fixed point depend on user's swipe gesture. to do so i'm using the code that answered here.
when I set the node as a rectangle - it's working just fine, the problem is when I trying to set the node as a circle (as I needed it to be). I have no idea why its happening, and what makes the difference.
here is the code that works (SKShapeNode as a rectangle):
override func didMove(to view: SKView) {
let ball = SKShapeNode(rect: CGRect(x: 100, y: 100, width: 100, height: 100))
ball.fillColor = SKColor.red
ball.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height/2)
ball.name = "BALL"
ball.physicsBody = SKPhysicsBody(circleOfRadius: 200)
ball.physicsBody?.angularDamping = 0.25
ball.physicsBody?.pinned = true
ball.physicsBody?.affectedByGravity = false
self.addChild(ball)
let om = 5.0
let er = 4.0
let omer = atan2(om, er)
print("om = \(om), er = \(er), atan2(omer) = \(omer)")
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in:self)
let node = atPoint(location)
if node.name == "BALL" {
let dx = location.x - node.position.x
let dy = location.y - node.position.y
// Store angle and current time
startingAngle = atan2(dy, dx)
startingTime = touch.timestamp
node.physicsBody?.angularVelocity = 0
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches{
let location = touch.location(in:self)
let node = atPoint(location)
if node.name == "BALL" {
let dx = location.x - node.position.x
let dy = location.y - node.position.y
let angle = atan2(dy, dx)
// Calculate angular velocity; handle wrap at pi/-pi
var deltaAngle = angle - startingAngle!
if abs(deltaAngle) > CGFloat.pi {
if (deltaAngle > 0) {
deltaAngle = deltaAngle - CGFloat.pi * 2
}
else {
deltaAngle = deltaAngle + CGFloat.pi * 2
}
}
let dt = CGFloat(touch.timestamp - startingTime!)
let velocity = deltaAngle / dt
node.physicsBody?.angularVelocity = velocity
// Update angle and time
startingAngle = angle
startingTime = touch.timestamp
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
startingAngle = nil
startingTime = nil
}
but when i change the SKShapeNode (inside my didMove function) to circle is just dont work - here is the changed code:
override func didMove(to view: SKView) {
let ball = SKShapeNode(circleOfRadius: 200)
ball.fillColor = SKColor.red
ball.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height/2)
ball.name = "BALL"
ball.physicsBody = SKPhysicsBody(circleOfRadius: 200)
ball.physicsBody?.angularDamping = 0.25
ball.physicsBody?.pinned = true
ball.physicsBody?.affectedByGravity = false
self.addChild(ball)
}
does anyone see/ know what am I dong wrong?
How would you expect to see that the shape is turning? Do you have something inside the shapenode with a texture that you can actually see rotating?
A turning circle shapenode looks the same as a static one. In the update loop of the scene grab the ball and print its zRotation to see if it's actually turning.
override func update(_ currentTime: TimeInterval) {
var ball: SKShapeNode = childNodeWithName("BALL") as SKShapeNode
print(ball.zRotation)
}
(this code was written without access to Swift so you might have to fix the syntax a tiny bit)
Remember the zRotation is measured in radians so it might not look how you expect but it it's changing then the SKShapeNode is actually rotating, you just can't see it.
You'll have to add a child node that has something visible on it.

How to move sprite with a joystick within a certain limit?

I want to make it so that the player can move with a joystick but cant go out of the circle. I made the joystick but I don't know how to do the other stuff. There is an example in the image below and also my code. Hope someone can help me, thanks.
class GameScene: SKScene {
var circuloPrincipal = SKSpriteNode(imageNamed: "circulo")
var circuloFondo = SKSpriteNode(imageNamed: "circuloFondo")
let base = SKSpriteNode(imageNamed: "circuloFondo")
let ball = SKSpriteNode(imageNamed: "circulo")
var stickActive:Bool = false
override func didMoveToView(view: SKView) {
/* Setup your scene here */
base.size = CGSize(width: 100, height: 100)
base.alpha = 0.3
base.zPosition = 2.0
base.position = CGPoint(x: frame.width / 2, y: frame.height / 2 - 310)
self.addChild(base)
ball.size = CGSize(width: 50, height: 50)
ball.color = circuloPrincipal.color
//ball.alpha = 0
ball.zPosition = 3.0
ball.position = base.position
self.addChild(ball)
circuloPrincipal.size = CGSize(width: 35, height: 35)
circuloPrincipal.position = CGPoint(x: frame.width / 2, y: frame.height / 2)
self.addChild(circuloPrincipal)
circuloPrincipal.color = UIColor(red: 0.75, green: 0.75, blue: 0.75, alpha: 1.0)
circuloPrincipal.colorBlendFactor = 1.0
circuloPrincipal.zPosition = 3.0
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
if (CGRectContainsPoint(ball.frame, location)) {
stickActive = true
}else {
stickActive = false
}
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
if (stickActive == true) {
let v = CGVector(dx: location.x - base.position.x, dy: location.y - base.position.y)
let angle = atan2(v.dy, v.dx)
let deg = angle * CGFloat(180 / M_PI)
print(deg + 180)
let lenght:CGFloat = base.frame.size.height / 2 - 20
let xDist: CGFloat = sin(angle - 1.57079633) * lenght
let yDist: CGFloat = cos(angle - 1.57079633) * lenght
ball.position = CGPointMake(base.position.x - xDist, base.position.y + yDist)
if (CGRectContainsPoint(base.frame, location)) {
ball.position = location
}else {
ball.position = CGPointMake(base.position.x - xDist, base.position.y + yDist)
}
} // termina stickActive
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
if (stickActive == true) {
let move: SKAction = SKAction.moveTo(base.position, duration: 0.2)
move.timingMode = .EaseOut
ball.runAction(move)
}
}
Look into SKConstraint - you can set a constraint that a node cannot move further than a specified distance from another node. You'd set this 'other' node to the centre of the area that your player is limited to. Sprite-Kit will automatically move your node back should it go to far. As this is being in the game loop at 60fps before the node is redrawn after it's moved, you won't get any weird 'jumping' effects.
https://developer.apple.com/reference/spritekit/skconstraint
That documentation is a bit lacking compared to some of the other SK documentation. Here's and example that make a green rectangle 'follow' a yellow triangle within a specified distance:
let distanceRange = SKRange(lowerLimit: 200, upperLimit: 400)
let distanceConstraint = SKConstraint.distance(distanceRange, toNode: yellowTriangle)
greenRect.constraints = [distanceConstraint]
The constraints property of an SKNode (greenRect) is an array, so if you also want an orientation constraint (to keep greenRect facing the yellow triangle, for example), you would code something like this:
let orientRange = SKRange(lowerLimit: 0.0, upperLimit: 0.0)
let orientConstraint = SKConstraint.orientToNode(yellowTriangle, offset: orientRange)
greenRect.constraints = [orientatConstraint, distanceConstraint]
For your particular example, you might want to set the constraint as:
let distanceRange = SKRange(lowerLimit: 0, upperLimit: limitcircle.size/2)
let distanceConstraint = SKConstraint.distance(distanceRange, toPoint: limitCircle.position)
player.constraints = [distanceConstraint]
This assumes that the circle from which the player cannot move is an SKNode called limitcircle and it's anchor point is set to (0.5, 0.5) i,e, it's center. The above code would constrain the player to a distance of between 0 and 1/2 the width of the circle (i.e. it's radius) from a point at the center of the circle.

How to make a node visible before any user interaction

I took and modified a piece of code which allows me to shoot bullets in every direction. It works perfectly. But it is generating a random bullet in the moment when player touch is ended. I have 6 different bullets and for purposes of my game I need player to see them before he will tap the screen. I tried to initialize a bullet in a global function and pass it to didMoveToView. It's not working, bullet is generating once and stays at its place.
That is my code for initializing a bullet:
class GameScene: SKScene, SKPhysicsContactDelegate {
var gameOver = false
let cannon = SKSpriteNode(imageNamed: "cannon")
let bulletInCannon = SKSpriteNode()
var endOfScreenRight = CGFloat()
var endOfScreenLeft = CGFloat()
let bmrArray = ["blBl", "magBl", "rBl"]
let cgyArray = ["cBl", "gBl", "yBl"]
let yrmArray = ["yBl", "rBl", "magBl"]
let bulletArray = ["rBullet","magBullet", "blBullet", "cBullet", "gBullet", "yBullet"]
override func didMoveToView(view: SKView) {
endOfScreenLeft = (self.size.width / 2) * CGFloat(-1)
endOfScreenRight = self.size.width / 2
cannon.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
self.addChild(cannon)
cannon.zPosition = 0
var randomBullet = Int(arc4random_uniform(6))
bulletInCannon.texture = SKTexture(imageNamed: bulletArray[randomBullet])
bulletInCannon.position = cannon.position
addChild(bulletInCannon)
bulletInCannon.zPosition = 1000
runAction(SKAction.repeatActionForever(SKAction.sequence([SKAction.runBlock(addCgyLine),
SKAction.waitForDuration(1.0)])))
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
let bullet = SKSpriteNode(texture: bulletInCannon.texture)
bullet.position = cannon.position
addChild(bullet)
let offset = location - bullet.position
let direction = offset.normalized()
let shootAmount = direction * 1000
let realDestination = shootAmount + bullet.position
let actionMove = SKAction.moveTo(realDestination, duration: 2.0)
let actionDone = SKAction.removeFromParent()
bullet.runAction(SKAction.sequence([actionMove, actionDone]))
var randomBullet = Int(arc4random_uniform(6))
bulletInCannon.texture = SKTexture(imageNamed: bulletArray[randomBullet])
}
}
func addCgyLine () {
var cgyBlock = SKSpriteNode(imageNamed: "cyanBox")
cgyBlock.position = CGPointMake(size.width + cgyBlock.size.width/2, CGRectGetMidY(self.frame) + 60)
addChild(cgyBlock)
var randomCGY = Int(arc4random_uniform(3))
cgyBlock.texture = SKTexture(imageNamed: cgyArray[randomCGY])
let actionMove = SKAction.moveTo(CGPoint(x: -cgyBlock.size.width/2, y: CGRectGetMidY(self.frame) + 60), duration: 3)
let actionDone = SKAction.removeFromParent()
cgyBlock.runAction(SKAction.sequence([actionMove, actionDone]))
SKActionTimingMode.EaseOut
}
How can I generate a bullet and only then shoot it? I will be glad to hear any ideas, thanks!
You can generate a bullet in a cannon as a global variable, and then create a new bullet in touchesEnded which will be the bullet that actually fires from the cannon. You set that new bullet's texture to the global bullet's texture. After firing the action, give the global bullet a new texture. If you need to have a reload time, then remove the global bullet temporarily, and use a Boolean to "lock" the cannon from firing until the time has elapsed.
Here's some code that I used. If the cannon moves, then make sure to update the position of the global bullet. The subtraction of CGPoint gives me an error, but I'm using Xcode 6.4.
Edit: Quicker and easier way by giving the bulletInCannon a value
var bulletInCannon = SKSpriteNode(imageNamed: "rBullet")
override func didMoveToView(view: SKView) {
var randomBullet = Int(arc4random_uniform(6))
bulletInCannon.texture = SKTexture(imageNamed: bulletArray[randomBullet])
bulletInCannon.position = cannon.position
addChild(bulletInCannon)
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
// Create bullet that will fire from the cannon
let bullet = SKSpriteNode(texture: bulletInCannon.texture)
bullet.position = cannon.position
addChild(bullet)
let offset = location - bullet.position
let direction = offset.normalized()
let shootAmount = direction * 1000
let realDestination = shootAmount + bullet.position
let actionMove = SKAction.moveTo(realDestination, duration: 2.0)
let actionDone = SKAction.removeFromParent()
bullet.runAction(SKAction.sequence([actionMove, actionDone]))
// Generate a random texture for the global bullet
var randomBullet = Int(arc4random_uniform(6))
bulletInCannon.texture = SKTexture(imageNamed: bulletArray[randomBullet])
}
}

Stop a ball sprite kit with Swift

I need to know how to make a ball slow down after two or three seconds to apply an impulse
func setupPlayer(){
player = SKSpriteNode(imageNamed: "ball")
player.anchorPoint = CGPoint(x: 0.5, y: 0.5)
player.position = CGPoint(x: size.width/2, y: playableStart)
let scale: CGFloat = 0.07
player.xScale = scale
player.yScale = scale
player.physicsBody = SKPhysicsBody(circleOfRadius: player.size.width / 2)
worlNode.addChild(player)
}
func movePlayer(){
player.physicsBody?.applyImpulse((CGVectorMake( 50, 50)))
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch in (touches as! Set<UITouch>) {
movePlayer()
}
}
One way to decelerate a physics body is to reduce its velocity over time:
override func update(currentTime: CFTimeInterval) {
// This controls how fast/slow the body decelerates
let slowBy:CGFloat = 0.95
let dx = player.physicsBody!.velocity.dx
let dy = player.physicsBody!.velocity.dy
player.physicsBody?.velocity = CGVectorMake(dx * slowBy, dy * slowBy)
}
Are you looking for something like this?
let wait = SKAction.waitForDuration(2.0)
let slow = SKAction.runBlock({
node.physicsBody?.applyImpulse(CGVectorMake(-node.physicsBody?.velocity.dx, -node.physicsBody?.velocity.dy))
//you will probably want to mess with the vector
})
node.runAction(SKAction.sequence([wait, slow]))