制作运动范围SKSpriteNode
Make Range of Movement SKSpriteNode
所以这是我的代码:
import SpriteKit
import GameplayKit
class GameScene: SKScene {
var ship1 = [2,1]
var ship2 = [1,2]
let jonahSpriteNode = SKSpriteNode(imageNamed: "jonah_spaceship")
let georgeSpriteNode = SKSpriteNode(imageNamed: "george_spaceship")
override func didMove(to view: SKView) {
//var jonahFrames = [SKTexture]()
jonahSpriteNode.position = CGPoint(x: 30, y: frame.midY)
jonahSpriteNode.size = CGSize(width: 100.0, height: 100.0)
addChild(jonahSpriteNode)
georgeSpriteNode.position = CGPoint(x: 628, y: frame.midY)
georgeSpriteNode.size = CGSize(width: 100.0, height: 100.0)
addChild(georgeSpriteNode)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches{
var touchLocation = touch.location(in: self)
var angle1 = atan2(jonahSpriteNode.position.y - touchLocation.y , jonahSpriteNode.position.x - touchLocation.x)
var angle = angle1 - CGFloat(Double.pi / 1)
let rotate = SKAction.rotate(toAngle: angle, duration: 1.0)
let move = SKAction.move(to: CGPoint(x: touchLocation.x, y: touchLocation.y), duration: 2.5)
let sequence = SKAction.sequence([rotate, move])
jonahSpriteNode.run(sequence)
}
}
}
我开始了一个 space 射击游戏,我想设置一个移动范围,这样 SKSpriteNode 只能移动这么远。我想让范围成为一个圆形区域。有谁知道我可以做到这一点的方法吗?我搜索了 google 和堆栈溢出,但没有相关问题。你知道,我是 swift 的新手。这可能是一个非常简单的答案,但我找不到。如果有人有想法请回答。
您可以像这样向节点添加约束:
func makeCircularRange(to node: SKNode) {
let range = SKRange(lowerLimit: 0, upperLimit: 100)
let constraint = SKConstraint.distance(range, to: .zero)
node.constraints = [constraint]
}
当然可以更改值和中心,.zero
其他值仅供示例。
在这个例子中,绿点有限制,红点没有。
所以这是我的代码:
import SpriteKit
import GameplayKit
class GameScene: SKScene {
var ship1 = [2,1]
var ship2 = [1,2]
let jonahSpriteNode = SKSpriteNode(imageNamed: "jonah_spaceship")
let georgeSpriteNode = SKSpriteNode(imageNamed: "george_spaceship")
override func didMove(to view: SKView) {
//var jonahFrames = [SKTexture]()
jonahSpriteNode.position = CGPoint(x: 30, y: frame.midY)
jonahSpriteNode.size = CGSize(width: 100.0, height: 100.0)
addChild(jonahSpriteNode)
georgeSpriteNode.position = CGPoint(x: 628, y: frame.midY)
georgeSpriteNode.size = CGSize(width: 100.0, height: 100.0)
addChild(georgeSpriteNode)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches{
var touchLocation = touch.location(in: self)
var angle1 = atan2(jonahSpriteNode.position.y - touchLocation.y , jonahSpriteNode.position.x - touchLocation.x)
var angle = angle1 - CGFloat(Double.pi / 1)
let rotate = SKAction.rotate(toAngle: angle, duration: 1.0)
let move = SKAction.move(to: CGPoint(x: touchLocation.x, y: touchLocation.y), duration: 2.5)
let sequence = SKAction.sequence([rotate, move])
jonahSpriteNode.run(sequence)
}
}
}
我开始了一个 space 射击游戏,我想设置一个移动范围,这样 SKSpriteNode 只能移动这么远。我想让范围成为一个圆形区域。有谁知道我可以做到这一点的方法吗?我搜索了 google 和堆栈溢出,但没有相关问题。你知道,我是 swift 的新手。这可能是一个非常简单的答案,但我找不到。如果有人有想法请回答。
您可以像这样向节点添加约束:
func makeCircularRange(to node: SKNode) {
let range = SKRange(lowerLimit: 0, upperLimit: 100)
let constraint = SKConstraint.distance(range, to: .zero)
node.constraints = [constraint]
}
当然可以更改值和中心,.zero
其他值仅供示例。
在这个例子中,绿点有限制,红点没有。