0

This code colorizes my ball.

var colorize1 = SKAction.colorizeWithColor(.redColor(), colorBlendFactor: 1.0, duration: 0.3) var colorize2 = SKAction.colorizeWithColor(.greenColor(), colorBlendFactor: 1.0, duration: 0.3) var colorize3 = SKAction.colorizeWithColor(.blueColor(), colorBlendFactor: 1.0, duration: 0.3) var actions = [colorize1, colorize2, colorize3] var randomIndex = Int(arc4random_uniform(3)) var action = actions[randomIndex] let seconds = 0.14 let delay = seconds * Double(NSEC_PER_SEC) // nanoseconds per seconds let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delay)) dispatch_after(dispatchTime, dispatch_get_main_queue(), { self.Ball.runAction(action) }) } 

This code colorizes my walls.

var colorBucket = [UIColor]() func randomColor() -> UIColor { if colorBucket.isEmpty { fillBucket() } let randomIndex = Int(arc4random_uniform(UInt32(colorBucket.count))) let randomColor = colorBucket[randomIndex] colorBucket.removeAtIndex(randomIndex) return randomColor } func fillBucket() { colorBucket = [UIColor.redColor(), UIColor.greenColor(), UIColor.blueColor()] } 

I need to write an if statement that detects whether or not the ball and wall are the same color when I collide with an spritekitnode() located in between my walls.

I've tried these:

if Wall1.color == UIColor.redColor() && Wall2.color == UIColor.redColor() && Ball.color != UIColor.redColor { died = true } 

My main issue is writing the if statement that determines if the ball and walls are the same color. I have the collision part down. Thank you so much for your help.

func didBeginContact(contact: SKPhysicsContact) { let firstBody = contact.bodyA let secondBody = contact.bodyB let ballWasContacted = firstBody.categoryBitMask == PhysicsCat.Ball || secondBody.categoryBitMask == PhysicsCat.Ball var wallWasContacted = firstBody.categoryBitMask == PhysicsCat.Wall || secondBody.categoryBitMask == PhysicsCat.Wall let scoreWasContacted = firstBody.categoryBitMask == PhysicsCat.Score || secondBody.categoryBitMask == PhysicsCat.Score let colorWasContacted = firstBody.categoryBitMask == PhysicsCat.Color || secondBody.categoryBitMask == PhysicsCat.Ball if ballWasContacted { if scoreWasContacted { score += 1 scoreLbl.text = "\(score)" let scoreNode = firstBody.categoryBitMask == PhysicsCat.Score ? firstBody.node : secondBody.node scoreNode!.removeFromParent() } else if wallWasContacted { enumerateChildNodesWithName("wallPair", usingBlock: ({ (node, error) in node.speed = 0 self.removeAllActions() })) if died == false { died = true createBTN() fallDie() } } } else if colorWasContacted { } } 

The colorWasContacted isn't really being used right now, but everything else is.

Code to change color of wall.

action = SKAction.colorizeWithColor(randomColor(), colorBlendFactor: 1.0, duration: 0.3) 

Wall1.runAction(action)

This is what happened when I added the breakpoints.

Breakpoints

2 Answers 2

1

Contacts only happen with two elements at a time, so you could do something like this in your didBeginContact (edit, based on your actual code):

... else if wallWasContacted { let sprite1 = firstBody.node as! SKSpriteNode let sprite2 = secondBody.node as! SKSpriteNode if sprite1.color.isEqual(sprite2.color) { // Whatever your heart desires... } else { // Something else you'd like to have done... } enumerateChildNodesWithName("wallPair", usingBlock: ({ (node, error) in node.speed = 0 self.removeAllActions() })) if died == false { died = true createBTN() fallDie() } } 

As this might help with this isolated issue, I belive (from looking through your other threads) that you need to rethink the whole logic when it comes to discovering passing between walls...

Sign up to request clarification or add additional context in comments.

19 Comments

I got this error. "Value of type 'SKNode?' has no member 'color'"
Yeah, but you really need to start not expecting every answer to be a 100% finished thing you can just copy and paste... In this case you need to cast the nodes to SKSpriteNodes first. But for all I know, you could have a subclass that you need to access some special function nobody at S.O. knows about...
I appreciate your help. I know it can be aggravating to deal with a beginner like me. What do I need to do? I have scoreNode that increases my score when my ball collides with it. I don't have a subclass. I have walls that colorize, and a ball that colorizes, and collision properties for the walls when my ball hits it.
Don't worry about being a beginner. We've all been there. Please don't abandon your own questions though when people reply. Could you please show me your didBeginContact as I believe there might be something to hook up to there that would make a lot of sense before I change the above...
Is the only way to detect if my the color of my ball and the color of two walls are the same is to have a collision while the ball is in between, and then when I collide, ask if the ball and walls are the same color, if they are, then somehow ignore the collision, but if they differ, collide?
|
0

As you are mixing questions and re-asking them (worded slightly differently) I'll attempt dealing with your colorization problems here as well, as this is most likely where you are failing. I say most likely because the code you supply does not show us that you colorize the walls at all.

It shows us that:

  1. You have logic in place to change color on the ball.
  2. You have logic to pick out a random color from a colorBucket.
  3. You have logic to refill the bucket when all the colors have been used.

It does not show us that you actually change the color-information for the walls. Could you please supply this part of the code (if it exists).

5 Comments

There's an issue that you might be able to explain. I don't want a solution to the problem, just an explanation. So when I write the print(Wall5.color) I receive this: UIDeviceRGBColorSpace 1 1 1 0. But the colors of walls are changing. Yet each time a new set of walls spawn it repeats that print statement. But if I say Wall5.color = UIColor.redColor() it's obviously going to continuously print UIDeviceRGBColorSpace 1 0 0 1. I guess I need to somehow store the color that it picks from the randomColor() each time its called.
No, the idea is that you hook into what's already available to you. The SpriteNodes have a property color. When you change the color the property is also changed. Have you tried setting those breakpoints to see that you get the expected behavior?
I uploaded a picture of what happened when I added the breakpoints. Updated it in the question section.
Is there perhaps some way we can move this discussion out of Slack (this isn't really the venue for it). Anyways: You need to learn how to debug your code: While at the breakpoint you can type the following in the right-hand panel, bellow (lldb) po sprite1.color and press enter then po sprite2.color and present enter. This should print out the color values for the two nodes at the time of contact.
My email is [email protected], sprite1 was red when Wall1 was red. and Sprite2 was UIDeviceRGBColorSpace 1 2.98023e-08 2.98023e-08 1... Which i think is the color of my ball. I think those big numbers happen when it colorizes my ball the same color twice.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.