0

Please help me! Im trying to call a function I have declared in the GameScene class, within the update function. But it doesn't recognise the function, I'm wondering if this is something to do with the class or something because I want to run the function every frame (but has to update for each individual spriteCopy, depending on its own movement) to make sure the sprite copies all follow the function and continue to, infinitely.

Thank you in advance for any help.

here is the code for the function that sort of works to an extent:

    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)


    }

}

and here is the function not being recognised in function update:

override func update(_ currentTime: TimeInterval) {

    directionRotation() //this line has error saying "use of unresolved identifier" 

    // Called before each frame is rendered
}

EDIT: I was thinking maybe there could be a way to spawn multiple spriteCopy's without the "copy()" method that will not restrict the access to the spriteCopy's properties after they have been spawned? Whilst remembering they still must have to be individual SpriteNodes so that the directionRotation function could be applied independently to each of them (FYI: The user can spawn upwards of 50+ sprite nodes)

1 Answers1

6

You have specified local function. You need move out from touchUp function realisation directionRotation

func directionRotation() {
    if let body = spriteCopy.physicsBody {
        if (body.velocity.speed() > 0.01) {
            spriteCopy.zRotation = body.velocity.angle()
        }
    }
}
func touchUp(atPoint pos : CGPoint) {

 ...
}

EDIT

I mean you need do some think like this:

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) 
    }
}
Sergey
  • 1,589
  • 9
  • 13
  • I tried putting the direction rotation function above the touchup function, which meant I had to change the spriteCopy to "sprite" because the copy hasn't been made yet. but it didn't seem to work. even the initial angle when spawning isn't working. - Thanks for your help so far – Tasman Barr Mar 06 '17 at 11:54
  • I was thinking maybe there could be a way to spawn multiple spriteCopy's without the "copy()" method that will not restrict the access to the spriteCopy's properties after they have been spawned? but yet they would still have to be individual SpriteNodes so that the directionRotation function could be applied independently to each of them – Tasman Barr Mar 06 '17 at 12:09
  • Can you explain what you want implement? – Sergey Mar 06 '17 at 12:57
  • As I understand you want rotate an object in its direction of motion. Try to add **SKNode** parameter for this function and implement rotation for this parameter. You can create array with all body and apply this function for each body in update function, or you can try to use **node.children** to have access for nodes for rotation. – Sergey Mar 06 '17 at 13:07
  • I have a gravity field, in which the sprites can move freely within. So, to make the sprite more lively and realistic they should face there direction. Imagine a character with a face on the sprite, that I want to face forward. Also, what do you mean by "add SKNode parameter for this function"? And if I was going to go about using node.children how would I do so? – Tasman Barr Mar 06 '17 at 22:59
  • Just found a link to what your saying (http://stackoverflow.com/questions/27880935/loop-through-all-children-of-an-sknode) I think would you be able to explain what they are saying a little more (I've only been coding for 3 weeks) - thankyou – Tasman Barr Mar 06 '17 at 23:00