- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的游戏快完成了。游戏由一艘移动的船组成,我们需要避免移动石头。当船和石头相撞时,游戏结束。我想实现第二个 View Controller ,它显示游戏结束标签、游戏得分和重启按钮。GameOver.swift 是另一个具有游戏得分和重启选项的 View Controller 。
这是我的 ViewController.swift
import UIKit
import QuartzCore
class ViewController: UIViewController {
@IBOutlet weak var myLBL: UILabel!
@IBOutlet weak var myView: UIView!
@IBOutlet weak var collectedCoin: UILabel!
// weak var moveWater: MovingWater!
var views : [String : UIView]!
var boat:UIImageView!
var stone:UIImageView!
var food:UIImageView!
var boatWreck:UIImageView!
var boatLeftRight : UILongPressGestureRecognizer!
var coins = Int()
var pass = "HELLO"
var tapTimer:Timer!
var tapTimer2: 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.addSubview(boat)
boatLeftRight = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.leftRight(tap:)))
boatLeftRight.minimumPressDuration = 0.001
myView.addGestureRecognizer(boatLeftRight)
tapTimer2 = Timer.scheduledTimer(timeInterval: TimeInterval(0.05), target: self, selector: #selector(ViewController.change), userInfo: nil, repeats: true)
}
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: myView)
if touch.x > myView.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"))
stone.frame = CGRect(x: 0, y: 0, width: 60, height: 90)
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: myView)
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 movingFood() {
food = UIImageView(image: UIImage(named: "fishcoin2.png"))
var stone3 = leftS + arc4random() % rightS
food.bounds = CGRect(x:0, y:0, width:81.0, height:124.0)
food.contentMode = .center;
food.layer.position = CGPoint(x: Int(stone3), y: 40)
food.transform = CGAffineTransform(rotationAngle: 3.142)
self.view.insertSubview(food, aboveSubview: myView)
UIView.animate(withDuration: 5, delay: 0, options: UIViewAnimationOptions.curveLinear, animations: { () -> Void in
self.food.frame.origin.y = self.view.bounds.height + self.food.frame.height - 50
}) { (success:Bool) -> Void in
self.food.removeFromSuperview()
self.movingFood()
}
}
func change(tap2: Timer) {
if(boat.layer.presentation()?.frame.intersects((food.layer.presentation()?.frame)!))!
{
coins = coins + 1
collectedCoin.text = "\(coins)"
if coins > 100 {
let a = UIAlertController(title: "WON", message: "WANT AGAIN", preferredStyle: UIAlertControllerStyle.alert)
a.addAction(UIAlertAction(title: "A", style: UIAlertActionStyle.cancel, handler: nil))
a.addAction(UIAlertAction(title: "B", style: UIAlertActionStyle.default, handler: { (a:UIAlertAction!) -> Void in
self.startGame()
}))
self.present(a, animated: true, completion: nil)
}
}
else if(boat.layer.presentation()?.frame.intersects((stone.layer.presentation()?.frame)!))! {
//this is where boat and stone collide
//I want to implement the gameoverVC here
//prepare() is not code sensing in my Xcode
stopGame()
}
}
func stopGame() {
tapTimer2.invalidate()
boat.image = UIImage(named: "wreckboat.png")
self.stone.layer.removeAllAnimations()
self.food.layer.removeAllAnimations()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//moveWater.backgroundStart()
startGame()
movingStone()
movingFood()
coins = 10
collectedCoin.text = "\(coins)"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
最佳答案
ViewController 的 prepare(forSegue:)
不是您调用的函数。它是为您调用的,就在转到新的 ViewController
之前。为了显示您的辅助 ViewController
,您必须创建它的一个实例,然后自己触发 segue。然后,您将设置 prepare()
函数以允许您的主 viewController 将数据传递给辅助 View Controller 。
以下是完成这项工作所需的步骤:
GameOver
ViewController
的实例。ViewController
创建一个到它的手动 segue 并给它一个标识符。StopGame()
中调用 performSegue(withIdentifier:)
。ViewController
的 prepare(for segue:)
以将数据(例如分数)传递给 GameOver
View Controller
。关于ios - CGRectIntersectsRect 中的 prepareforsegue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43480902/
嗨,我正在尝试使用 iPhone 上的加速计制作一个简单的游戏。 所以我有一个名为enemyBlock 的 UIView 子类。我在屏幕上放了一些这样的东西。这些 block 一遍又一遍地使用 [UI
就像在马里奥游戏中,如果您跳到怪物的顶部,怪物就会被击倒。 我在两个对象(玩家和怪物)之间使用 CGRectIntersectsRect;但是,怪物会从任何方向被击倒。 如何在对象的特定点处将两个对象
我有代码生成一个随机 CGRect,检查它是否与先前数组中的任何其他矩形相交,如果不相交,则将新矩形添加到数组中。这工作得很好,但是我也想确保随机 CGRect 也不会与屏幕中央的图像重叠。我创建了一
您好,我正在使用 cocos2d 编写游戏并使用 ipad 5.0 模拟器。在编译时,我收到此链接错误,提示未找到体系结构 i386 的 CGRectIntersectsRect 符号。我想我缺少正确
是否有与 CGRectIntersectsRect 等价的东西,它更像是点相交的线...我正在制作游戏,RectIntersectsRect 可以实现我想要的功能,但它看起来很糟糕因为有时一个对象的角
我正在尝试使用 CGRectIntersectsRect 更新我的分数每次两个图像相互碰撞时,我都想增加我的分数。但是,我的分数有时不会显示周期性,它会增加一个单位,但是当两个 UIimage 以更高
我目前正在尝试测试两个 CGRect 之间的碰撞。通常在 Objective C 中我会使用 CGRectIntersectsRect 方法,但在 Coso2d Android 中唯一可用的方法似乎是
我正在用 swift 编写一个小游戏,其中一艘船必须绕过障碍物。但是,我在 CGRectInersectsRect 中不断收到“调用中参数 #2 缺少参数”错误。我已经检查了如何在目录中调用它,并且调
我有一个问题。 我希望两个物体发生碰撞。这是我当前的代码: if (CGRectIntersectsRect(_redBall.frame, _boxRed.frame)) { score =
这行代码在 iOS 8 之前是有效的,但是在更新到 iOS 8 之后这行代码就不再有效了。 if (CGRectIntersectsRect(icon1.frame, pig.frame) &
我正在使用两个 UIImageView,我已经为每个 UIImageView 添加了一个 subview (UIView)。我使用 CGRectIntersectsRect 来检测碰撞,但不起作用。所
map 上有注释(视觉标签)。当用户在 map 中缩放(放大或缩小)时,将调用名为“regionDidChangeAnimated”的方法。每次完成此操作时,我都会尝试使用快速枚举来查看当前哪些注释对
我有 8 个 UIImageView,它们必须随机放置。我为每个 imageView 生成一个随机的 x,y pos,然后我需要检查是否有任何 imageViews 相交。如果它们相交,它会再次计算随
我正在制作一款益智游戏,您必须绕过障碍物移动物体才能到达目标。但是,由于某些原因,当我使用 CGRectIntersectsRect 时对象重叠。我希望对象在彼此接触边缘时停止,而不是在它们相互重叠时
我的游戏快完成了。游戏由一艘移动的船组成,我们需要避免移动石头。当船和石头相撞时,游戏结束。我想实现第二个 View Controller ,它显示游戏结束标签、游戏得分和重启按钮。GameOver.
我的应用程序在 CGRectIntersectsRect 上崩溃,我不知道该怎么办。 这是我的代码: if(CGRectIntersectsRect(player.frame,enemy.frame)
这就是我正在尝试做的事情.. 我有字母表中所有字母的 UIImageViews。如果用户从字母表中向下平移一个字母来拼写一个单词,并且该字母被平移并放下,没有与任何其他字母相交,那么我假设他们没有拼写
我有一个应用程序布局,需要检测图像何时与另一个图像发生碰撞。 在这里,用户通过点击他们想要的位置在屏幕上创建多个“球”,即名为“imgView”的 UIImageView 的同一个实例: - (voi
我有 4 个元素:从屏幕顶部落下的绿球、黄球、橙球和红球 我还有一个元素,蓝球,它会跟随我的触摸。 所有这些都运行良好! :D 但是,我想检测绿球何时与蓝球相交 我的蓝色球被放置在 viewDidLo
这是我用来检测 2 个 UIImageViews 是否相互碰撞的代码。 if (CGRectIntersectsRect(Appy.frame, Bottom.frame)) { [self
我是一名优秀的程序员,十分优秀!