我正在制作一款平台游戏,但我遇到了一个问题,即只有一个移动按钮可以使用。有人可以花点时间检查我的代码,看看它有什么问题吗?
我没有得到我的错误,我的猜测是我更改 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
}
}
}
最佳答案
当您开始游戏时,xspd
变量以零值开始。当点击左侧节点时,您从 xspd
中减去 6,从而在 xspd
上得到 -6 的结果。此外,您还有 updatePositions()
函数,我想它会在每一帧调用,在该函数中,您可以使用基于 xspd
值的向量来应用脉冲。之后的 if 条件在首先点击左侧节点时永远不会满足,因为你得到一个负的 xspd
值,它打破了条件 (&& xspd > 0
),因此你永远不会获取初始速度为负 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
}
希望我已经正确理解了您的问题并且这对您有所帮助
关于iphone - 机芯不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36423481/