为什么我不能在函数更新中调用函数?
问题描述:
请帮帮我!我试图调用我在GameScene类中声明的函数,在更新函数中。但它不识别函数,我想知道这是否与类或某事有关,因为我想每帧都运行该函数(但必须根据自己的运动更新每个单独的spriteCopy)以确保精灵副本都遵循该功能并继续无限地继续。为什么我不能在函数更新中调用函数?
非常感谢您的帮助。
这里是排序的作品在一定程度上对函数的代码:
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
}
编辑:我想也许可以有一种方法来产生多个spriteCopy的没有“副本()”方法,它们不会限制对spriteCopy的属性进行产生后的访问?同时记住它们仍然必须是单独的SpriteNodes,以便DirectionRotation函数可以独立应用于每个SpriteNode(FYI:用户可以产生超过50个Sprite节点)
答
您已指定本地函数。你需要从润色功能实现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)
}
}
我试图把方向旋转功能在touchup函数之上,这意味着我必须将spriteCopy更改为“sprite”,因为尚未创建副本。但它似乎没有工作。甚至在产卵不起作用时的初始角度。 - 感谢您的帮助到目前为止 –
我在想也许有可能有一种方法来产生多个spriteCopy的没有“副本()”方法,它们不会限制对spriteCopy的属性进行产生后的访问?但是它们仍然必须是单独的SpriteNodes,以便directionRotation函数可以独立应用于每个SpriteNode。 –
您能解释一下你想要实现什么吗? – Sergey