我正在制作平台游戏,但我遇到的问题是其中只有一个移动按钮有效。有人可以花一些时间来检查我的代码,看看它有什么问题吗?
我没有得到我的错误,我的猜测是我在更改sod变量时出现问题。当我运行应用程序并单击并按住“左键”时,它会移动一点但然后减慢到停止。就好像我更改变量的脚本只运行了一次,即使我把它按住了。有趣的是好吧,在我跑之前它很好。不幸的是,我不记得我改变了什么。
谢谢,詹姆斯
class GameScene: SKScene {
var alien = SKSpriteNode()
var left = SKSpriteNode()
var right = SKSpriteNode()
var jump = SKSpriteNode()
var level = SKSpriteNode()
var background = SKSpriteNode()
var myLabel = SKLabelNode(fontNamed:"Chalkduster")
var playerLight = SKLightNode()
var xspd :Double = 0
var touching :Bool = false
var alienFrame :String = "stand"
var textureFrames :Double = 0
var buttonTouching :Bool = true
var buttonCheck :Int = 0
var ninety :Double = 0
override func didMoveToView(view: SKView) {
self.physicsWorld.gravity = CGVectorMake(0, -9.8)
alien = SKSpriteNode(imageNamed: "alien_stand")
alien.physicsBody = SKPhysicsBody(circleOfRadius: alien.frame.height / 2)
alien.position = CGPoint(x: self.frame.width / 6, y: self.frame.height / 2)
alien.xScale = 0.7
alien.yScale = 0.7
alien.physicsBody?.affectedByGravity = true
alien.physicsBody?.dynamic = true
alien.physicsBody?.allowsRotation = false
alien.zPosition = 1
alien.zRotation = 0
self.addChild(alien)
left = SKSpriteNode(imageNamed: "left")
left.position = CGPoint(x: self.frame.width / 8, y: self.frame.height / 3.5)
left.physicsBody?.pinned = true
left.xScale = 2
left.yScale = 2
left.zPosition = 3
left.alpha = 0.4
self.addChild(left)
right = SKSpriteNode(imageNamed: "right")
right.position = CGPoint(x: self.frame.width / 3, y: self.frame.height / 3.5)
right.physicsBody?.pinned = true
right.xScale = 2
right.yScale = 2
right.zPosition = 4
right.alpha = 0.4
self.addChild(right)
jump = SKSpriteNode(imageNamed: "up")
jump.position = CGPoint(x: (self.frame.width / 8) * 7, y: self.frame.height / 3.5)
jump.physicsBody?.pinned = true
jump.xScale = 2
jump.yScale = 2
jump.zPosition = 5
jump.alpha = 0.4
self.addChild(jump)
myLabel.text = "Hello, World!";
myLabel.fontSize = 45;
myLabel.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame));
self.addChild(myLabel)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
touching = true
if let touch = touches.first {
if xspd > -40 && xspd < 40 {
buttonCheck = 0
if jump.containsPoint(touch.locationInNode(self)) {
alien.physicsBody?.applyImpulse(CGVectorMake(0, 250))
jump.alpha = 0.1
alien.texture = SKTexture(imageNamed: "alien_jump")
buttonCheck += 1
}
if left.containsPoint(touch.locationInNode(self)) {
xspd -= 6
left.alpha = 0.1
alien.xScale = -0.7
buttonCheck += 1
}
if right.containsPoint(touch.locationInNode(self)) {
xspd += 6
right.alpha = 0.1
alien.xScale = 0.7
buttonCheck += 1
}
if buttonCheck > 0 {
buttonTouching = true
}
else {
buttonTouching = false
}
}
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
touching = false
}
override func update(currentTime: CFTimeInterval) {
updatePositions()
playerLight.position = CGPoint(x: alien.position.x, y: alien.position.y)
textureFrames += 1
}
func updatePositions() {
myLabel.text = String(round(xspd))
alien.physicsBody?.applyImpulse(CGVector(dx: CGFloat(xspd), dy: 0))
if touching == true && buttonTouching == true && xspd > 0 {
if xspd > 0 {
ninety = 900
}
else {
ninety = -900
}
if alienFrame == "stand" {
alien.texture = SKTexture(imageNamed: "alien_walk1")
alienFrame = "walk1"
}
else {
if alienFrame == "walk1" {
if textureFrames > 9.9 / xspd {
alien.texture = SKTexture(imageNamed: "alien_walk2")
alienFrame = "walk2"
textureFrames = 0
}
}
else {
if alienFrame == "walk2" {
if textureFrames > 9.9 / xspd {
alien.texture = SKTexture(imageNamed: "alien_walk1")
alienFrame = "walk1"
textureFrames = 0
}
}
}
}
}
else {
if xspd < 0.6 && xspd > -0.6 {
alien.texture = SKTexture(imageNamed: "alien_stand")
}
else {
if xspd != 0 {
xspd = xspd * 0.85
}
}
right.alpha = 0.4
left.alpha = 0.4
jump.alpha = 0.4
}
}
}
答案 0 :(得分:2)
当你开始游戏时,xspd
变量的值从零开始。点击左侧节点时,从xspd
中减去6,在xspd
上得到-6的结果。此外,你有updatePositions()
函数,我猜想每一帧都被调用,在该函数中你使用基于xspd
值的向量来应用一个脉冲。由于你得到一个破坏条件(xspd
)的负&& xspd > 0
值,因此在点击左节点之后永远不会满足if条件,因此你永远不会获得任何具有初始速度的动画负x。
要修复动画,您可以将xspd
封装在abs()
中,该&& abs(xspd) > 0
将始终返回正数。
if xspd < 0.6 && xspd > -0.6 {
alien.texture = SKTexture(imageNamed: "alien_stand")
} else if touching == false && xspd != 0 { // Never attempt to decrease movement speed if the player is in fact touching a movement button
xspd = xspd * 0.85
}
下一个问题是你的播放器停止移动,因为如果点击并按住左侧节点,你将无法保持移动速度而无需重复点击左侧节点。
我的建议,您可以尝试以下方法:
ActionListener
我希望我能正确理解你的问题并且这很有帮助