2014-10-10 48 views
0

我知道这是愚蠢的问题。 我正在开发一个益智游戏和代码是:如何保存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 
      } 
     } 
    } 
} 

我现在被困在当用户完成整个画面我怎么能比较所有图片的位置,所以应该警惕的消息出现,并说“游戏是完整的”。我没有任何想法,我该怎么做。

请帮我完成这个游戏。

在此先感谢。

+0

只需维护一个带有@ [@“piece1”:false,@“piece2”:true ...等对象的字典)。因此,当用户在正确的位置放置一块拼图时,可以将其设置为true,然后检查是否全部为真。如果真的那么游戏结束了。 – nikhil84 2014-10-10 08:48:18

+0

你可以在编辑我的代码吗? – 2014-10-10 09:33:40

+0

可能会在您的代码中提出一些解释/评论,以便我更好地理解,然后我会相应地做出更改。 – nikhil84 2014-10-10 09:43:06

回答

1

你的代码的变化和解释: -

override func viewDidLoad() { 
    super.viewDidLoad() 
      ........YOUR CODE...... 


    //READ below....... 
    var tagValue: NSInteger = 0 //change it as me typing directly in post. 
    var imageDict: Dictionary<String, String> = [:] 
    var posStateDict: Dictionary<String, String> = [:] //Change it as per swift format. 

    var frame: CGPoint = CGPointZero 

    for v in 0...2{ 

     for h in 0...2{ 

      ........YOUR CODE...... 

      //......UPDATED....... 
      //Now you should also add a tag value to your imageView 

      myImageView.tag = tagValue 
      myImageView.userInteractionEnabled = true 

      frame = CGPointMake(xCen, yCen) 
      imageDict.setValue(NSStringFromCGPoint(frame), forKey: NSString.stringWithString("/(tagValue)"));  

      posStateDict[NSString.stringWithString("(d)")] = ["false"]     
      tagValue = tagValue + 1; //Incrementing tagValue. 

     } 
         ........YOUR CODE...... 
    } 
        ........YOUR CODE...... 
} 

// touch event 
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) { 

        ........YOUR CODE...... 

     if leftisEmpty || rightisEmpty || topisEmpty || bottomisEmpty{ 

        ........YOUR CODE...... 


      //READ below.......Rest code here. 
      //Here you need to get that imageView object which is moved. 
      //From that imageView you need to get tag value. 
      //With the tag value you could search it's orignal frame from the imageDict and compare the frame of it. 
      //If frame match then set their respective object true in posStateDict dictionary. 
      //Now check this dictionary if all true and no false then game is over. 
     } 
    } 
} 

}

对不起,我没能为零部件代码作为了一些工作。希望这个解释能帮助你。

+0

请你可以让代码更具可读性,因为我没有正确理解它。 :( – 2014-10-10 11:04:28

+0

我已经把一个标签// READ放在下面.......所以你需要把它读出来。我没有改变代码,但提出了解释做什么,因为我有时间限制。需要编码该部分 – nikhil84 2014-10-10 11:09:33

+0

你能告诉我如何为框架(X,Y)分配值吗? – 2014-10-10 12:23:10

0

也许将原始imageView中心保存在一个数组中,然后将其与新位置进行比较?

1

你应该有一个3x3阵列,它包含当前拼图在每个位置的编号。最初,这将是

var pieces = [[0, 1, 2], [3, 4, 5], [6, 7, 8]] 

其中0表示空白点。每次移动一个拼图时,该阵列都会被更新,例如, 当片#3向上移动然后

pieces = [[3, 1, 2], [0, 4, 5], [6, 7, 8]] 

如果pieces是等于初始阵列再次游戏结束。

相关问题