我知道这是愚蠢的问题。 我正在开发一个益智游戏和代码是:如何保存Imageview的原始位置并将其与同一Imageview的新位置进行比较?
import UIKit
class ViewController: UIViewController {
var allImageViews : [UIImageView] = []
var allCenters : NSMutableArray = NSMutableArray()
var emptySpot : CGPoint = CGPoint()
var originalCenters : NSMutableArray = NSMutableArray()
override func viewDidLoad() {
super.viewDidLoad()
// Center point for piece of image for iPhone 5S simulator
var xCen : CGFloat = 53
var yCen : CGFloat = 53
// arrangement of 3*3 piece of images
for v in 0...2{
for h in 0...2{
var pieces = [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
let filename = String(format: "Smile_%02i.gif", h+v*3)
let Image = UIImage(named: filename)
let myImageView = UIImageView(image: Image)
var curCenter : CGPoint = CGPointMake(xCen, yCen)
//adding centerpoint to allCenter array
allCenters.addObject(NSValue(CGPoint : curCenter))
myImageView.frame = CGRectMake(0, 0, 106, 106)
myImageView.center = curCenter
myImageView.userInteractionEnabled = true
//adding all Images in array allImageviews
allImageViews.append(myImageView)
self.view.addSubview(myImageView)
//Increment on X-Axis
xCen += 106
}
//again sex X centerpoint to 53
xCen = 53
//Increment on Y-Axis
yCen += 106
}
// romove one piece at index 0
allImageViews.removeAtIndex(0).removeFromSuperview()
// we have all 8 imageview and all 9 centerpoints
self.randomizeBlocks()
}
// placing the piece of images randomly
func randomizeBlocks(){
// new array that copy centerpoint from allCenters
var centersCopy: NSMutableArray = allCenters.mutableCopy() as NSMutableArray
var randLocInt : Int
var randLoc : CGPoint
// loop for all peice of images saved in allImageViews
for i in allImageViews {
//Place Images randomly
randLocInt = Int(arc4random()) % centersCopy.count
randLoc = centersCopy.objectAtIndex(randLocInt).CGPointValue()
i.center = randLoc
centersCopy.removeObjectAtIndex(randLocInt)
}
// for finding empyblock which is at index 0
emptySpot = centersCopy.objectAtIndex(0).CGPointValue()
}
var leftisEmpty : Bool = Bool()
var rightisEmpty : Bool = Bool()
var topisEmpty : Bool = Bool()
var bottomisEmpty : Bool = Bool()
// touch event
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
var topCen : CGPoint
var left : CGPoint
var right : CGPoint
var top : CGPoint
var bottom : CGPoint
let touches = touches.allObjects as [UITouch]
var myTouch = touches.first
if let myTouch = myTouch{
topCen = myTouch.view.center
// movement to empty block
left = CGPointMake(topCen.x - 106, topCen.y)
right = CGPointMake(topCen.x + 106, topCen.y)
top = CGPointMake(topCen.x, topCen.y+106)
bottom = CGPointMake(topCen.x, topCen.y-106)
// Deciding which side is empty
if emptySpot == left{
leftisEmpty = true
}
if emptySpot == right{
rightisEmpty = true
}
if emptySpot == top{
topisEmpty = true
}
if emptySpot == bottom{
bottomisEmpty = true
}
if leftisEmpty || rightisEmpty || topisEmpty || bottomisEmpty{
UIView.beginAnimations(nil, context:nil)
UIView.setAnimationDuration(0.5)
myTouch.view.center = emptySpot
UIView.commitAnimations()
emptySpot = topCen
leftisEmpty = false
rightisEmpty = false
topisEmpty = false
bottomisEmpty = false
}
}
}
}
我现在被困在当用户完成整个画面我怎么能比较所有图片的位置,所以应该警惕的消息出现,并说“游戏是完整的”。我没有任何想法,我该怎么做。
请帮我完成这个游戏。
在此先感谢。
只需维护一个带有@ [@“piece1”:false,@“piece2”:true ...等对象的字典)。因此,当用户在正确的位置放置一块拼图时,可以将其设置为true,然后检查是否全部为真。如果真的那么游戏结束了。 – nikhil84 2014-10-10 08:48:18
你可以在编辑我的代码吗? – 2014-10-10 09:33:40
可能会在您的代码中提出一些解释/评论,以便我更好地理解,然后我会相应地做出更改。 – nikhil84 2014-10-10 09:43:06