Reputation: 1
so im working on a kind of side scroller, first game with Xcode and swift but here is what i have:
import SpriteKit
import GameplayKit
let jumpButton = SKSpriteNode(imageNamed: "jumpButton")
var jumpButtonPosition = CGPoint(x: 500, y: -250)
var selectedButton = "nodeName"
let background1 = SKSpriteNode(imageNamed: "Woods_Background")
let background2 = SKSpriteNode(imageNamed: "Woods_Background")
let background3 = SKSpriteNode(imageNamed: "Woods_Background")
let player = SKSpriteNode(imageNamed: "player")
var playerColor = UIColor.red
var playerSize = CGSize(width: 50, height: 100)
var playerPhysicsBody = player.physicsBody
let playerJumpVector = CGVector(dx: 0, dy: 50)
let ground = SKSpriteNode()
var camPosition = CGPoint()
var playerPosition = camPosition
let cam = SKCameraNode()
var anchorpoint = CGPoint(x: 0.5, y: 0.5)
let endPoint = CGPoint(x: 100000, y: 0)
class GameScene: SKScene {
override func didMove(to view: SKView) {
super.didMove(to: view)
self.camera = cam
self.addChild(cam)
spawnPlayer()
spawnBackground()
spawnJumpButton()
}
func spawnGround(){
scene?.addChild(ground)
ground.size = CGSize(width: size.width * 1, height: size.height * 0.1)
ground.position = CGPoint(x: size.width * 0.5, y: size.height * 0.05)
ground.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: size.height, height: size.height * 0.1))
ground.physicsBody?.isDynamic = false
}
func spawnPlayer() {
scene?.addChild(player)
player.position = camPosition
player.zPosition = 1
player.size = playerSize
player.physicsBody = playerPhysicsBody
playerPhysicsBody?.affectedByGravity = false
playerPhysicsBody?.mass = 50
}
func spawnJumpButton() {
scene?.addChild(jumpButton)
jumpButton.position = jumpButtonPosition
jumpButton.zPosition = 0.5
jumpButton.size = CGSize(width: 100, height: 100)
jumpButton.name = "jumpButton"
jumpButton.isUserInteractionEnabled = false
}
func jumpPlayer() {
playerPhysicsBody?.applyImpulse(playerJumpVector)
}
func spawnBackground() {
scene?.addChild(background1)
background1.anchorPoint = CGPoint(x: 0.5, y: 0.5)
background1.position.x = 0
background1.zPosition = 0
background1.size = CGSize(width: 3240, height: 1080)
scene?.addChild(background2)
background2.anchorPoint = CGPoint(x: 0.5, y: 0.5)
background2.position.x = 3240
background2.zPosition = 0
background2.size = CGSize(width: 3240, height: 1080)
scene?.addChild(background3)
background3.anchorPoint = CGPoint(x: 0.5, y: 0.5)
background3.position.x = 6480
background3.zPosition = 0
background3.size = CGSize(width: 3240, height: 1080)
}
override func update(_ currentTime: TimeInterval) {
if cam.position.x > background2.position.x {
background1.position.x = background3.position.x + 3240
}
if cam.position.x > background3.position.x {
background2.position.x = background1.position.x + 3240
}
if cam.position.x > background1.position.x {
background3.position.x = background2.position.x + 3240
}
camPosition.x = playerPosition.x
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
if selectedButton == "nodeName" {
if jumpButton.contains(touch.location(in: self)) {
jumpPlayer()
}
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
selectedButton = "nodeName"
}
}
Im trying to get the player to jump with use of the button, when clicked the player should jump about 20 pixels in the air. same applies for the running motion of the player, cant get it to run forever without animations, with which you cant do other stuff at the same time.
Upvotes: 0
Views: 61
Reputation: 11537
The player.physicalBody is nil and need to handle!
func spawnPlayer() {
scene?.addChild(player)
player.position = camPosition
player.zPosition = 1
player.size = playerSize
//Here adding two lines
player.physicsBody = SKPhysicsBody()
playerPhysicsBody?.linearDamping = 1
playerPhysicsBody?.affectedByGravity = false
playerPhysicsBody?.mass = 50
}
func jumpPlayer() {
//Here changing one line
playerPhysicsBody?.applyImpulse(playerJumpVector)
}
Upvotes: 0