My if statement in spritekit (swift isn't working). I'm trying to increase the number of obstacles as the...





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-1















I'm creating a game in Spritekit where a player has to collect coins. While collecting them, they have to avoid bombs. Basically, I want to increase the number of bombs that appear as the score gets higher... so I used an if statement - but it isn't working. The number of bombs that appear tends to stay the same:



 if self.score >= 0 && self.score < 20 {

bombTimer = Timer.scheduledTimer(withTimeInterval: 4, repeats: true, block: { (timer) in
self.createBomb()
})

} else if self.score >= 20 && self.score < 35 {

bombTimer = Timer.scheduledTimer(withTimeInterval: 3, repeats: true, block: { (timer) in
self.createBomb()
})

} else if self.score >= 35 && self.score < 50 {

bombTimer = Timer.scheduledTimer(withTimeInterval: 2, repeats: true, block: { (timer) in
self.createBomb()
})

} else if self.score >= 50 && self.score < 150 {

bombTimer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { (timer) in
self.createBomb()
})

}









share|improve this question

























  • What are you doing with the bombTimer? Have you checked to see that createBomb works properly? Or that as the score increases, the code falls into different if clauses?

    – tomerpacific
    Jan 3 at 22:01











  • bombTimer is my variable of type Timer (var bombTimer : Timer?). I have checked that 'createBomb' works properly. It's just that the number of bombs that appear is still the same.

    – Ron M
    Jan 3 at 22:21


















-1















I'm creating a game in Spritekit where a player has to collect coins. While collecting them, they have to avoid bombs. Basically, I want to increase the number of bombs that appear as the score gets higher... so I used an if statement - but it isn't working. The number of bombs that appear tends to stay the same:



 if self.score >= 0 && self.score < 20 {

bombTimer = Timer.scheduledTimer(withTimeInterval: 4, repeats: true, block: { (timer) in
self.createBomb()
})

} else if self.score >= 20 && self.score < 35 {

bombTimer = Timer.scheduledTimer(withTimeInterval: 3, repeats: true, block: { (timer) in
self.createBomb()
})

} else if self.score >= 35 && self.score < 50 {

bombTimer = Timer.scheduledTimer(withTimeInterval: 2, repeats: true, block: { (timer) in
self.createBomb()
})

} else if self.score >= 50 && self.score < 150 {

bombTimer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { (timer) in
self.createBomb()
})

}









share|improve this question

























  • What are you doing with the bombTimer? Have you checked to see that createBomb works properly? Or that as the score increases, the code falls into different if clauses?

    – tomerpacific
    Jan 3 at 22:01











  • bombTimer is my variable of type Timer (var bombTimer : Timer?). I have checked that 'createBomb' works properly. It's just that the number of bombs that appear is still the same.

    – Ron M
    Jan 3 at 22:21














-1












-1








-1


0






I'm creating a game in Spritekit where a player has to collect coins. While collecting them, they have to avoid bombs. Basically, I want to increase the number of bombs that appear as the score gets higher... so I used an if statement - but it isn't working. The number of bombs that appear tends to stay the same:



 if self.score >= 0 && self.score < 20 {

bombTimer = Timer.scheduledTimer(withTimeInterval: 4, repeats: true, block: { (timer) in
self.createBomb()
})

} else if self.score >= 20 && self.score < 35 {

bombTimer = Timer.scheduledTimer(withTimeInterval: 3, repeats: true, block: { (timer) in
self.createBomb()
})

} else if self.score >= 35 && self.score < 50 {

bombTimer = Timer.scheduledTimer(withTimeInterval: 2, repeats: true, block: { (timer) in
self.createBomb()
})

} else if self.score >= 50 && self.score < 150 {

bombTimer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { (timer) in
self.createBomb()
})

}









share|improve this question
















I'm creating a game in Spritekit where a player has to collect coins. While collecting them, they have to avoid bombs. Basically, I want to increase the number of bombs that appear as the score gets higher... so I used an if statement - but it isn't working. The number of bombs that appear tends to stay the same:



 if self.score >= 0 && self.score < 20 {

bombTimer = Timer.scheduledTimer(withTimeInterval: 4, repeats: true, block: { (timer) in
self.createBomb()
})

} else if self.score >= 20 && self.score < 35 {

bombTimer = Timer.scheduledTimer(withTimeInterval: 3, repeats: true, block: { (timer) in
self.createBomb()
})

} else if self.score >= 35 && self.score < 50 {

bombTimer = Timer.scheduledTimer(withTimeInterval: 2, repeats: true, block: { (timer) in
self.createBomb()
})

} else if self.score >= 50 && self.score < 150 {

bombTimer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { (timer) in
self.createBomb()
})

}






swift if-statement






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 22:23







Ron M

















asked Jan 3 at 21:55









Ron MRon M

13




13













  • What are you doing with the bombTimer? Have you checked to see that createBomb works properly? Or that as the score increases, the code falls into different if clauses?

    – tomerpacific
    Jan 3 at 22:01











  • bombTimer is my variable of type Timer (var bombTimer : Timer?). I have checked that 'createBomb' works properly. It's just that the number of bombs that appear is still the same.

    – Ron M
    Jan 3 at 22:21



















  • What are you doing with the bombTimer? Have you checked to see that createBomb works properly? Or that as the score increases, the code falls into different if clauses?

    – tomerpacific
    Jan 3 at 22:01











  • bombTimer is my variable of type Timer (var bombTimer : Timer?). I have checked that 'createBomb' works properly. It's just that the number of bombs that appear is still the same.

    – Ron M
    Jan 3 at 22:21

















