How to make objects move? - ios

This is my code I wrote in Xcode using Sprite Kit, Swift. I'm trying to make a game like ComboQuest. The game consists of a moving bar that moves left and right trying to hit objects. As I'm trying to recreate that left and right movement here is my code:
func rightSprite(){
let actionR = SKAction.moveByX(0.001, y: 0, duration: 0.01)
Sprite.runAction(SKAction.repeatActionForever(actionR))
}
func leftSprite(){
let actionR = SKAction.moveByX(-0.001, y: 0, duration: 0.01)
Sprite.runAction(SKAction.repeatActionForever(actionR))
}
These two functions are then activate in the TouchesBegan override func, but there is a small flaw. When you touch the screen to change the direction there is not a complete reaction.
How would you guys code this?

First off, make sure you have a valid instance of a SKSpriteNode. It would be helpful to see more of your code to see where you are creating the bar sprite you would like to move. For example, below I created a red rectangle sprite.
let bar = SKSpriteNode(color: UIColor.redColor(), size: CGSize(width: 50, height: 10))
As far as your actions, you need to call them on your instance of a sprite. I used the same bar sprite from the example above. Also, since you are setting an action to repeat forever, you will need to remove that action to get it to stop. It appears you want the bar to move left and right. You can either remove all actions as I have done, or you could name your actions, and just remove a certain one. For simplicity, I just removed all actions.
func rightSprite(){
let actionR = SKAction.moveByX(0.001, y: 0, duration: 0.01)
bar.removeAllActions()
bar.runAction(SKAction.repeatActionForever(actionR))
}
func leftSprite(){
let actionL = SKAction.moveByX(-0.001, y: 0, duration: 0.01)
bar.removeAllActions()
bar.runAction(SKAction.repeatActionForever(actionL))
}

Take a look a this :
override func didMoveToView(view: SKView) {
let sprite = SKSpriteNode(color: UIColor.purpleColor(), size: CGSize(width: 20, height:50))
var startPoint:CGPoint = CGPoint(x: 100, y: CGRectGetMidY(frame))
var endPoint:CGPoint = CGPoint(x: 300, y: CGRectGetMidY(frame))
let moveLeft = SKAction.moveTo(endPoint, duration:3)
let moveRight = SKAction.moveTo(startPoint, duration:3)
let sequence = SKAction.sequence([moveLeft,moveRight])
sprite.position = startPoint
addChild(sprite)
sprite.runAction(SKAction.repeatActionForever(sequence), withKey:"moving")
}
I assume that your scene and view are initialized and sized correctly (eg that you done something like this scene.size = skView.bounds.size ) so when you try this example, sprite will show up on screen (instead of possible off-screen because you probably loading a scene from a .sks file).
IMO this is the natural way when using SKAction to move a sprite from point A to point B continuously (like from a Combo Quest) and that is:
Create starting and ending point
Make separate actions for moving sprite left and right
Make a sequence to run those two actions one after another
Repeat that action forever
Run that action with key to give your self a way to stop an action
When you want to stop action, you should do something like this:
if(sprite.actionForKey("moving") != nil){
sprite.removeActionForKey("moving")
}

Related

How to create a physics body for an SKSpriteNode with an interior in Sprite Kit (Swift)

