gpt4 book ai didi

ios - 将 x 位置设置为 SKSpriteNode

转载 作者:行者123 更新时间:2023-11-30 12:48:53 26 4
gpt4 key购买 nike

您好,我正在开发一个简单的积木游戏。游戏看起来像这样。

enter image description here

在此图像的底部,您可以看到一个桨(黄色条)。在touchesBegan 上,我想获取触摸的x 位置并将挡板移动到该x 位置。

我尝试过,但收到错误。

2016-12-21 14:35:50.540 BrickOut[1347:38411] SKUtil.m: MGGetBoolAnswer is not available in the simulator.

X Location : (95.0, 670.33332824707)

fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

Swift 代码

import SpriteKit
import GameplayKit

class GameScene: SKScene {

var ball:SKSpriteNode!
var paddle:SKSpriteNode!

override func didMove(to view: SKView) {
ball = self.childNode(withName: "Ball") as! SKSpriteNode!
paddle = self.childNode(withName: "Paddle") as! SKSpriteNode!


ball.physicsBody?.applyImpulse(CGVector(dx: 50, dy: 50))
let border = SKPhysicsBody(edgeLoopFrom: (view.scene?.frame)!)
border.friction = 0
self.physicsBody=border

}



override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

for touch in touches{
let touchLocation = touch.location(in: self.view)
print("X Location : ", touchLocation)
paddle.position.x = touchLocation.x

}
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches{

let touchLocation = touch.location(in: self.view)
print("X Location moved: ", touchLocation)
paddle.position.x = touchLocation.x

}
}

}

有人可以帮我解决这个问题吗?我想将桨的 x 位置更改为触摸的 x 位置。

最佳答案

首先你可以忽略这个

2016-12-21 14:35:50.540 BrickOut[1347:38411] SKUtil.m: MGGetBoolAnswer is not available in the simulator.

接下来,在这些行中

ball = self.childNode(withName: "Ball") as! SKSpriteNode!
paddle = self.childNode(withName: "Paddle") as! SKSpriteNode!

你应该转换为SKSpriteNode而不是SKSpriteNode!

现在再次运行...您刚刚在场景加载时发生了崩溃,对吧?

这意味着在您的 sks 中,您没有放置名为 BallSKSpriteNode 和/或另一个 SKSpriteNode > 命名为Paddle

关于ios - 将 x 位置设置为 SKSpriteNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41258999/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com