ios - 触摸并按住 SpriteKit SKSpriteNode 速度

标签 ios swift sprite-kit skspritenode

我的 SKSpriteNode 由于 self.physicsWorld.gravity = CGVectorMake(0.0, -4.0); 而被迫掉到地上。当用户点击显示并按住它时,我希望 SKSpriteNode 飞起来,当用户停止触摸后,它再次落到地上。

我试过改变速度,但它只会产生很小的反弹,比如 applyImpulse 方法...:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        /* Called when a touch begins */

        for touch: AnyObject in touches {

            let location = touch.locationInNode(self)

            flyingObject.physicsBody.velocity = CGVectorMake(0, 100)
        }        
    }

编辑:

这是我的场景初始化代码

override func didMoveToView(view: SKView) {
        /* Setup your scene here */

        // Physics
        self.physicsWorld.gravity = CGVectorMake(0.0, -4.0);

        // flyingObject
        var flyingObjectTexture = SKTexture(imageNamed:"flyingObject")
        flyingObject.filteringMode = SKTextureFilteringMode.Nearest

        flyingObject = SKSpriteNode(texture: flyingObjectTexture)
        flyingObject.setScale(1)
        flyingObject.position = CGPoint(x: self.frame.size.width * 0.35, y: self.frame.size.height * 0.6)

        flyingObject.physicsBody = SKPhysicsBody(circleOfRadius:flyingObject.size.height/2.0)
        flyingObject.physicsBody.dynamic = true
        flyingObject.physicsBody.allowsRotation = false

        self.addChild(flyingObject)

        // Ground
        var groundTexture = SKTexture(imageNamed:"Ground")

        var sprite = SKSpriteNode(texture:groundTexture)
        sprite.setScale(0.5)
        sprite.position = CGPointMake(self.size.width/2 - 100, sprite.size.height)

        self.addChild(sprite)

        var ground = SKNode()

        ground.position = CGPointMake(0, groundTexture.size().height / 2)
        ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.width, groundTexture.size().height * 0.5))

        ground.physicsBody.dynamic = false
        self.addChild(ground)

    }

最佳答案

感谢 Code Monkey,我能够想出解决方案,这比我想象的要容易:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        /* Called when a touch begins */
        isTouching = true
    }

    override func touchesEnded(touches: NSSet!, withEvent event: UIEvent!) {
        /* Called when a touch begins */
        isTouching = false
    }

    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
        if isTouching {
            flyingObject.physicsBody.applyImpulse(CGVectorMake(0, 5))
        }
    }

关于ios - 触摸并按住 SpriteKit SKSpriteNode 速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24580510/

相关文章:

ios - 导航 Controller 中不允许使用 UINavigationControllers

javascript - 如何使用 firebase 云函数增加徽章计数

ios - UITableView 单元格在应用程序重新启动后立即更新

swift - 无法使用 '_' 类型的参数列表调用 '_' - 我应该使用这两个选项中的哪一个?

Swift SpriteKit,不能多次发射子弹

ios - SpriteKit 液体(水)

ios - SKSpriteNode 上不连续纹理的物理体路径

ios - 在 subview Controller 之间切换

ios - 我的第 3 方 iOS 应用程序会自动向 Apple Watch 发送通知吗?

ios - APNS 负载大小问题