What are you doing with the bombTimer? Have you checked to see that createBomb works properly? Or that as the score increases, the code falls into different if clauses?

– tomerpacific
Jan 3 at 22:01





What are you doing with the bombTimer? Have you checked to see that createBomb works properly? Or that as the score increases, the code falls into different if clauses?

– tomerpacific
Jan 3 at 22:01













bombTimer is my variable of type Timer (var bombTimer : Timer?). I have checked that 'createBomb' works properly. It's just that the number of bombs that appear is still the same.

– Ron M
Jan 3 at 22:21





bombTimer is my variable of type Timer (var bombTimer : Timer?). I have checked that 'createBomb' works properly. It's just that the number of bombs that appear is still the same.

– Ron M
Jan 3 at 22:21












1 Answer
1






active

oldest

votes


















0














If you are writing code inside of a SKScene class I like to use SKActions rather than NSTimers since the actions will pause when the scene is paused.



Paste this function inside your scene class somewhere.
Using a switch statement is nicer to use when working with a buttload of conditions



func getBombInterval() -> Double? {
switch true {
case score >= 0 && score < 20: return 4
case score < 35: return 3
case score < 50: return 2
case score < 150: return 1
default: return nil
}
}


Then replace the code you posted above with this:



guard let interval = getBombInterval() else { print("Score is less than 0") ; return }
let waitAction = SKAction.wait(forDuration: interval)
run(waitAction, completion: { self.createBomb() } )





share|improve this answer
























  • I tried doing that, but after some time, the bombs stop appearing. Thanks for your help, though.

    – Ron M
    Jan 4 at 23:47












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54030338%2fmy-if-statement-in-spritekit-swift-isnt-working-im-trying-to-increase-the-n%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














If you are writing code inside of a SKScene class I like to use SKActions rather than NSTimers since the actions will pause when the scene is paused.



Paste this function inside your scene class somewhere.
Using a switch statement is nicer to use when working with a buttload of conditions



func getBombInterval() -> Double? {
switch true {
case score >= 0 && score < 20: return 4
case score < 35: return 3
case score < 50: return 2
case score < 150: return 1
default: return nil
}
}


Then replace the code you posted above with this:



guard let interval = getBombInterval() else { print("Score is less than 0") ; return }
let waitAction = SKAction.wait(forDuration: interval)
run(waitAction, completion: { self.createBomb() } )





share|improve this answer
























  • I tried doing that, but after some time, the bombs stop appearing. Thanks for your help, though.

    – Ron M
    Jan 4 at 23:47
















0














If you are writing code inside of a SKScene class I like to use SKActions rather than NSTimers since the actions will pause when the scene is paused.



Paste this function inside your scene class somewhere.
Using a switch statement is nicer to use when working with a buttload of conditions



func getBombInterval() -> Double? {
switch true {
case score >= 0 && score < 20: return 4
case score < 35: return 3
case score < 50: return 2
case score < 150: return 1
default: return nil
}
}


Then replace the code you posted above with this:



guard let interval = getBombInterval() else { print("Score is less than 0") ; return }
let waitAction = SKAction.wait(forDuration: interval)
run(waitAction, completion: { self.createBomb() } )





share|improve this answer
























  • I tried doing that, but after some time, the bombs stop appearing. Thanks for your help, though.

    – Ron M
    Jan 4 at 23:47














0












0








0







If you are writing code inside of a SKScene class I like to use SKActions rather than NSTimers since the actions will pause when the scene is paused.



Paste this function inside your scene class somewhere.
Using a switch statement is nicer to use when working with a buttload of conditions



func getBombInterval() -> Double? {
switch true {
case score >= 0 && score < 20: return 4
case score < 35: return 3
case score < 50: return 2
case score < 150: return 1
default: return nil
}
}


Then replace the code you posted above with this:



guard let interval = getBombInterval() else { print("Score is less than 0") ; return }
let waitAction = SKAction.wait(forDuration: interval)
run(waitAction, completion: { self.createBomb() } )





share|improve this answer













If you are writing code inside of a SKScene class I like to use SKActions rather than NSTimers since the actions will pause when the scene is paused.



Paste this function inside your scene class somewhere.
Using a switch statement is nicer to use when working with a buttload of conditions



func getBombInterval() -> Double? {
switch true {
case score >= 0 && score < 20: return 4
case score < 35: return 3
case score < 50: return 2
case score < 150: return 1
default: return nil
}
}


Then replace the code you posted above with this:



guard let interval = getBombInterval() else { print("Score is less than 0") ; return }
let waitAction = SKAction.wait(forDuration: interval)
run(waitAction, completion: { self.createBomb() } )






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 4 at 19:22









Muffinman2497Muffinman2497

343211




343211













  • I tried doing that, but after some time, the bombs stop appearing. Thanks for your help, though.

    – Ron M
    Jan 4 at 23:47



















  • I tried doing that, but after some time, the bombs stop appearing. Thanks for your help, though.

    – Ron M
    Jan 4 at 23:47

















I tried doing that, but after some time, the bombs stop appearing. Thanks for your help, though.

– Ron M
Jan 4 at 23:47





I tried doing that, but after some time, the bombs stop appearing. Thanks for your help, though.

– Ron M
Jan 4 at 23:47




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54030338%2fmy-if-statement-in-spritekit-swift-isnt-working-im-trying-to-increase-the-n%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Angular Downloading a file using contenturl with Basic Authentication

Olmecas

Can't read property showImagePicker of undefined in react native iOS