swift - Make Range of Movement SKSpriteNode
问题描述
So here is my code:
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)
}
}
}
I started a space shooting game and I wanted to set a range of movement so the SKSpriteNode can only move so far. I want to make the range a circular area. Does anyone know a way that I can do this? I searched google and stack overflow but no question are related. Just so you know, I am new to swift. It is probably a really easy answer but I couldn't find one. If anyone has ideas please answer.
解决方案
您可以像这样向节点添加约束:
func makeCircularRange(to node: SKNode) {
let range = SKRange(lowerLimit: 0, upperLimit: 100)
let constraint = SKConstraint.distance(range, to: .zero)
node.constraints = [constraint]
}
当然您可以更改值和中心,.zero
其他值仅用于示例。
在这个例子中,绿点有约束,红点没有。
推荐阅读
- python - 基于列值比较 2 个数据帧
- node.js - 与 mongoose 的事务(事务号仅允许在副本集成员或 mongos 上)
- c++ - 当给定相同的字符串输入时,`std::atof` 是否保证产生相同的输出?
- python - 使用烧瓶应用程序在一天中的特定时间安排功能
- mysql - 将具有相同 ID 的多行合并为一个
- excel - VBA:不正确的颜色
- ios - 仅限 iOS 设备在 webrtc 视频流应用程序中“未处理的承诺拒绝:OperationError:无法应用收到的候选者”
- python - 我在 heroku 上部署应用程序时遇到问题找不到满足 mysql-client==2.0.1 要求的版本
- c# - 在 lambda 上执行 distinct() 时替换 IEquatable 接口
- visual-studio - VS 2019 找不到 SDK