swiftspritecgpointtouchesmoved

Moving sprite with finger gesture


I'm trying to move the sprite (red dot) whenever I move my finger anywhere on the screen of the device. The following code seems to work only within a small invisible circle rather than in the whole area of the screen, meaning the sprite seems to encounter some sort of wall and doesn't go any further. Anyone can figure out why?

import SpriteKit
import GameplayKit
var positionX  = CGFloat()
var positionY  = CGFloat()
var newPositionX = CGFloat()
var newPositionY = CGFloat()
var redDotPosition = CGPoint()
var oldPosition = CGPoint()
var newPosition = CGPoint()
class GameScene: SKScene {
var redDot : SKSpriteNode?

var entities = [GKEntity]()
var graphs = [String : GKGraph]()

private var lastUpdateTime : TimeInterval = 0
private var label : SKLabelNode?
private var spinnyNode : SKShapeNode?

override func sceneDidLoad() {

    redDot = childNode(withName: "redDot") as? SKSpriteNode
    redDot?.texture = SKTexture(imageNamed: "redDot")
    redDot?.position = CGPoint(x: frame.midX , y: frame.midX)



}


    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

    for t in touches { self.touchMoved(toPoint: t.location(in: self))


        oldPosition = t.previousLocation(in: self)
        let oldPositionX = oldPosition.x
        let oldPositionY = oldPosition.y

        newPositionX = t.location(in: self).x
        newPositionY = t.location(in: self).y
        deltaX = newPositionX - oldPositionX
        deltaY = newPositionY - oldPositionY
        redDot?.position = CGPoint(x: redDot!.position.x + deltaX , y: redDot!.position.y + deltaY)


    }


}

Solution

  • Nevermind!

    the problem lied in the fact that I had accidentally anchored my sprite to the scene, therefore it wasn't moving more than a certain amount.