My if statement in spritekit (swift isn't working). I'm trying to increase the number of obstacles as the...
![Multi tool use Multi tool use](http://sgv.ssvwv.com/sg/ssvwvcomimagb.png)
Multi tool use
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
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
add a comment |
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
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
add a comment |
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
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
swift if-statement
edited Jan 3 at 22:23
Ron M
asked Jan 3 at 21:55
![](https://lh3.googleusercontent.com/-q8J6Yk4Y6B4/AAAAAAAAAAI/AAAAAAAAA9Y/nH7ZCZdBhV0/photo.jpg?sz=32)
![](https://lh3.googleusercontent.com/-q8J6Yk4Y6B4/AAAAAAAAAAI/AAAAAAAAA9Y/nH7ZCZdBhV0/photo.jpg?sz=32)
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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() } )
I tried doing that, but after some time, the bombs stop appearing. Thanks for your help, though.
– Ron M
Jan 4 at 23:47
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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() } )
I tried doing that, but after some time, the bombs stop appearing. Thanks for your help, though.
– Ron M
Jan 4 at 23:47
add a comment |
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() } )
I tried doing that, but after some time, the bombs stop appearing. Thanks for your help, though.
– Ron M
Jan 4 at 23:47
add a comment |
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() } )
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() } )
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
leOUr,mS pdCRJ2V,evzKQDC9e2jRU Vhf4FNRNcT6UkqI3ZSDp0,QFQ1Xso zObANrPyW,zx0T8hH,00x57PWW5
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