我创造了一个能够永久地产生硬币的功能。 但是,我在移除碰到它们的球触及的硬币时遇到了麻烦。顺便说一下,我在类中有一个名为coinBoolean的变量设置为false。这是我的代码:
func didBeginContact(contact: SKPhysicsContact) {
if ((contact.bodyA.categoryBitMask == groundGroup && contact.bodyB.categoryBitMask == ballGroup) ||
(contact.bodyA.categoryBitMask == ballGroup && contact.bodyB.categoryBitMask == groundGroup)) {
}
if ((contact.bodyA.categoryBitMask == ballGroup && contact.bodyB.categoryBitMask == coinGroup) ||
(contact.bodyA.categoryBitMask == coinGroup && contact.bodyB.categoryBitMask == ballGroup)) {
print("TRUE")
coinBoolean = true
}
else {
print("false")
coinBoolean = false
}
}
func addAndMoveCoins() {
let coin = SKSpriteNode(imageNamed: "Coin")
coin.zPosition = 55
coin.position = CGPoint(x: self.frame.size.width*0.9, y: randomY())
coin.physicsBody = SKPhysicsBody(rectangleOfSize: coin.size)
coin.physicsBody?.categoryBitMask = coinGroup
coin.physicsBody?.contactTestBitMask = ballGroup
coin.physicsBody?.affectedByGravity = false
coin.physicsBody?.dynamic = false
let moveCoinLeft = SKAction.moveByX(-self.size.height, y: 0, duration: 5)
let repeatAction1 = SKAction.repeatActionForever(moveCoinLeft)
let removeObstacle1 = SKAction.removeFromParent()
let moveAndRemove1 = SKAction.sequence([repeatAction1, removeObstacle1])
coin.runAction(moveAndRemove1)
self.addChild(coin)
if coinBoolean == true {
coin.removeFromParent()
}
else if coinBoolean == false {
}
}
func repeatCoins() {
let generateCoins = SKAction.sequence([SKAction.runBlock(self.addAndMoveCoins), SKAction.waitForDuration(1.3)])
let endlessAction = SKAction.repeatActionForever(generateCoins)
runAction(endlessAction)
}
答案 0 :(得分:0)
您的coinBoolean
变量可能变化太快。我建议从碰撞(bodyA或bodyB)中获取要删除的节点,并将其从父节点
答案 1 :(得分:0)
在接触阶段只需在硬币对象上设置一个动作,而不是做布尔值。
foreach (var time in times)
{
var now = time.Key;
var expected = time.Value;
TimeSpan timer;
if (now <= startTime) {
// no need to do anything here - just substract
timer = startTime - now;
}
else {
// normalize start time to current month
var normalized = new DateTime(now.Year, now.Month, startTime.Day, startTime.Hour, startTime.Minute, startTime.Second);
if (normalized >= now) {
// normalized date is later in the same month - substract
timer = normalized - now;
}
else {
// normalized date is before current - move to next month
timer = normalized.AddMonths(1) - now;
}
}
if (last.Date != now.Date) Console.WriteLine();
Console.WriteLine($"{now:yyyy-MM-dd HH:mm} -> {startTime:yyyy-MM-dd HH:mm} = {timer:dd\\.hh} {(timer != expected ? "EXPECTED " + expected.ToString("dd\\.hh") : "CORRECT ")}");
last = now;
}
你需要检查bodyA当然是否是硬币对象