请帮帮我!我试图在更新函数中调用我在 GameScene 类中声明的函数。但它不识别该功能,我想知道这是否与类有关,或者因为我想在每一帧运行该功能(但必须为每个单独的 spriteCopy 更新,取决于它自己的运动)到确保 Sprite 副本都遵循函数并无限地继续。
提前感谢您的帮助。
这是在一定程度上起作用的函数代码:
func touchUp(atPoint pos : CGPoint) {
if let spriteCopy = self.sprite?.copy() as! SKShapeNode? {
spriteCopy.fillColor = UIColor.white
spriteCopy.position = initialTouch
spriteCopy.physicsBody?.restitution = 0.5
spriteCopy.physicsBody?.friction = 0
spriteCopy.physicsBody?.affectedByGravity = false
spriteCopy.physicsBody?.linearDamping = 0
spriteCopy.physicsBody?.angularDamping = 0
spriteCopy.physicsBody?.angularVelocity = 0
spriteCopy.physicsBody?.isDynamic = true
spriteCopy.physicsBody?.categoryBitMask = 1 //active
spriteCopy.isHidden = false
touchUp = pos
xAxisLength = initialTouch.x - touchUp.x
yAxisLength = initialTouch.y - touchUp.y
xUnitVector = xAxisLength / distanceBetweenTouch * power * 300
yUnitVector = yAxisLength / distanceBetweenTouch * power * 300
spriteCopy.physicsBody?.velocity = CGVector(dx: xUnitVector, dy: yUnitVector)
func directionRotation() {
if let body = spriteCopy.physicsBody {
if (body.velocity.speed() > 0.01) {
spriteCopy.zRotation = body.velocity.angle()
}
}
}
directionRotation() //When I run the function with this line, the spriteCopy
//is spawned initially with the right angle (in the direction
//of movement) but doesn't stay updating the angle
sprite?.isHidden = true
self.addChild(spriteCopy)
}
}
这里是函数更新中未被识别的函数:
override func update(_ currentTime: TimeInterval) {
directionRotation() //this line has error saying "use of unresolved identifier"
// Called before each frame is rendered
}
编辑:我在想也许有一种方法可以在没有“copy()”方法的情况下生成多个 spriteCopy,这样在生成后不会限制对 spriteCopy 属性的访问?同时记住它们仍然必须是单独的 SpriteNode,以便可以将 directionRotation 函数独立应用于它们中的每一个(仅供引用:用户可以生成超过 50 个 Sprite 节点)
最佳答案
您已指定局部函数。你需要移出touchUp功能实现directionRotation
func directionRotation() {
if let body = spriteCopy.physicsBody {
if (body.velocity.speed() > 0.01) {
spriteCopy.zRotation = body.velocity.angle()
}
}
}
func touchUp(atPoint pos : CGPoint) {
...
}
编辑
我的意思是你需要像这样思考:
func directionRotation(node:SKNode) {
if let body = node.physicsBody {
if (body.velocity.speed() > 0.01) {
node.zRotation = body.velocity.angle()
}
}
}
override func update(_ currentTime: TimeInterval) {
for node in self.children
{
directionRotation(node)
}
}
关于ios - 为什么我不能在函数更新中调用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42624491/