首页 > 解决方案 > 如何在 spritekit 中让球穿过篮筐?

问题描述

我有一个篮球框 png,如果用户点击篮框上方的正确位置,我希望篮球穿过它,但是每当我点击屏幕让球落下时,它总是从篮框反​​弹,即使它就在上方它应该通过它。我怎样才能让篮球直接从篮筐中掉下来?这是我的代码。谢谢。

import SpriteKit
import GameplayKit

class GameScene: SKScene {

    override func didMove(to view: SKView) {
        
//This is the basketball rim png that I put in my scene
        let rim = SKSpriteNode(imageNamed: "resized Basketball hoop.png")
        rim.position = CGPoint(x: 512, y: 384)
        rim.physicsBody = SKPhysicsBody(texture: rim.texture!, size: rim.texture!.size())
        rim.physicsBody?.isDynamic = false
        addChild(rim)
        
        
        }
    
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
//This is when I touch the screen and a basketball falls from where I touched
        if let touch = touches.first {
        let location = touch.location(in: self)
        let basketball = SKSpriteNode(imageNamed: "basketball png.png")
        basketball.physicsBody = SKPhysicsBody(circleOfRadius: basketball.size.width / 16)
        basketball.size.width = basketball.size.width / 8
        basketball.size.height = basketball.size.height / 8
        basketball.physicsBody?.restitution = 0.4
        basketball.position = location
        physicsBody = SKPhysicsBody(edgeLoopFrom: frame)
        addChild(basketball)
            
    }
}
}

标签: swiftsprite-kit

解决方案


推荐阅读