如何让玩家在Swift中只跳一次?

时间:2016-04-26 10:49:49

标签: swift

我想知道如何让一名球员只跳一次直到他击中地面,就像在大多数比赛中一样。我尝试了这个: swift - Jump only when landed ,但它并没有真正对我有用,因为无论出于何种原因,玩家只是在我发送垃圾邮件时才会跳过屏幕并且他每次点击都只跳了30次。这是它的样子:

下面是:GameScene:SkScene {

v

在:didMoveToView(view:SKView)

var ableToJump = true
var Player = SKSpriteNode(imageNamed: "Spieler_rechts")

在:override func update(currentTime:NSTimeInterval)

Player.position = CGPoint(x: self.frame.size.width*0.1, y: Ground.frame.size.height + Player.frame.size.height)
Player.zPosition = 3
Player.physicsBody = SKPhysicsBody(rectangleOfSize: Player.size)
Player.physicsBody?.allowsRotation = false
Player.physicsBody?.affectedByGravity = true
Player.physicsBody?.restitution = 0
self.addChild(Player)

在:override func touchesBegan(touches:Set,withEvent event:UIEvent?)

if Player.physicsBody?.velocity.dy == 0 {
    ableToJump = true
}
else {
    ableToJump = false
}

1 个答案:

答案 0 :(得分:0)

以下是我的表现:

编辑:

    class MainScene: SKScene, SKPhysicsContactDelegate {

        let colorChanger = ColorChanger()
        let ground = SKSpriteNode(imageNamed: "Ground")
        let player = SKSpriteNode(imageNamed: "Player")
        let block1 = SKSpriteNode(imageNamed: "Block2")
        let block2 = SKSpriteNode(imageNamed: "Block2")
        let scoreLabel = SKLabelNode(fontNamed: "Chalkboard SE")
        let highScoreLabel = SKLabelNode(fontNamed: "Chalkboard SE")
        let life = SKSpriteNode(imageNamed: "Player")
        let life2 = SKSpriteNode(imageNamed: "Player")
        let life3 = SKSpriteNode(imageNamed: "Player")
        var firstTime = Int()

        var blockMaxX = CGFloat(0)
        var origBlockPositionX = CGFloat(0)
        var score = Int()
        var highScore = Int()
        var lives = Int()

        var groundPositionX = CGFloat(0)
        var maxBarX = CGFloat(0)
        var groundSpeed = 5
        var playerBaseLine = CGFloat(0)
        var more = CGFloat(0)

        var onGround = true
        var velocityY = CGFloat(0)
        let gravity = CGFloat(1.3)

        enum Collision:UInt32{
            case Player = 1
            case Block = 2
        }



override func didMoveToView(view: SKView) {
self.physicsWorld.contactDelegate = self
        let color1 = colorChanger.randomColor()
        runAction(SKAction.colorizeWithColor(color1, colorBlendFactor: 1, duration: 0.2))
        self.ground.anchorPoint = CGPointMake(0, 1)
        self.ground.position = CGPointMake(CGRectGetMinX(self.frame),CGRectGetMidY(self.frame)-23)
        self.ground.size = CGSize(width: 1100, height: 4000)

        self.groundPositionX = self.ground.position.x
        self.maxBarX = self.ground.size.width - self.frame.size.width - 100
        self.maxBarX *= -1

        self.player.size = CGSize(width: 50, height: 50)
        self.playerBaseLine = self.frame.height / 2
        self.player.position = CGPointMake(CGRectGetMinX(self.frame) + self.player.size.width / 1.5 , self.playerBaseLine)
        self.player.physicsBody = SKPhysicsBody(circleOfRadius: CGFloat(self.player.size.width / 2))
        self.player.physicsBody!.affectedByGravity = false
        self.player.physicsBody!.categoryBitMask = Collision.Player.rawValue
        self.player.physicsBody!.contactTestBitMask = Collision.Block.rawValue
        self.player.physicsBody!.collisionBitMask = Collision.Block.rawValue

override func update(currentTime: NSTimeInterval) {

            self.velocityY  += self.gravity
            self.player.position.y -= velocityY

            if self.player.position.y < self.playerBaseLine {
                self.player.position.y = self.playerBaseLine
                velocityY = 0.0
                onGround = true
            }
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
            if self.onGround{
                self.velocityY = -18.0
                self.onGround = false
            }
    }

你需要玩引力;)