I am currently making an iOS game in which a ball must bounce within a frame defined by the below image:
The game relies on the below method to automatically create and assign a physics body to the frame surrounding the game.
frame.physicsBody = SKPhysicsBody(texture: frameTexture, size: frame.size)
This code, however, assigns a body that encompasses the outside of the image, rather than the interior, and thus treats the blank interior as a solid object. This, in turn, means that the ball cannot be inside the frame and is forced off the screen immediately after being spawned.
Is there any way to give a physics body an interior?
Another post here: SpriteKit SKPhysicsBody with Inner Edges aims to do something similar, but does not involve automatic wrapping around a texture (hence the new question)
You can use init(edgeLoopFrom:) initializer to achieve inner edge friction. you can use this code below:
override func didMove(to view: SKView) {
//Setup scene's physics body (setup the walls)
physicsBody = SKPhysicsBody(edgeLoopFrom: frame)
let back = SKSpriteNode(imageNamed: "1")
back.position = CGPoint(x: frame.midX, y: frame.midY)
back.size = CGSize(width: 500, height: 600)
back.zPosition = -3
let rect = CGRect(origin: CGPoint(x: -back.size.width/2, y: -back.size.height/2), size: back.size)
back.physicsBody = SKPhysicsBody(edgeLoopFrom: rect)
addChild(back)
//Add Red ball "inside" the back sprite
let red = SKShapeNode(circleOfRadius: 20)
red.fillColor = .red
red.strokeColor = .clear
red.position = back.position
red.physicsBody = SKPhysicsBody(circleOfRadius: 20)
red.physicsBody?.restitution = 1
red.physicsBody?.friction = 0
red.zPosition = 1
red.physicsBody?.affectedByGravity = false
addChild(red)
red.physicsBody?.applyImpulse(CGVector(dx: 20, dy: 15))
}
please follow this Question and have a look to the answer given below.
i am providing a screenshot of my project

SKAction.rotate not working?

I'm trying to create a node and animate it's zRotation property with an action, however, when attempting to run the action, the zRotation property of my node is not changed. I'm unsure why this isn't working.
import UIKit
import SpriteKit
let aim = SKSpriteNode()
print(aim.zRotation)
aim.zRotation = CGFloat(Double.pi/2)
print(aim.zRotation)
let myAction = SKAction.rotate(toAngle: CGFloat(3.14), duration: 0)
aim.run(myAction)
print(aim.zRotation)
image of code with output
An SKAction will only be evaluated when your node is in a scene that is in turn being displayed in a view:
An SKAction object is an action that is executed by a node in the
scene (SKScene). ... When the scene processes its nodes, actions
associated with those nodes are evaluated.
(From Apple's SKAction documentation)
At the moment you have neither of those things so the action remains dormant. For an example of adding nodes to a scene and displaying that scene in a playground see this from Swift Studies.
I guess you want to achieve something like this
Issues
There a a few errors in your code:
An action with duration 0 will instantly apply the change, better setting a greater value.
You are not presenting the SKView in Playground
Your SKSpriteNode has not image
Solution
In Playground select View > Assistant > Show Assistant Editor in order to open a panel on the right where we'll add the SKView.
Now add this code an wait for the red square to appear in the Assistant Editor.
import PlaygroundSupport
import SpriteKit
let sceneView = SKView(frame: CGRect(x:0 , y:0, width: 500, height: 500))
let scene = SKScene(size: CGSize(width: 500, height: 500))
scene.anchorPoint.x = 0.5
scene.anchorPoint.y = 0.5
sceneView.showsFPS = true
sceneView.presentScene(scene)
PlaygroundPage.current.liveView = sceneView
let square = SKShapeNode(rect: CGRect(x: 0, y: 0, width: 100, height: 100))
square.fillColor = .red
scene.addChild(square)
square.run(.rotate(toAngle: 3.14, duration: 20))

SKSpriteNode not scalling properly

I'm developing a small game using Swift & SpriteKit. When I add a SKSpriteNode for Restart button, it doesn't scale properly.
The size of Restart button is 100px in height and width. If I don't set scale , it covers the whole screen and make the screen appear white. I figured out that if I setScale to 0.005 only than it appears on screen, but not in proper size.
import Foundation
import SpriteKit
class EndScene: SKScene {
var restartBtn = SKSpriteNode()
override func didMoveToView(view: SKView) {
background()
restartGame()
}
func restartGame() {
restartBtn = SKSpriteNode(imageNamed: "restartBtn")
restartBtn.setScale(0.005)
restartBtn.position = CGPoint(x: self.size.width / 2, y: self.size.height / 4)
restartBtn.zPosition = 1
self.addChild(restartBtn)
}
func background() {
let bkg = SKSpriteNode(imageNamed: "Background")
bkg.size = self.frame.size
bkg.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2)
bkg.zPosition = -2
self.addChild(bkg)
}
}
Here is the output of this code,
Restart Button Output
UPDATE
I put scene!.scaleMode = .AspectFill right inside didMoveToView function and it helped in rendering the shape of the SpriteNode properly. But still I have to setScale(0.001) to make the size of Restart button fit in the screen. Can anyone assist me what line of code I'm still missing?
Instead of using .setScale try using restartBtn.size = CGSize(Width: 50, Height: 50)
This uses the Sprite Kit's resize formula.
I ran into this as well. It happened when I forgot to specify the size of the scene. Instead of calling initWithSize I just wrote init and the scene started to scale down all nodes by x1000. Nodes were visible, but they required a scale of 0.001

Vertical endless scrolling background in sprite kit

I'm making a game and I'd like to have my background scroll vertically endlessly but I have no clue how to start. I'm pretty new to coding and sprite kit especially so any help would be greatly appreciated.
Anyone know how I could go about doing this?
You want to have two nodes, one to start with and one that follows it. Lastly, loop forever.
func createGroundForward() {
let groundTexture = SKTexture(imageNamed: "Track")
for i in 0 ... 1 {
let ground = SKSpriteNode(texture: groundTexture)
ground.zPosition = -4
ground.position = CGPoint(x: 0, y: -groundTexture.size().height + (groundTexture.size().height + (groundTexture.size().height * CGFloat(i))))
addChild(ground)
let moveLeft = SKAction.moveBy(x:0 , y: -groundTexture.size().height, duration: 5)
let moveReset = SKAction.moveBy(x:0 , y: groundTexture.size().height, duration: 0)
let moveLoop = SKAction.sequence([moveLeft, moveReset])
let moveForever = SKAction.repeatForever(moveLoop)
ground.run(moveForever)
}
}
Then add the function to your didMove function (which is like viewDidLoad for Sprite).
override func didMove(to view: SKView) {
self.anchorPoint = CGPoint(x: 0.5, y: 0.5)
createGroundForward()
}
I will give you an answer conceptually to give you a place to start since you have not provided any code.
You will need multiple images that are the size of the screen and stack them on top of each other vertically.
You then want to move these images down point by point. When the top of one of the images reaches the bottom of the screen, put it back on top of your stack of images. This will cause a never ending scrolling of your background.
Best of luck!

show keyboard on SKNode touch

I am trying to do a registration scene inside a game on iOS. what I have so far is this:
Since I couldn't manage to put UITextField inside the Scene that I have, I tried to put SKNodes that activate the keyboard on touch. It would be very useful if someone could help me to put UITextFields inside my game scene (because it would be easier to get a registration scene, and with less code, and also with native support for this).
My code so far is this:
let nameForm:SKSpriteNode = SKSpriteNode(imageNamed: "RegistrationScene_TEST_textviews")
let usernameForm:SKSpriteNode = SKSpriteNode(imageNamed: "RegistrationScene_TEST_textviews")
let passwordForm:SKSpriteNode = SKSpriteNode(imageNamed: "RegistrationScene_TEST_textviews")
let passwordForm2:SKSpriteNode = SKSpriteNode(imageNamed: "RegistrationScene_TEST_textviews")
And when someone touch any of these nodes it should be managed with a method like this:
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
let touch = touches.anyObject()! as UITouch
let location = touch.locationInView(self.view)
for touch:AnyObject in touches{
if CGRectContainsPoint(nameForm.frame, touch.locationInNode(self)){
//Function to display the keyboard
//Function that saves the keystroke
//Function to display a nameLabel with the saved keystroke
}
//same for the other Forms
//methods for the buttons (DONE)
}
}
If there is any other way to achieve a text input into the game (to save game statistics or something useful for the gameplay) please let me know, any help would be very appreciated
EDIT:
According to Cristian Woerz, i made the following and this solved my problem.
let txtField:UITextField!
txtField = UITextField(frame: CGRect(x: 0, y: 0, width: 200, height: 30));
self.view?.addSubview(txtField!)
Normally, you shouldn't use UIKit-elements in a SpriteKit-game. But For this problem, you can use a UITextfield and add it to your view:
var txtField: UITextField = UITextField(frame: CGRect(x: 0, y: 0, width: 200, height: 30));
self.view.addSubview(txtField)
You could also make your UITextField semi transparent by setting the background-color alpha value to less then the default 1:
txtField.backgroundColor = UIColor(red: 125/255, green: 125/250, blue: 125/250, alpha: 0.5)

Resources