gpt4 book ai didi

swift - 如何在一定范围内用操纵杆 move Sprite ?

转载 作者:可可西里 更新时间:2023-11-01 02:16:24 25 4
gpt4 key购买 nike

我想让玩家可以用操纵杆 move 但不能离开圆圈。我做了操纵杆,但我不知道怎么做其他的东西。下图中有一个示例,还有我的代码。希望有人能帮助我,谢谢。

class GameScene: SKScene {

var circuloPrincipal = SKSpriteNode(imageNamed: "circulo")
var circuloFondo = SKSpriteNode(imageNamed: "circuloFondo")
let base = SKSpriteNode(imageNamed: "circuloFondo")
let ball = SKSpriteNode(imageNamed: "circulo")
var stickActive:Bool = false



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

base.size = CGSize(width: 100, height: 100)
base.alpha = 0.3
base.zPosition = 2.0
base.position = CGPoint(x: frame.width / 2, y: frame.height / 2 - 310)
self.addChild(base)

ball.size = CGSize(width: 50, height: 50)
ball.color = circuloPrincipal.color
//ball.alpha = 0
ball.zPosition = 3.0
ball.position = base.position
self.addChild(ball)

circuloPrincipal.size = CGSize(width: 35, height: 35)
circuloPrincipal.position = CGPoint(x: frame.width / 2, y: frame.height / 2)
self.addChild(circuloPrincipal)
circuloPrincipal.color = UIColor(red: 0.75, green: 0.75, blue: 0.75, alpha: 1.0)
circuloPrincipal.colorBlendFactor = 1.0
circuloPrincipal.zPosition = 3.0
}

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


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

if (CGRectContainsPoint(ball.frame, location)) {

stickActive = true
}else {

stickActive = false

}

}
}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)

if (stickActive == true) {


let v = CGVector(dx: location.x - base.position.x, dy: location.y - base.position.y)
let angle = atan2(v.dy, v.dx)
let deg = angle * CGFloat(180 / M_PI)
print(deg + 180)

let lenght:CGFloat = base.frame.size.height / 2 - 20
let xDist: CGFloat = sin(angle - 1.57079633) * lenght
let yDist: CGFloat = cos(angle - 1.57079633) * lenght

ball.position = CGPointMake(base.position.x - xDist, base.position.y + yDist)

if (CGRectContainsPoint(base.frame, location)) {

ball.position = location
}else {

ball.position = CGPointMake(base.position.x - xDist, base.position.y + yDist)

}


} // termina stickActive

}

}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
if (stickActive == true) {

let move: SKAction = SKAction.moveTo(base.position, duration: 0.2)
move.timingMode = .EaseOut

ball.runAction(move)

}
}

最佳答案

查看 SKConstraint - 你可以设置一个约束,一个节点不能 move 超过与另一个节点的指定距离。您可以将此“其他”节点设置为您的玩家所限制区域的中心。 Sprite-Kit 会自动将您的节点移回原处。由于这是在节点 move 后重新绘制之前以 60fps 的速度进入游戏循环,因此您不会得到任何奇怪的“跳跃”效果。

https://developer.apple.com/reference/spritekit/skconstraint

与其他一些 SK 文档相比,该文档有点缺乏。这是一个使绿色矩形“跟随”指定距离内的黄色三角形的示例:

let distanceRange = SKRange(lowerLimit: 200, upperLimit: 400)
let distanceConstraint = SKConstraint.distance(distanceRange, toNode: yellowTriangle)
greenRect.constraints = [distanceConstraint]

SKNode (greenRect) 的 constraints 属性是一个数组,因此如果您还想要一个方向约束(例如,让 greenRect 面向黄色三角形),您可以编写如下代码:

let orientRange = SKRange(lowerLimit: 0.0, upperLimit: 0.0)
let orientConstraint = SKConstraint.orientToNode(yellowTriangle, offset: orientRange)
greenRect.constraints = [orientatConstraint, distanceConstraint]

对于您的特定示例,您可能希望将约束设置为:

let distanceRange = SKRange(lowerLimit: 0, upperLimit: limitcircle.size/2)
let distanceConstraint = SKConstraint.distance(distanceRange, toPoint: limitCircle.position)
player.constraints = [distanceConstraint]

这假设玩家无法 move 的圆圈是一个名为 limitcircle 的 SKNode,它的 anchor 设置为 (0.5, 0.5),即它的中心。上面的代码会将玩家限制在距离圆心点的圆宽度(即半径)的 0 到 1/2 之间。

关于swift - 如何在一定范围内用操纵杆 move Sprite ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38090705/

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