基于随机着色比较两种精灵颜色? [英] Comparing two sprites colors based on random colorization?

查看:153
本文介绍了基于随机着色比较两种精灵颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码将我的球着色。

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)


        })

    }

此代码着色我的墙。

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()]
}

当我与位于墙壁之间的spritekitnode()碰撞时,我需要编写一个if语句来检测球和墙是否是相同的颜色。

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.

我试过这些:

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

died = true

}

我的主要问题是编写if语句来确定球和墙是否是相同的颜色。我把碰撞部分放下了。
非常感谢你的帮助。

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 {


    }

}

colorWasContacted isn'现在真的被使用,但其他一切都是。

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

改变墙壁颜色的代码。

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

Wall1.runAction(动作)

Wall1.runAction(action)

这就是h当我添加断点时出现。

This is what happened when I added the breakpoints.

断点

推荐答案

联系人一次只发生两个元素,所以你可以在你的 didBeginContact中做类似的事情(根据您的实际代码编辑):

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...

这篇关于基于随机着色比较两种精灵颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