我正在使用swift構建一款遊戲遊戲。目標是避免進入的寶石,並隨着關卡的增加獲得儘可能多的積分。石頭撞向船隻的方向相反。但我無法察覺船與石之間的碰撞,石頭穿過船。船可以向左或向右移動。我正在用swift構建一個遊戲遊戲。無法檢測到衝突
我用rect1.interects(rect2)作爲交集。
謝謝。
這裏是ViewController.swift
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var moveWater: MovingWater!
var boat:UIImageView!
var stone:UIImageView!
var boatLeftRight : UILongPressGestureRecognizer!
var tapTimer:Timer!
var leftM:UInt32 = 55
var rightM:UInt32 = 250
var leftS:UInt32 = 35
var rightS:UInt32 = 220
func startGame() {
boat = UIImageView(image: UIImage(named: "boat"))
boat.frame = CGRect(x: 0, y: 0, width: 60, height: 90)
boat.frame.origin.y = self.view.bounds.height - boat.frame.size.height - 10
boat.center.x = self.view.bounds.midX
self.view.insertSubview(boat, aboveSubview: moveWater)
boatLeftRight = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.leftRight(tap:)))
boatLeftRight.minimumPressDuration = 0.001
moveWater.addGestureRecognizer(boatLeftRight)
}
func leftRight(tap:UILongPressGestureRecognizer) {
if tap.state == UIGestureRecognizerState.ended {
if (tapTimer != nil) {
self.tapTimer.invalidate()
}
} else if tap.state == UIGestureRecognizerState.began {
let touch = tap.location(in: moveWater)
if touch.x > moveWater.frame.midX {
tapTimer = Timer.scheduledTimer(timeInterval: TimeInterval(0.005), target: self, selector: #selector(ViewController.moveBoat(time:)), userInfo: "right", repeats: true)
} else {
tapTimer = Timer.scheduledTimer(timeInterval: TimeInterval(0.005), target: self, selector: #selector(ViewController.moveBoat(time:)), userInfo: "left", repeats: true)
}
}
}
func moveBoat(time:Timer) {
if let d = time.userInfo as? String! {
var bot2 = boat.frame
if d == "right" {
if bot2.origin.x < CGFloat(rightM) {
bot2.origin.x += 2
}
} else {
if bot2.origin.x > CGFloat(leftM) {
bot2.origin.x -= 2
}
}
boat.frame = bot2
}
}
func movingStone() {
stone = UIImageView(image: UIImage(named: "stones.png"))
var stone2 = leftS + arc4random() % rightS
stone.bounds = CGRect(x:10, y:10, width:81.0, height:124.0)
stone.contentMode = .center;
stone.layer.position = CGPoint(x: Int(stone2), y: 10)
stone.transform = CGAffineTransform(rotationAngle: 3.142)
self.view.insertSubview(stone, aboveSubview: moveWater)
UIView.animate(withDuration: 5, delay: 0, options: UIViewAnimationOptions.curveLinear, animations: {() -> Void in
self.stone.frame.origin.y = self.view.bounds.height + self.stone.frame.height + 10
}) { (success:Bool) -> Void in
self.stone.removeFromSuperview()
self.movingStone()
}
}
func update() {
if(boat.bounds.intersects(stone.bounds)) {
boat.image = //set new image
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
moveWater.backgroundStart()
startGame()
movingStone()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
有許多問題,我可以在你的方法見: - 缺少遊戲主循環,每個遊戲的實體將關於獲得至少更新到位置 - 混合幀座標 - 碰撞將不得不從頭開始做的, uiview.bounds不一定與用戶體驗視角衝突盒中的優化重疊 我推薦的是: - 查找一些教程如何設計遊戲 - 切換到SpriteKit,它會給你「物理」免費 –
爲什麼我不能使用碰撞檢測。我不知道爲什麼我無法檢測到碰撞! –
使用框架,您只能輕鬆檢測到矩形碰撞,如果您將碰撞矩形設置得很小(比我認爲的小),但無法檢測到以此方式相交的自定義形狀。 –