sprite kit - Swift Comparing two color objects to see if their color is the same with touch -


the choiceball set black randomly changes color match 1 of other balls each tap. idea tap the other balls according choiceball's color is.

for example: choiceball turns blue. touch blue ball. choiceball turns red touch red ball etc.

how can check if have tapped right ball according choiceball. can implement score board keep score.

thanks :)

class gamescene: skscene {     var choiceball = skshapenode(circleofradius: 50)    var blueball = skshapenode(circleofradius: 50)    var greenball = skshapenode(circleofradius: 50)    var yellowball = skshapenode(circleofradius: 50)    var redball = skshapenode(circleofradius: 50)     var array = [skcolor(red: 0.62, green: 0.07, blue: 0.04, alpha: 1), skcolor(red: 0, green: 0.6, blue: 0.8, alpha: 1), skcolor(red: 0, green: 0.69, blue: 0.1, alpha: 1), skcolor(red: 0.93, green: 0.93, blue: 0, alpha: 1)]   override func didmovetoview(view: skview) {      choiceball.position = cgpointmake(self.frame.size.width / 2, self.frame.size.height / 2)     choiceball.fillcolor = skcolor.blackcolor()     self.addchild(choiceball)      blueball.position = cgpointmake(self.frame.size.width * 0.35, self.frame.size.height * 0.25)     blueball.fillcolor = skcolor(red: 0, green: 0.6, blue: 0.8, alpha: 1)     self.addchild(blueball)      redball.position = cgpointmake(self.frame.size.width * 0.65, self.frame.size.height * 0.75)     redball.fillcolor = skcolor(red: 0.62, green: 0.07, blue: 0.04, alpha: 1)     self.addchild(redball)      yellowball.position = cgpointmake(self.frame.size.width * 0.35, self.frame.size.height * 0.75)     yellowball.fillcolor = skcolor(red: 0.93, green: 0.93, blue: 0, alpha: 1)     self.addchild(yellowball)      greenball.position = cgpointmake(self.frame.size.width * 0.65, self.frame.size.height * 0.25)     greenball.fillcolor = skcolor(red: 0, green: 0.69, blue: 0.1, alpha: 1)     self.addchild(greenball) }   override func touchesbegan(touches: set<nsobject>, withevent event: uievent) {      let randomindex = int(arc4random_uniform(uint32(array.count)))     choiceball.fillcolor = array[randomindex] } 

basically need check touches collisions balls , if collide, can check colors. don't forget skip choice ball if don't want checked taps well.

for t in touches { // check each touch     let touch = t as! uitouch // convert uitouch pre swift 2.0     let pos = touch.locationinnode(self) // find touch position      child in self.children { // check each children in scene         if let ball = child as? skshapenode { // convert child shape node             if ball !== choiceball && ball.containspoint(pos) { // check collision, skip if it's choice ball                 if ball.fillcolor == choiceball.fillcolor { // collision found, check color                     // stuff, colors match                 }             }         }     } } 

Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

Nuget pack csproj using nuspec -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -