I want to make a simple platformer game in Xcode using Swift so that when I hold the left side of the screen, it continuously moves left until let go and vice versa. I have already added a jump feature but if you have any other suggestions I am open to take advice.
Here is my GameController.swift code:
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
let character = SKSpriteNode(imageNamed:"square.png")
let block = SKSpriteNode(imageNamed: "square.png")
override func didMoveToView(view: SKView) {
/* Setup your scene here */
//Physics
self.physicsWorld.gravity = CGVectorMake(0.0, -5.0)
self.physicsWorld.contactDelegate = self
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
//Character
character.position = CGPoint(x:CGRectGetMidX(self.frame),
y:CGRectGetMidY(self.frame))
character.setScale(0.25)
character.physicsBody = SKPhysicsBody(rectangleOfSize: character.size)
self.addChild(character)
//block
block.position = CGPoint(x: CGRectGetMidX(self.frame), y:
CGRectGetMidY(self.frame)*0.3)
block.setScale(0.2)
block.physicsBody = SKPhysicsBody(rectangleOfSize: block.size)
block.physicsBody?.dynamic = false
self.addChild(block)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
for touch:AnyObject in touches {
let location = touch.locationInNode(self)
if location.x < CGRectGetMidX(self.frame) && location.y <
CGRectGetMidY(self.frame)*0.7{
character.physicsBody?.applyImpulse(CGVector(dx: -50, dy: 0))
} else if location.x > CGRectGetMidX(self.frame) && location.y <
CGRectGetMidY(self.frame)*0.7{
character.physicsBody?.applyImpulse(CGVector(dx: 50, dy: 0))
} else if location.y > character.position.y + 15 {
character.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 50))
}
func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
}
func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
character.removeActionForKey("moveAction")
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch:AnyObject in touches {
let location = touch.locationInNode(self)
if location.x < CGRectGetMidX(self.frame) && location.y < CGRectGetMidY(self.frame)*0.7{
moveAction = SKAction.repeatActionForever(SKAction.moveByX(-20, y: 0, duration: 0.1))
let character.runAction(moveAction, withKey: "moveAction")
} else if location.x > CGRectGetMidX(self.frame) && location.y < CGRectGetMidY(self.frame)*0.7{
let moveAction = SKAction.repeatActionForever(SKAction.moveByX(20, y: 0, duration: 0.1))
character.runAction(moveAction, withKey: "moveAction")
} else if location.y > character.position.y + 15 {
let moveAction = SKAction.repeatActionForever(SKAction.moveByX(0, y: 20, duration: 0.1))
character.runAction(moveAction, withKey: "moveAction")
}
}
}
I edited the touchesBegan method and I added a touchesEnded method. You seem to have tried to embed touchesEnded (and update) into the touchesBegan, which you definitely cannot do.
Okay, so, the code is pretty self explanatory, but anyway:
Instead of what you had with the vectors, I added an SKAction that runs forever. Changing the duration will change how fast the object moves. A lower duration results in a faster object, and vice versa (because lower duration means less time to get somewhere, hence a faster speed). When touchesEnded is called, the action, whose key is "moveAction" is removed from character, thereby stopping it.
That's pretty much it. If you have any questions, please, tell me!
By the way, I'm not sure if you intended for this, but when you run the action for the object to move up, character ultimately ends up bouncing up and down because of the gravity.
Related
I would like to have the following interplay between line and ball in my game: a line gives direction and speed to the ball. The longer the line, the faster the ball.
What I have now: a ball is following the line and stops at the end of it. But it shouldn't stop here. Of course, I understand that the ball is only following the path I made. But how could I change it?
Here is my code:
import SpriteKit
import GameplayKit
class GameScene: SKScene, SKPhysicsContactDelegate {
// Basic for dynamic sizes step01
var width = CGFloat()
var height = CGFloat()
var ball:SKSpriteNode!
var line:SKShapeNode!
var startPoint: CGPoint!
var location = CGPoint()
override func didMove(to view: SKView) {
self.backgroundColor = .purple
//declare dynamic size of the screen
width = self.frame.size.width
height = self.frame.size.height
self.physicsBody = SKPhysicsBody(edgeLoopFrom: frame)
self.physicsWorld.gravity = CGVector.zero
createBall()
}
func createBall(){
ball = SKSpriteNode(imageNamed: "yellowBtn")
ball.position = CGPoint(x:0, y: -(height/2.5))
ball.size = CGSize(width: width/6, height: width/6)
self.addChild(ball)
}
func drawLine(endPoint:CGPoint){
if(line != nil ){ line.removeFromParent() }
startPoint = ball.position
let path = CGMutablePath()
path.move(to: startPoint)
path.addLine(to: endPoint)
line = SKShapeNode()
line.path = path
line.lineWidth = 5
line.strokeColor = UIColor.white
self.addChild(line)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if(line != nil ){ line.removeFromParent()
for touch in (touches ) {
let location = touch.location(in: self)
drawLine(endPoint: location)
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in (touches ) {
let location = touch.location(in: self)
drawLine(endPoint: location)
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in (touches ) {
location = touch.location(in: self)
drawLine(endPoint: location)
}
let moveAction = SKAction.move(to: location, duration: 10)
ball.run(moveAction)
}
override func update(_ currentTime: TimeInterval) {
}
}
I have found at least a part of answer to my question: I need to calculate CGVector manually (lastPoint - firstPoint) and then apply impulse to the ball:
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in (touches ) {
location = touch.location(in: self)
drawLine(endPoint: location)
}
let dx = location.x - startPoint.x
let dy = location.y - startPoint.y
let movement = CGVector(dx: dx, dy: dy)
ball.physicsBody = SKPhysicsBody(circleOfRadius: 50)
ball.physicsBody?.applyImpulse(movement)
}
Now I should find the second part: how to set the speed of the ball accordingly to the length of the line.
im currently working on an app in which your player which is a small round ball, gets dragged around the screen by your finger. Then every 10 seconds a enemy ball gets added in which automatically tracks your ball and follows it until it runs into it. this is the code I have for this so far. you can drag your ball around the screen, bu the enemy ball which is only showing up as a red x even though its file is in the assets gets spawned every frame and they just move to the position of where your ball was when it was spawned. is there any way I can fix this, any help is appreciated. the contact between the balls I will add later on.
import SpriteKit
import GameplayKit
class GameScene: SKScene {
var me = SKSpriteNode()
let enemy = SKSpriteNode (imageNamed: "ellipse 1")
override func didMove(to view: SKView) {
me = self.childNode(withName: "me") as! SKSpriteNode
let border = SKPhysicsBody (edgeLoopFrom: self.frame)
border.friction = 0
self.physicsBody = border
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches{
let location = touch.location(in: self)
me.run(SKAction.moveTo(x: location.x, duration: 0))
me.run(SKAction.moveTo(y: location.y, duration: 0))
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches{
let location = touch.location(in: self)
me.run(SKAction.moveTo(x: location.x, duration: 0))
me.run(SKAction.moveTo(y: location.y, duration: 0))
}
}
override func update(_ currentTime: TimeInterval) {
let enemy = SKSpriteNode (imageNamed: "ball 2")
enemy.position = CGPoint(x:667, y: -200)
enemy.run(SKAction.moveTo(x: me.position.x, duration: 1.5))
enemy.run(SKAction.moveTo(y: me.position.y, duration: 1.5))
enemy.zPosition = +1
addChild(enemy)
}
}
So the problem you're having is that your enemy ball images aren't loading, and you're seeing a red X instead?
If so you need to break your code that creates your sprite into steps. First load the image into a temporary variable, print it, and then use the image to create your sprite:
let enemyImage = imageNamed: "ellipse 1"
print("enemyImage = \(enemyImage)")
let enemy = SKSpriteNode (enemyImage)
If enemyImage is nil, you need to figure out why that is.
Here is a modified version of your code that'll spawn an enemy once every 10 seconds and have them follow you. I used the Spaceship graphic provided by Apple's SpriteKit template for the enemy. As for your issue with the enemy graphic not showing up I'd make sure that the image is named "ball 2" in the assets folder.
import SpriteKit
import GameplayKit
class Enemy: SKSpriteNode {
var enemySpeed: CGFloat = 4
}
class GameScene: SKScene {
var me: SKSpriteNode!
var enemies = [Enemy]()
override func didMove(to view: SKView) {
me = self.childNode(withName: "me") as! SKSpriteNode
let border = SKPhysicsBody (edgeLoopFrom: self.frame)
border.friction = 0
self.physicsBody = border
spawnEnemy() // Spawn an enemy to begin with
Timer.scheduledTimer(timeInterval: 10.0, target: self, selector: #selector(spawnEnemy), userInfo: nil, repeats: true) // Spawns an enemy every 10 seconds
}
func spawnEnemy() {
let enemy = Enemy(imageNamed: "Spaceship")
enemy.xScale = 0.25
enemy.yScale = 0.25
enemy.position = CGPoint(x: CGFloat(arc4random() % UInt32(size.width)) - size.width / 2, y: CGFloat(arc4random() % UInt32(size.height)) - size.height / 2)
enemy.zPosition = 1
addChild(enemy)
enemies.append(enemy)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
me.run(SKAction.move(to: location, duration: 0.05))
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
me.run(SKAction.move(to: location, duration: 0.05))
}
}
override func update(_ currentTime: TimeInterval) {
for enemy in enemies {
let angle = CGFloat(atan2f(Float(me.position.y - enemy.position.y), Float(me.position.x - enemy.position.x)))
enemy.zRotation = angle - CGFloat.pi / 2
enemy.position = CGPoint(x: enemy.position.x + cos(angle) * enemy.enemySpeed, y: enemy.position.y + sin(angle) * enemy.enemySpeed)
}
}
}
import SpriteKit
import GameplayKit
class GameScene: SKScene {
var me = SKSpriteNode()
override func didMove(to view: SKView) {
me = self.childNode(withName: "me") as! SKSpriteNode
let border = SKPhysicsBody (edgeLoopFrom: self.frame)
border.friction = 0
self.physicsBody = border
run(SKAction.repeatForever(SKAction.sequence([SKAction.run(createEnemy), SKAction.wait(forDuration: 10.0)])))
}
func createEnemy () {
let enemy = SKSpriteNode(imageNamed: "ball 1")
enemy.name = "enemy"
enemy.position = CGPoint(x:667, y: -200)
enemy.run(SKAction.moveTo(x: me.position.x, duration: 1.5))
enemy.run(SKAction.moveTo(y: me.position.y, duration: 1.5))
enemy.zPosition = +1
addChild(enemy)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches{
let location = touch.location(in: self)
me.run(SKAction.moveTo(x: location.x, duration: 0))
me.run(SKAction.moveTo(y: location.y, duration: 0))
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches{
let location = touch.location(in: self)
me.run(SKAction.moveTo(x: location.x, duration: 0))
me.run(SKAction.moveTo(y: location.y, duration: 0))
}
}
override func update(_ currentTime: TimeInterval) {
}
}
I need help with the movement in Swift. I have a SKSpriteNode and I need help with touch and drag movement left or right to move and the SKSpriteNode. But only the left and right. When I click on the other side than the x position of the picture is to just run over smoothly. Could anyone help how to do it? Thanks.
import SpriteKit
class PlayScene: SKScene {
let rocket = SKSpriteNode(imageNamed: "rocket")
override func didMoveToView(view: SKView) {
rocket.position = CGPoint(x: self.frame.width/2, y: self.frame.height * 0.8)
self.addChild(rocket)
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
rocket.position = CGPoint(x: location.x, y: self.frame.height * 0.8)
}
}
In this, the rocket animates for 1 second to the location of the touch and then moves with the touch if the user moves their finger.
class GameScene: SKScene {
let rocket = SKSpriteNode(imageNamed: "rocket")
override func didMoveToView(view: SKView) {
rocket.position = CGPoint(x: self.frame.width/2, y: self.frame.height * 0.8)
self.addChild(rocket)
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
rocket.position = location
}
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
let moveAction = SKAction.moveTo(location, duration: 1/*Or as long as you want it to move*/)
rocket.runAction(moveAction)
}
}
}
Hope this helps.
My code for my touches began function is this:
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
let moveBall = SKAction.moveToY(380, duration: 0.4)
let moveBalldown = SKAction.moveToY(293, duration: 0.4)
if ball.position.y == 293 {
ball.runAction(moveBall)
}
if ball.position.y == 380 {
ball.runAction(moveBalldown)
}
When I tap the screen, nothing happens.
I want the ball to move up and down depending on the tap.
The ball should move up on the first tap and then back down once it is at y = 380, and the screen is tapped again.
Here is the rest of the code that affects the ball.
var ball = SKSpriteNode(imageNamed: "ball")
self.ball.position = CGPoint(x:40, y:293)
self.addChild(self.ball)
self.ball.yScale = 0.17
self.ball.xScale = 0.17
self.physicsWorld.gravity = CGVectorMake(0, 0)
ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.height * 0.08)
ball.physicsBody?.dynamic = true
var degreeRotation = CDouble(self.SBEBSpeed) * M_PI / 180
self.ball.zRotation -= CGFloat(degreeRotation)
let moveBall = SKAction.moveToY(380, duration: 0.4)
let moveBalldown = SKAction.moveToY(293, duration: 0.4)
if ball.position.y == 293 {
ball.runAction(moveBall)
}
if ball.position.y == 380 {
ball.runAction(moveBalldown)
}
I haven't touched SpriteKit in some time but I wanted to test your code. I'm currently running XCode Version 6.3.1, and I see they've updated the touchesBegan function to now take a Set of NSObjects. But your actual code inside the method runs just fine, so as long as you have set your "ball" to the right coordinates when it is initially rendered. Here is my working example:
import SpriteKit
class GameScene: SKScene {
var ball = SKSpriteNode()
override func didMoveToView(view: SKView) {
var ballTexture = SKTexture(imageNamed: "ball.png")
ball = SKSpriteNode(texture: ballTexture)
ball.position = CGPoint(x: CGRectGetMidX(self.frame), y: 293)
self.addChild(ball)
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
let moveBall = SKAction.moveToY(380, duration: 0.4)
let moveBalldown = SKAction.moveToY(293, duration: 0.4)
if ball.position.y == 293 {
ball.runAction(moveBall)
}
if ball.position.y == 380 {
ball.runAction(moveBalldown)
}
}
EDIT: (Because I can't comment yet, and I didn't want to make a new answer) I ran your code after you updated it and the ball does not even appear on my screen. Is that happening to you when you say nothing is happening? The only problem was the x-position. I would recommend using coordinates that are relative of self.frame
I am making a simple side scrolling game using apple's swift programming language and I want to make my character move left when the left half of the screen is touched and right when the other half is touched. I have done this using this code:
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if location.x < CGRectGetMidX(self.frame){
character.position.x -= 30
} else if location.x > CGRectGetMidX(self.frame){
character.position.x += 30
} else {
println("jump")
}
}
but he stops moving immediately after he moves 30 px over. my question is, can someone explain how to make him keep moving until the user lifts their finger?
This is my GameScene.swift file:
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
let character = SKSpriteNode(texture: SKTexture(imageNamed: "character"))
override func didMoveToView(view: SKView) {
/* Setup your scene here */
//world
self.physicsWorld.gravity = CGVectorMake(0.0, -5.0)
//background
var background = SKSpriteNode(imageNamed: "background")
background.size.height = self.frame.size.height
background.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
self.addChild(background)
//character
character.position = CGPointMake(self.frame.size.width * 0.6, self.frame.size.height * 0.6)
character.setScale(0.015)
character.physicsBody = SKPhysicsBody(circleOfRadius: CGFloat(character.size.width / 2))
var charPos = character.position
character.physicsBody?.dynamic = true
self.addChild(character)
//platform 1
var platformTexture = SKTexture(imageNamed: "platform")
var platform = SKSpriteNode(texture: platformTexture)
platform.position = CGPointMake(self.frame.size.width * 0.6, CGRectGetMidY(self.frame))
platform.physicsBody = SKPhysicsBody(rectangleOfSize: platform.size)
platform.physicsBody?.dynamic = false
platform.setScale(0.25)
self.addChild(platform)
//platform 2
var platformTexture2 = SKTexture(imageNamed: "platform")
var platform2 = SKSpriteNode(texture: platformTexture2)
platform2.position = CGPointMake(self.frame.size.width * 0.4, self.frame.size.height * 0.3)
platform2.physicsBody = SKPhysicsBody(rectangleOfSize: platform2.size)
platform2.physicsBody?.dynamic = false
platform2.setScale(0.25)
self.addChild(platform2)
//platform main
var platformTexture3 = SKTexture(imageNamed: "platform")
var platform3 = SKSpriteNode(texture: platformTexture2)
platform3.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMinY(self.frame) + platform3.size.height / 3)
platform3.physicsBody = SKPhysicsBody(rectangleOfSize: platform3.size)
platform3.physicsBody?.dynamic = false
platform3.setScale(1)
platform3.size.width = platform3.size.width * CGFloat(2.0)
self.addChild(platform3)
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */
func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
}
You have to apply force constantly to character in every update: call in order to move it without stopping. You can also apply impulses in order to move character, or make it jump. Here is an example based on you code, to give you a basic idea how you can move characters using physics (or by changing velocity vector of a character manually). Look through the comments to find what is important.
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
let character = SKSpriteNode(texture: SKTexture(imageNamed: "pauseButton"))
//Boolean variable to store information about move signal (updated in touchesBegan and touchesEnded method)
var move = false
override func didMoveToView(view: SKView) {
/* Setup your scene here */
//world
self.physicsWorld.gravity = CGVectorMake(0.0, -5.0)
self.physicsWorld.contactDelegate = self; //don't forget to set contact delegate if you want to use contact detection and methods like didBeginContact and didEndContact
//background
//just a physical border so that character can't escape from us :)
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
//character
character.position = CGPointMake(self.frame.size.width * 0.6, self.frame.size.height * 0.6)
character.setScale(0.415)
character.physicsBody = SKPhysicsBody(circleOfRadius: CGFloat(character.size.width / 2))
var charPos = character.position
character.physicsBody?.dynamic = true
self.addChild(character)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
//Hold finger at upper area to move character constantly to the right.
if location.y > 400{
//moving allowed, force is applied in update method. read the docs about applyImpulse and applyForce methods and the differences between those two.
move = true
}else{
if location.x < CGRectGetMidX(self.frame){
character.physicsBody?.applyImpulse(CGVector(dx: -30, dy: 0))
//tap somewhere above this to make character jump
if(location.y > 250) {
character.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 50))
}
} else if location.x > CGRectGetMidX(self.frame){
character.physicsBody?.applyImpulse(CGVector(dx: 30, dy: 0))
//tap somewhere above this to make character jump
if(location.y > 250) {
character.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 50))
}
}
}
}
}
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
move = false
//character.physicsBody?.velocity = CGVector(dx: 0, dy: 0) //you can make character to stop by manually setting its velocity vector to (0,0)
}
override func update(currentTime: CFTimeInterval) {
if(move){character.physicsBody?.applyForce(CGVector(dx: 30, dy: 0))}
//if(move){character.position = CGPoint(x: character.position.x+1, y:character.position.y)} //not recommended if you need physics simulation (eg. collisions)
}
}
Note that you can change node's position in every update call to achieve what you want, like this :
if(move){character.position = CGPoint(x: character.position.x+1, y:character.position.y)}
But this way you will pull the node out of physics simulation and you can experience unexpected results. Search SO about this topic, there are some good posts about all this.
Hope this helps a bit.