- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在制作一个 iOS 应用程序,遇到了一些物理问题。正如您通过下面的 .GIF 可以看出的那样,当我旋转六边形并且球以一定角度撞击矩形时,它失去了一些速度并且不会反弹得那么高。这是因为共享的原因here (主要是因为我限制了球的水平位置,它只在击球角度时使用垂直速度,因此失去了速度)。
我一辈子都想不出解决这个问题的方法。有人有什么想法吗??
Ball节点代码:
func createBallNode(ballColor: String) -> SKSpriteNode {
let ball = SKSpriteNode(imageNamed: ballColor)
ball.position = CGPoint(x: CGRectGetMidX(frame), y: CGRectGetMidY(frame)+30)
ball.zPosition = 1
ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.width/2)
ball.physicsBody?.affectedByGravity = true
ball.physicsBody?.restitution = 1
ball.physicsBody?.linearDamping = 0
ball.physicsBody?.friction = 0
ball.physicsBody?.categoryBitMask = ColliderType.Ball.rawValue
ball.physicsBody?.contactTestBitMask = ColliderType.Rect.rawValue
ball.physicsBody?.collisionBitMask = ColliderType.Rect.rawValue
let centerX = ball.position.x
let range = SKRange(lowerLimit: centerX, upperLimit: centerX)
let constraint = SKConstraint.positionX(range)
ball.constraints = [constraint]
return ball
}
最佳答案
是的,这个问题可能是球没有完全“对齐”时击中六边形造成的。在这种情况下,球失去垂直速度以支持水平轴。
既然你想要一个“离散逻辑”,我相信在这种情况下你应该避免物理(至少对于弹跳部分)。重复垂直移动球的 SKAction
会容易得多。
我准备了一个简单的例子
class GameScene: SKScene {
override func didMove(to view: SKView) {
super.didMove(to: view)
let ball = createBallNode()
self.addChild(ball)
}
func createBallNode() -> SKSpriteNode {
let ball = SKSpriteNode(imageNamed: "ball")
ball.position = CGPoint(x: frame.midX, y: frame.minY + ball.frame.height / 2)
let goUp = SKAction.move(by: CGVector(dx: 0, dy: 600), duration: 1)
goUp.timingMode = .easeOut
let goDown = SKAction.move(by: CGVector(dx: 0, dy: -600), duration: 1)
goDown.timingMode = .easeIn
let goUpAndDown = SKAction.sequence([goUp, goDown])
let forever = SKAction.repeatForever(goUpAndDown)
ball.run(forever)
return ball
}
}
如果您需要在每次球接触六边形底部时执行检查,您可以使用此代码
class GameScene: SKScene {
override func didMove(to view: SKView) {
super.didMove(to: view)
let ball = createBallNode()
self.addChild(ball)
}
func createBallNode() -> SKSpriteNode {
let ball = SKSpriteNode(imageNamed: "ball")
ball.position = CGPoint(x: frame.midX, y: frame.minY + ball.frame.height / 2)
let goUp = SKAction.move(by: CGVector(dx: 0, dy: 600), duration: 1)
goUp.timingMode = .easeOut
let goDown = SKAction.move(by: CGVector(dx: 0, dy: -600), duration: 1)
goDown.timingMode = .easeIn
let check = SKAction.customAction(withDuration: 0) { (node, elapsedTime) in
self.ballTouchesBase()
}
let goUpAndDown = SKAction.sequence([goUp, goDown, check])
let forever = SKAction.repeatForever(goUpAndDown)
ball.run(forever)
return ball
}
private func ballTouchesBase() {
print("The ball touched the base")
}
}
正如您现在看到的,每次球处于较低的 y 坐标时,方法 ballTouchesBase
都会被调用。这是为六边形的颜色添加检查的正确位置。
关于ios - SKSpriteNode 在碰撞时失去速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39211457/
我在我的项目(Ubuntu 18.04.2 LTS)中使用了 celery 4.4.0 版本。当我提出异常('功能中的功能太少无法分类')时, celery 项目失去了 worker ,我得到了这样的
我在这样的更新面板中有一个 GridView :
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 关闭 9 年前。 这个问题似乎不是关于 a specific programming problem,
当我尝试使用 jquery 的 html() 将 HTML 附加到另一个元素时。我失去了 ng-click 的能力。这是我的代码的粗略描述。例如我有一个名为 testController 的 Cont
我正在将 python 连接到 mySqL 数据库,并从 android 发送两个 Double 值以存储在 Mysql 中,然后我收到此错误 enter code here Exception i
无法理解为什么这个简单的事情没有反应性。看起来我错过了一些 Vue 的基础。 {{connection_status}} export default {
我不明白出了什么问题,在我的设备上应用程序运行良好,但有时在日志中(应用程序用户、 session 、崩溃......通过某些服务跟踪)我看到应用程序崩溃(NullPointerException),
我正在处理一个我有点困惑的问题。问题是假设您是二战期间英国空军的一名将军。您还剩下 100 架飞机来保卫英国。在您执行的每个任务中,每架飞机都有 50% 的几率被德国高射炮击落,因此每次执行任务您都会
我相信我的问题与以下问题非常相似 Spring security 'remember me' cookie not avaiable in first request . 基本上,我有一组用于公司内部
似乎 Bootstrap 下拉按钮在单击后仍保持焦点。 我的用例与此不兼容, 用户应该在单击按钮后按“SPACE”键,为了触发与按钮无关的东西。 问题是按钮保持焦点状态,因此按空格键 激活按钮。 我希
我有一个 MKMapView,其中有很多从解析器 xml 定义的注释引脚;那是我的代码: -(IBAction)LoadAnnotation:(id)sender { RXML element
我有一个 HTML 表格,用户可以在其中输入他的待机开始时间和待机结束时间,该时间计算总时数,并基于此计算获得的补偿时数。这一切都有效。 现在,它所做的是,每当用户输入开始和结束时间时,用户必须将焦点
Windows 系统更新后,Eclipse 不再了解 Android 的任何信息...我如何恢复对 Android 的支持?我尝试重新安装 SDK,但没有成功... 太奇怪了。我注意到项目模板丢失了,
我正在发送 SOAP 请求来更新某些实体。当我创建(而不是更新)几乎相同的东西时,一切正常。我跟踪了客户端和传输的日志记录,最后它给了我一个线索。它看起来像这样: DEBUG:suds.client:
这个问题在这里已经有了答案: TypeError: Cannot read property 'setState' of undefined (3 个答案) How to access the co
我使用 PostgreSQL 和 repmgrd 创建了一个 Docker 镜像,所有这些镜像都是使用 supervisor 启动的。 我现在的问题是,当它启动时,由 supervisor 生成的 r
新的 iOS 开发者为 iPhone 编写第一个委托(delegate)应用程序。 模态调用的父 View Controller 有六个 subview Controller ,它们使用父 View
我目前正在 MVC4 中创建一些用户配置文件编辑表单,并且为了测试,我将 UserId 属性呈现为表单上的只读文本框,如下所示: @Html.LabelFor(model => model.
我是一名优秀的程序员,十分优秀!