在Swift中为多个对象添加重力行为

时间:2014-09-12 18:08:13

标签: ios swift uicollisionbehavior uigravitybehavior

我正在构建一个游戏,其中有许多方块在屏幕周围浮动,彼此反弹,墙壁等等,直到它们被摧毁。目前,我有一个主人UIGravityBehavior,我在创建时添加了每个方块(一个名为SquareView的UIView类),然后在它被销毁时将其删除。但我我应该在SquareView对象中这样做。这是相关的代码:

class ViewController: UIViewController, UICollisionBehaviorDelegate  {
    var gravity: UIGravityBehavior!

...

// next sequence adds a square
let newSquare = SquareView()
newSquare.createSquare(touch.locationInView(view).x, y: touch.locationInView(view).y)
view.addSubview(newSquare)
gravity.addItem(newSquare)

...

// this removes a square
gravity.removeItem(currentSquare)

所以我相信我应该将ViewController之外的代码移到我对SquareView类的定义中:

func createSquare(x: CGFloat, y: CGFloat){
    self.backgroundColor = color[touchCount]
    self.frame = CGRect(x: x, y: y, width: size[touchCount], height: size[touchCount])
}

顺便说一下,我对UICollisionBehavior做了同样的事情,并假设它也不是最佳的。

任何帮助都会很棒!

0 个答案:

没有答案
相关问题