所以我正在开发一种侧面滚动器,第一款使用Xcode和swift的游戏,但这是我所拥有的:
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"
}
}
我试图让玩家使用按钮进行跳跃,当单击该按钮时,玩家应该在空中跳跃约20个像素。这同样适用于播放器的运行动作,如果没有动画,就无法使其永远运行,而不能同时进行其他操作。
答案 0 :(得分:0)
player.physicalBody为零,需要处理!
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)
}