Touch Sprite, make it jump up then fall down again(repeat as many times as spritenode is tapped.)

北战南征 提交于 2019-12-08 03:15:04

问题


I created a project where I have a ball and when the view loads, it falls down, which is good. I'm trying to get the ball to jump back up and fall down again when the Spritenode is tapped.

--This Question was edited-- Originally, I was able to get it to work when sprite.userInteractionEnabled = false. I had to turn this statement true in order to get the score to change.

Now I can't get the balls to fall and be tapped to jump. When I turn ball.physicsBody?.dynamic = true, the balls will fall due to gravity. How do I tap the sprite itself and make it jump.

GameScene.swift (For those who want to try the code for themselves.)

import SpriteKit

class GameScene: SKScene {
var ball: Ball!


private var score = 0 {
    didSet { scoreLabel.text = "\(score)" }
}


override func didMoveToView(view: SKView) {
    let ball = Ball()


    scoreLabel = SKLabelNode(fontNamed:"Geared-Slab")
    scoreLabel.fontColor = UIColor.blackColor()
    scoreLabel.position = CGPoint( x: self.frame.midX, y: 3 * self.frame.size.height / 4 )
    scoreLabel.fontSize = 100.0
    scoreLabel.zPosition = 100
    scoreLabel.text = String(score)
    self.addChild(scoreLabel)





    ball.position = CGPoint(x:self.size.width / 2.0, y: 440)

    addChild(ball)



    ball.physicsBody = SKPhysicsBody(circleOfRadius: 120)
    ball.physicsBody?.dynamic = true
    ball.physicsBody?.allowsRotation = false

    ball.physicsBody?.restitution = 3
    ball.physicsBody?.friction = 0
    ball.physicsBody?.angularDamping = 0
    ball.physicsBody?.linearDamping = 0

    ball.physicsBody?.usesPreciseCollisionDetection = true
}


 class Ball: SKSpriteNode {



    init() {
        let texture = SKTexture(imageNamed: "Ball")
        super.init(texture: texture, color: .clearColor(), size: texture.size())
        userInteractionEnabled = true

    }


    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let scene = self.scene as! GameScene
        scene.score += 1



    }

Before, it was a SKNode being tapped using CGVectorMake(impulse, velocity) now, it's a SKSpriteNode, and I tried using SKAction, but it either does not work, or I'm putting it in the wrong place(touches begin).


回答1:


I tested your code and it seems that using firstBall.userInteractionEnabled = true is the cause. Without it, it should work. I did some research (here for example), but can't figure out what's the reason of this behavior with userInteractionEnabled. Or for what reason do you use userInteractionEnabled?

Update due to update of question

First ball.physicsBody?.restitution = 3 defines the bounciness of the physics body. The default value is 0.2 and the property must be between 0.0 ans 1.0. So if you set it to 3.0 it will cause some unexpected effects. I just deleted it to use the default value of 0.2.

Second, to make the ball jump after tap and increase the score I put

physicsBody?.velocity = CGVectorMake(0, 600)
physicsBody?.applyImpulse(CGVectorMake(0, 1100))

in the touchesBegan method of the Ball class

Result



来源:https://stackoverflow.com/questions/38790908/touch-sprite-make-it-jump-up-then-fall-down-againrepeat-as-many-times-as-sprit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!