Swift 3 游戏按钮按下循环
Posted
技术标签:
【中文标题】Swift 3 游戏按钮按下循环【英文标题】:Swift 3 game button pressed loop 【发布时间】:2017-09-13 23:26:13 【问题描述】:如果我按下按钮,图像将随机出现,我有一组图像。并且有一个图像也从数组中调用,但它只是在我打开游戏时随机出现。我想为按下的按钮做一个条件语句。就像单击按钮后我有 5 个条件: 1- 如果按下并且出现的图像与 UIIMage 视图不同,则会添加向下分数 2-如果按钮没有按下 2 秒钟将出现向下。 3-如果按下按钮并且它与 UIimage 相同,则游戏结束。 4- 如果他因为在 2 秒内没有击中图像而计算出 4 张图像,他将输掉。
var array:[UIImage] = [UIImage(named: "1.png")!,
UIImage(named: "2.png")!,
UIImage(named: "3.png")!,
UIImage(named: "4.png")!,
UIImage(named: "5.png")!,
UIImage(named: "6.png")!,
UIImage(named: "7.png")!,
UIImage(named: "8.png")!,
UIImage(named: "9.png")!,
UIImage(named: "10.png")!]
var random = arc4random_uniform(10)
@IBAction func myButtonPressed(button: UIButton)
var randomNum: UInt32 = 10
randomNum = arc4random_uniform(UInt32(array.count))
myButton.setImage(UIImage(named: "bird\(randomNum).png"), for: UIControlState.normal)
// myButton.setImage(UIImage(named: "\(randomNum).png"), for: UI)
self.myImage.animationImages = array
let buttonWidth = myButton.frame.width
let buttonHeight = myButton.frame.height
// Find the width and height of the enclosing view
let viewWidth = myButton.superview!.bounds.width
let viewHeight = myButton.superview!.bounds.height
// Compute width and height of the area to contain the button's center
let xwidth = viewWidth - buttonWidth
let yheight = viewHeight - buttonHeight
// Generate a random x and y offset
let xoffset = CGFloat(arc4random_uniform(UInt32(xwidth)))
let yoffset = CGFloat(arc4random_uniform(UInt32(yheight)))
// Offset the button's center by the random offsets.
myButton.center.x = xoffset + buttonWidth / 2
myButton.center.y = yoffset + buttonHeight / 2
/* for i in array
if myButton != myImage
randomNum = arc4random_uniform(UInt32(array.count))
if else
*/
【问题讨论】:
【参考方案1】:也许你可以尝试这样的事情。至于您的第一个条件,您的 UIImage 视图变量是什么。它是故事板上显示的另一个 UIImage 视图吗?
import UIKit
class ViewController: UIViewController
var array: [UIImage] = [UIImage(named: "1.png")!,
UIImage(named: "2.png")!,
UIImage(named: "3.png")!,
UIImage(named: "4.png")!,
UIImage(named: "5.png")!]
@IBOutlet weak var myButton: UIButton!
var pressed: Bool = false;
var score: Int = 0;
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//2nd Condition: if button did not press for 2 seconds will appear down
if(pressed == false)
Timer.scheduledTimer(timeInterval: 0.2, target: self, selector: #selector(ViewController.doSomething), userInfo: nil, repeats: true)
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
@IBAction func isPressed(_ sender: UIButton)
pressed = true;
var randomNum: UInt32 = 10
randomNum = arc4random_uniform(UInt32(array.count));
myButton.setImage(UIImage(named: "\(randomNum).png"), for: .normal)
// 1st Condition: if pressed and the image appeared is not same as the UIIMage view down score will be added
if( UIImage(named: "\(randomNum).png") != myButton.currentImage)
//Add View Down Score
score += 1;
// 3rd Condition: if button pressed and it is same as the UIimage so game over
else
// Game Over
func doSomething()
// Not sure what you mean by will appear down
print("Action")
我不太确定你的第四个条件是什么。请澄清
【讨论】:
嗨,是的,我有 UIImage 视图,这是图像向下 我有我的按钮,当我按下它时第二个图像会随机出现,并且发生了 4 个条件。 你有 git,也许我可以关闭你的存储库并尝试处理你目前拥有的东西 我试过你的更正;图像 UIImage down 消失了。它应该在那里并且每次我打开游戏时都会随机出现。所以相应地 myButton 图像具有 if 条件。我的意思是图像会下降。他丢失了,所以如果他没有在 2 秒内按下按钮,图像会下降并保持不变,如果他收集了 4 张图像,他会丢失,你明白我的意思吗,这很复杂。游戏结束的功能是什么? 此操作有什么帮助吗?以上是关于Swift 3 游戏按钮按下循环的主要内容,如果未能解决你的问题,请参考以下文章
在按钮按下 swift 3 时获取 UITableView 单元格的行