【发布时间】: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
} */
}
【问题讨论】: