用SpriteKit抛出一个对象

时间:2015-05-22 11:03:20

标签: ios swift sprite-kit physics

目前我有以下代码。即使代码构建成功,我似乎无法让它工作。我试图这样做,当你轻弹对象时,它会以你的开始和结束触摸的速度移动。

import SpriteKit
class GameScene: SKScene {
    var sprite: SKSpriteNode!
    var touchPoint: CGPoint = CGPoint()
    var touching: Bool = false
    override func didMoveToView(view: SKView) {
        self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
        sprite = SKSpriteNode(color: UIColor.redColor(), size: CGSize(width: 50, height: 50))
        sprite.physicsBody = SKPhysicsBody(rectangleOfSize: sprite.size)
        sprite.position = CGPoint(x: self.size.width/2.0, y: self.size.height/2.0)
        self.addChild(sprite)
    }

    //for touch: AnyObject in touches {
    //let location = touch.locationInNode(self)
    //let touchedNode = self.nodeAtPoint(location)

     override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        for touch: AnyObject in touches {

        let location = touch.locationInNode(self)
        if sprite.frame.contains(location) {
            touchPoint = location
            touching = true
        }
    }
    func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
        for touch: AnyObject in touches {
        let location = touch.locationInNode(self)
        touchPoint = location
    }
    func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
        touching = false
    }
    func update(currentTime: CFTimeInterval) {
        if touching {
            let dt:CGFloat = 1.0/60.0
            let distance = CGVector(dx: touchPoint.x-sprite.position.x, dy: touchPoint.y-sprite.position.y)
            let velocity = CGVector(dx: distance.dx/dt, dy: distance.dy/dt)
            sprite.physicsBody!.velocity = velocity
        }
    }
}}}

3 个答案:

答案 0 :(得分:2)

您不小心将touchesMovedtouchesEndedupdate放在touchesBegan内。除了你的代码工作。提示存在问题的事实是您不需要在touchesMovedtouchesEndedupdate前加override作为前缀。

将来,我建议使用断点和print语句检查您希望执行的方法,实际上是在运行。这样做,您就会发现您的touchesMovedtouchesEndedupdate版本未被调用。

无论如何,在这里它已经纠正了它,现在它完美地运作了:

import SpriteKit
class GameScene: SKScene {
    var sprite: SKSpriteNode!
    var touchPoint: CGPoint = CGPoint()
    var touching: Bool = false
    override func didMoveToView(view: SKView) {
        self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
        sprite = SKSpriteNode(color: UIColor.redColor(), size: CGSize(width: 50, height: 50))
        sprite.physicsBody = SKPhysicsBody(rectangleOfSize: sprite.size)
        sprite.position = CGPoint(x: self.size.width/2.0, y: self.size.height/2.0)
        self.addChild(sprite)
    }

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        for touch: AnyObject in touches {

            let location = touch.locationInNode(self)
            if sprite.frame.contains(location) {
                touchPoint = location
                touching = true
            }
        }
    }

    override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
        for touch: AnyObject in touches {
            let location = touch.locationInNode(self)
            touchPoint = location
        }
    }

    override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
        touching = false
    }

    override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) {
        touching = false
    }

    override func update(currentTime: NSTimeInterval) {
        if touching {
            let dt:CGFloat = 1.0/60.0
            let distance = CGVector(dx: touchPoint.x-sprite.position.x, dy: touchPoint.y-sprite.position.y)
            let velocity = CGVector(dx: distance.dx/dt, dy: distance.dy/dt)
            sprite.physicsBody!.velocity = velocity
        }
    }
}

答案 1 :(得分:0)

ABakerSmith为Swift 4更新了解决方案:

[(clrDgSingleSelected)]="selectedUser"
[clDgRowSelection]="true"

答案 2 :(得分:0)

解决方案已更新,没有将触摸/拖动卡扣到精灵的中间。如果要拖动/投掷对象,则希望该投掷来自用户触摸该对象的位置,而不是捕捉到该对象的中间。这样会使它看起来更平滑

import SpriteKit
class GameScene: SKScene {
    var sprite: SKSpriteNode!
    var touchPoint: CGPoint = CGPoint()
    var touching: Bool = false
    var xDif = CGFloat()
    var yDif = CGFloat()

    override func didMove(to view: SKView) {
        self.physicsBody = SKPhysicsBody.init(edgeLoopFrom: self.frame)
        sprite = SKSpriteNode(color: .red, size: CGSize(width: 50, height: 50))
        sprite.physicsBody = SKPhysicsBody.init(rectangleOf: sprite.size)
        sprite.position = CGPoint(x: self.size.width/2.0, y: self.size.height/2.0)
        self.addChild(sprite)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first?.location(in: self) {
            xDif = sprite.position.x - touch.x
            yDif = sprite.position.y - touch.y

            if sprite.frame.contains(touch) {
                touchPoint = touch
                touching = true
            }
        }
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        for t in touches {
            let location = t.location(in: self)
            touchPoint = location
        }
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        touching = false
    }

    override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
        touching = false
    }

    override func update(_ currentTime: TimeInterval) {
        if touching {
            let dt:CGFloat = 1.0/60.0
            let distance = CGVector(dx: touchPoint.x-sprite.position.x + xDif, dy: touchPoint.y-sprite.position.y + yDif)
            let velocity = CGVector(dx: distance.dx/dt, dy: distance.dy/dt)
            sprite.physicsBody!.velocity = velocity
        }
    }
}