我正在建立一个突破游戏(#cs193p)并且我已经开始了一般的工作设置:砖块,球和桨都按照它们的要求绘制。除了碰撞边界之外,碰撞类似于应有的工作似乎是错误的。它们通常比我构建它们的路径大,但并非总是如此。
我已将球的弹性设置为零,因此它会停留在桨上,因此差异很明显。此屏幕截图显示球停在桨的不正确碰撞边界上。
砖块和底部的黑色区域反应略有不同。对于砖块,当它在视觉上到达下一排时,球似乎与底部的砖块碰撞。我的砖块正在消失,而且#34;正确地#34;所以碰撞得到了砖块消失的双重证实。底部的黑色区域(用于平移移动球拍的空间)具有类似的问题:球在弹跳之前稍微下降到该区域。
这是一堆代码,因为我不知道问题可能在哪里。
来自我的BreakoutBehavior
课程(因为它似乎不是问题的一部分,所以我忽略了重力部分):
let collider: UICollisionBehavior = {
let collider = UICollisionBehavior()
collider.translatesReferenceBoundsIntoBoundary = true
return collider
}()
private let ballBehavior: UIDynamicItemBehavior = {
let behavior = UIDynamicItemBehavior()
behavior.allowsRotation = true
behavior.elasticity = 1.25
return behavior
}()
internal func addColliderBoundary(path: UIBezierPath, named name: String) {
collider.removeBoundary(withIdentifier: name as NSCopying)
collider.addBoundary(withIdentifier: name as NSCopying, for: path)
}
override init() {
super.init()
addChildBehavior(gravity)
addChildBehavior(collider)
addChildBehavior(ballBehavior)
}
internal func addItem (_ item: UIDynamicItem) {
gravity.addItem(item)
collider.addItem(item)
ballBehavior.addItem(item)
}
这是我BreakoutView
课程中的一些代码:
我给你的划桨只是边界问题的一个例子,而不是给砖块和平底锅区域更高效。因此,我所提及的一些变量当然不会出现在我的摘录中。请注意,我遗漏了一些事情,例如向动画师添加行为,但知道一切确实有效,我只是遇到了这些边界问题。
private var paddleSize: CGSize {
return CGSize(width: brickSize.width * 3, height: brickSize.height)
}
private var paddleOrigin: CGPoint {
let x = frame.midX - paddleSize.width / 2
let y = panOrigin.y - paddleSize.height
return CGPoint(x: x, y: y)
}
private func addBallAndPaddle () {
let paddleFrame = CGRect(origin: paddleOrigin, size: paddleSize)
let paddleView = UIView(frame: paddleFrame)
paddleView.backgroundColor = UIColor.white
paddleView.layer.borderWidth = 0.25
addSubview(paddleView)
paddle = paddleView
behavior.addColliderBoundary(path: UIBezierPath(rect: paddleFrame), named: Boundaries.paddle)
let ballFrame = CGRect(origin: ballOrigin, size: ballSize)
let ballView = UIView(frame: ballFrame)
ballView.backgroundColor = UIColor.white
ballView.layer.borderWidth = 0.25
addSubview(ballView)
behavior.addItem(ballView)
ball = ballView
}
private lazy var animator: UIDynamicAnimator = {
let animator = UIDynamicAnimator(referenceView: self.superview!)
animator.delegate = self
return animator
}()
private lazy var behavior: BreakoutBehavior = {
let behavior = BreakoutBehavior()
behavior.collider.collisionDelegate = self
return behavior
}()
这是来自BreakoutViewController的代码。在configureGameView()
viewDidLayoutSubviews
private func configureGameView () {
gameView.frame = topView.frame // topView is the view in IB
gameView.backgroundColor = UIColor.lightGray
gameView.addViews() // adds bricks, ball, paddle, pan area
firstTap.numberOfTapsRequired = 1
gameView.addGestureRecognizer(firstTap) // DOESN'T WORK
if let panView = gameView.panner {
panView.addGestureRecognizer(UIPanGestureRecognizer(target: gameView, action: #selector(gameView.movePaddle(_:))))
panView.addGestureRecognizer(firstTap) // DOESN'T WORK
}
}
谢谢!