gpt4 book ai didi

swift - 基于随机着色比较两个 Sprite 颜色?

转载 作者:行者123 更新时间:2023-11-30 10:02:05 25 4
gpt4 key购买 nike

此代码为我的球着色。

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

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

我已经尝试过这些:

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

died = true

}

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

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 目前并未真正使用,但其他所有内容都已使用。

更改墙壁颜色的代码。

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

Wall1.runAction( Action )

这就是我添加断点时发生的情况。

Breakpoints

最佳答案

一次只能与两个元素发生联系,因此您可以在 didBeginContact 中执行类似的操作(根据您的实际代码进行编辑):

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

由于这可能有助于解决这个孤立的问题,我相信(通过查看您的其他线程)您需要重新思考整个逻辑,当涉及到发现在墙壁之间传递时......

关于swift - 基于随机着色比较两个 Sprite 颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37873958/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com