【问题标题】:Sprite Kit stop ImpulseSprite Kit 停止冲动
【发布时间】:2015-11-09 03:17:48
【问题描述】:

我想在每次点击屏幕时增加一个CGFloat。 浮点数有一个设定的最大值和最小值。 我试图通过这个建议:StackOverflow

但是,这只会在进行触摸或达到最大值后增加 CGFloat。我想在触摸时增加 CGFloat,这意味着你触摸的时间越长,Jump/CGFloat 就越高。

问题可能在于冲动,应用后无法更改。也就是说,当“玩家”获得 20 的脉冲后,触摸屏幕的时间更长,浮点数可能会增加,但脉冲不会增加。

如果您查看我当前的代码,则在触摸屏幕时将脉冲设置为最大,但如果释放,则应删除该操作。然而,它不起作用,冲动并没有停止。

我知道你可以在按压后将身体的速度设置为一个值,如果按压结束,速度会回到 0,所以它会停止“跳跃”,但这看起来不太顺畅就像冲动一样。

有人有解决办法吗?

struct Constants {

static let minimumJumpForce:CGFloat = 20.0
static let maximumJumpForce:CGFloat = 60.0
}

class GameScene: SKScene, SKPhysicsContactDelegate {

var force: CGFloat = 20.0

func longPressed(longPress: UIGestureRecognizer) {

    if (longPress.state == UIGestureRecognizerState.Began) {

        println("Began")

        self.pressed = true

        let HigherJump = SKAction.runBlock({Player.physicsBody?.applyImpulse(CGVectorMake(0, Constants.maximumJumpForce))})

        self.runAction(HigherJump , withKey:"HighJump")

    }else if (longPress.state == UIGestureRecognizerState.Ended) {

        println("Ended")

        self.pressed = false

        self.removeActionForKey("HighJump")

    }

}

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    /* Called when a touch begins */

    for touch in (touches as! Set<UITouch>) {
        let location = touch.locationInNode(self)

    }
}

override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
    for touch in (touches as! Set<UITouch>) {
        let location = touch.locationInNode(self)

}
}

override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */

}

【问题讨论】:

    标签: ios swift sprite-kit physics


    【解决方案1】:

    1.基于 SpriteKit 从 Xcode 模板创建“游戏”
    2.复制粘贴列出的代码到GameScene类

    import SpriteKit
    
    class GameScene: SKScene, SKPhysicsContactDelegate {
    
        var location = CGPoint()
        var floorSize = CGSize()
        var floorColor = UIColor()
        var player = SKSpriteNode()
    
        override func didMoveToView(view: SKView) {
    
            view.showsFPS = true;
            view.showsNodeCount = true;
            view.showsDrawCount = true;
    
            self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
            self.physicsBody?.categoryBitMask = 1
            self.physicsBody?.contactTestBitMask = 1
            self.physicsWorld.gravity = CGVectorMake(0, 0)
            self.physicsWorld.contactDelegate = self;
    
            location = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame))
    
            player = SKSpriteNode(imageNamed:"Spaceship")
            player.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: 320, height: 320))
            player.physicsBody?.categoryBitMask = 1
            player.physicsBody?.collisionBitMask = 1
            player.physicsBody?.contactTestBitMask = 1
            player.physicsBody?.linearDamping = 0;
            player.xScale = 1
            player.yScale = 1
            player.position = location
            self.addChild(player)
    
        }
    
        override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
            self.physicsWorld.gravity = CGVectorMake(0, 0)
            let direction = Float(1.5708)//Float(player.zRotation) + Float(M_PI_2)
            player.physicsBody?.applyForce(CGVector(dx: 150000*CGFloat(cosf(direction)), dy: 150000*CGFloat(sinf(direction))))
        }
    
        override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
            self.physicsWorld.gravity = CGVectorMake(0, -7.9)
        }
    
    }
    

    3.运行应用程序

    这应该为您提供“跳跃”游戏的起点:)

    【讨论】:

    • 我在没有 longPress 的情况下尝试了它,并在开始触摸时插入了 applyImpulse。它仍然给我同样的结果。
    • @Albert 你试过我的解决方案了吗? Here is video of my code
    • 是的,我使用了完全相同的代码。问题可能在于冲动,应用后您无法更改它。这意味着,在“播放器”获得 20 的脉冲之后,并且触摸屏幕的时间更长,Float 可能会增加,但脉冲不会增加。这是我想要的结果:Youtube
    • 嘿,很抱歉回答迟了。我用您的代码完全尝试过,它仍然只在下一次点击后将增加的冲动施加到角色上。此外,用最小和最大浮点数像这样处理它也很困难。
    • 所以,查看我上面的最新更新。我用 UIDynamics 创建了示例。希望这会对您有所帮助,现在肯定会起作用:)
    【解决方案2】:

    尝试改变这个:

    if(self.pressed){
    
        let HigherJump = SKAction.runBlock({if(self.force < Constants.maximumJumpForce){
            self.force += 2.0
        }else{
            self.force = Constants.maximumJumpForce
            }})
        self.runAction(HigherJump)
    }
    

    到这里:

    if(self.pressed){
        if(self.force < Constants.maximumJumpForce) {
             self.force += 2.0
        }
        else {
             self.force = Constants.maximumJumpForce
        }
    }
    

    这里不需要使用 runBlock SKAction。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多