gpt4 book ai didi

objective-c - 将对象移动到触摸位置

转载 作者:行者123 更新时间:2023-11-30 10:19:06 25 4
gpt4 key购买 nike

如何将“玩家”位置移动到用户触摸屏幕的位置?我希望他们能够通过点击不同的地方来不断移动它。这是我到目前为止所拥有的,但它不起作用:

类游戏场景:SKScene {

let player = SKSpriteNode(imageNamed: "Spaceship")

override func didMoveToView(view: SKView) {
// 2
backgroundColor = SKColor.whiteColor()
// 3
player.position = CGPoint(x: size.width/2, y: size.height/2)

// 4
addChild(player)




}

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */

for touch: AnyObject in touches {
let location = touch.locationInNode(self)

let player = SKSpriteNode(imageNamed:"Spaceship")
player.position = location




}
}

最佳答案

touchesBegan 中的 let player = SKSpriteNode(imageNamed:"Spaceship") 行中,您将创建 spaceship 节点的新实例。该节点不在场景中,设置新节点的位置不会更改现有玩家节点的位置。

只需尝试重置玩家节点的位置即可。

尝试

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {

let touch = touches.anyObject() as UITouch?
if let location = touch?.locationInNode(self)
{
player.position = location
}
}

关于objective-c - 将对象移动到触摸位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28312628/

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