sprite kit - Trying to change an object position on touch using spritekit and swift -
Then I have this code as part of a view:
import Spraitkit class Gemssin: SKScene {override function didMoveToView (see: SKView) {// set the background color background color = SKColor.whiteColor () var player = SKShapeNode (circleOfRadius: 20) Player.position = CGPoint (X: size.width / 2 Wi-size .height / 2) Player.name = "player" Player.fillColor = SKColor.blackColor () Player.physicsBody = SKPhysicsBody (circleOfRadius: 20) Player.physicsBody .dynamic = false addChild (player)} // touch Override function touchesBegan (touches: NSSet, withEvent event: UIEvent) {h Touch = touches.anyObject () as the UITouch touchLocation = touch.locationInNode (self) Player.position = CGPoint (X: size.width * 0.2, Y: size.height * 0.2) Print ( "you touched") }} g
"use the unresolved identifier" player "
I Since the player was previously created in the didMoveToView function, he does not recognize the "Player" Touch in the Bulgan Function How can I solve this problem? Whenever you touch the screen anywhere, I can create an object in that situation.
Try It
var player = SKShapeNode (circleOfRadius: 20 ) ... override function didMoveToView (see: SKView) {Player.name = "player" Player.fillColor = SKColor.blackColor () Player.physicsBody = SKPhysicsBody (circleOfRadius: 20) Player.physicsBody .dynamic = false addChild (self. Player)} Function touchesMoved override? (Touchs: NSSet !, withEvent event: UIEvent) {go to touch = touches.anyObject () go to touchLocation = Touch.locationInNode (self) as self.Player.position = touchLocation.position}
Comments
Post a Comment