swift - 如何在 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)
}
}
}
解决方案
推荐阅读
- node.js - MongoClient 发生错误时不会触发回调
- tomcat - Tomcat Rewrite Valve - 如何在客户端浏览器上保留漂亮的 URL
- ruby-on-rails - 不能将 x-editable 包含到 Rails 6
- android - Cordova CLI“cordova build android”错误“似乎不是 xcode 项目,没有 xcode 项目文件...”?
- sql - 我有一个冲突矩阵,以及具有不同访问权限的用户列表,我想检查该用户是否为冲突访问权限
- ios - 如何使用 VoIP 取消通话
- php - 1 个域有 2 个不同的应用程序(1 个 laravel,1 个普通 php 应用程序)
- mysql - 如何使用 phpMyAdmin 中的 SQL 查询设置具有许多特殊字符的列值?
- bash - 如何将不同均匀间隔的文本块从一个文件复制到包含每个块的多个文件中
- c# - 在 ASP.NET MVC Core 中重定向后防止页面缓存在浏览器上