gpt4 book ai didi

ios - 将对象移动到特定区域

转载 作者:行者123 更新时间:2023-11-30 13:58:08 25 4
gpt4 key购买 nike

我在 View 中有一个名为 circle 的圆和一条名为 ground 的线。

enter image description here

我可以在整个 View 中拖动和移动圆圈,但是当我尝试限制它(在代码中)仅在地面区域下移动时 - 它有效(当我尝试将其拖动到地面上方时)并且圆圈仅通过我的 x 位置拖动移动,但是当我尝试将其拖回到地面下时 - 圆圈仅通过 x 位置拖动移动,而 y 位置拖动不会改变(y 是地面 y)。

如何让它以自由的方式再次移回地面下,而不仅仅是拖动的 x 位置?这是我迄今为止尝试过的:

let ground = SKSpriteNode()
var circle = SKShapeNode(circleOfRadius: 40)

override func didMoveToView(view: SKView) {
/* Setup your scene here */

// ground.physicsBody?.dynamic = false
ground.color = SKColor.redColor()
ground.size = CGSizeMake(self.frame.size.width, 5)
ground.position = CGPoint(x: CGRectGetMidX(self.frame), y: self.frame.size.height / 5)

self.addChild(ground)

circle.position = CGPointMake(CGRectGetMidX(self.frame) ,CGRectGetMinY(ground.frame) - (circle.frame.size.height / 2))
circle.strokeColor = SKColor.blackColor()
circle.glowWidth = 1.0
circle.fillColor = SKColor.orangeColor()

self.addChild(circle)

}
var lastTouch: CGPoint? = nil
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
let touch = touches.first
let touchLocation = touch!.locationInNode(self)
if circle.position.y < ground.position.y - (circle.frame.size.height / 2){
circle.position = touchLocation
} else {
circle.position.x = CGFloat(touchLocation.x)
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first
let touchLocation = touch!.locationInNode(self)
if circle.position.y < ground.position.y - (circle.frame.size.height / 2) {
circle.position = touchLocation
} else {
circle.position.x = CGFloat(touchLocation.x)
}
}

最佳答案

我已经解决了:

touchesMoved 函数中我编写了以下代码:

for touch in touches {
if touchLocation.y < ground.position.y {
circle.position = touchLocation
} else {
circle.position.x = touchLocation.x
}
}

关于ios - 将对象移动到特定区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33340690/

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