CGRect.maxX — gameArea.maxX
up vote
0
down vote
favorite
I am trying to keep the object in the screen/gameArea and i simply can't.
I use these if statement and it prevents the object from moveing to the right side
if player.position.x > gameArea.maxX - player.size.width/2 {
player.position.x = gameArea.maxX - player.size.width/2
}
but i can't apply to the other side because it crashes
if player.position.x < gameArea.maxX - player.size.width/2 {
player.position.x = gameArea.maxX - player.size.width/2
}
ios swift cgrect
add a comment |
up vote
0
down vote
favorite
I am trying to keep the object in the screen/gameArea and i simply can't.
I use these if statement and it prevents the object from moveing to the right side
if player.position.x > gameArea.maxX - player.size.width/2 {
player.position.x = gameArea.maxX - player.size.width/2
}
but i can't apply to the other side because it crashes
if player.position.x < gameArea.maxX - player.size.width/2 {
player.position.x = gameArea.maxX - player.size.width/2
}
ios swift cgrect
1
What is the error message from the crash? That should provide some useful information, or at least indicate what line the crash is on.
– Craig Siemens
Nov 22 at 16:12
Your second example usesgameArea.maxX
as well, when it probably should usegameArea.minX
. This shouldn't necessarily be the source of the crash, but if there was a floating point error then both of them might fire in the same loop.
– n_b
Nov 22 at 16:25
if player.position.x > gameArea.maxX - player.size.width { player.position.x = gameArea.maxX - player.size.width } if player.position.x < gameArea.minX + player.size.width { player.position.x = gameArea.minX + player.size.width }
– Genti Malm
Nov 22 at 16:35
i fixed it it works with these combinations of if statements instead
– Genti Malm
Nov 22 at 16:35
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am trying to keep the object in the screen/gameArea and i simply can't.
I use these if statement and it prevents the object from moveing to the right side
if player.position.x > gameArea.maxX - player.size.width/2 {
player.position.x = gameArea.maxX - player.size.width/2
}
but i can't apply to the other side because it crashes
if player.position.x < gameArea.maxX - player.size.width/2 {
player.position.x = gameArea.maxX - player.size.width/2
}
ios swift cgrect
I am trying to keep the object in the screen/gameArea and i simply can't.
I use these if statement and it prevents the object from moveing to the right side
if player.position.x > gameArea.maxX - player.size.width/2 {
player.position.x = gameArea.maxX - player.size.width/2
}
but i can't apply to the other side because it crashes
if player.position.x < gameArea.maxX - player.size.width/2 {
player.position.x = gameArea.maxX - player.size.width/2
}
ios swift cgrect
ios swift cgrect
edited Nov 22 at 17:16
rmaddy
237k27308374
237k27308374
asked Nov 22 at 16:06
Genti Malm
1
1
1
What is the error message from the crash? That should provide some useful information, or at least indicate what line the crash is on.
– Craig Siemens
Nov 22 at 16:12
Your second example usesgameArea.maxX
as well, when it probably should usegameArea.minX
. This shouldn't necessarily be the source of the crash, but if there was a floating point error then both of them might fire in the same loop.
– n_b
Nov 22 at 16:25
if player.position.x > gameArea.maxX - player.size.width { player.position.x = gameArea.maxX - player.size.width } if player.position.x < gameArea.minX + player.size.width { player.position.x = gameArea.minX + player.size.width }
– Genti Malm
Nov 22 at 16:35
i fixed it it works with these combinations of if statements instead
– Genti Malm
Nov 22 at 16:35
add a comment |
1
What is the error message from the crash? That should provide some useful information, or at least indicate what line the crash is on.
– Craig Siemens
Nov 22 at 16:12
Your second example usesgameArea.maxX
as well, when it probably should usegameArea.minX
. This shouldn't necessarily be the source of the crash, but if there was a floating point error then both of them might fire in the same loop.
– n_b
Nov 22 at 16:25
if player.position.x > gameArea.maxX - player.size.width { player.position.x = gameArea.maxX - player.size.width } if player.position.x < gameArea.minX + player.size.width { player.position.x = gameArea.minX + player.size.width }
– Genti Malm
Nov 22 at 16:35
i fixed it it works with these combinations of if statements instead
– Genti Malm
Nov 22 at 16:35
1
1
What is the error message from the crash? That should provide some useful information, or at least indicate what line the crash is on.
– Craig Siemens
Nov 22 at 16:12
What is the error message from the crash? That should provide some useful information, or at least indicate what line the crash is on.
– Craig Siemens
Nov 22 at 16:12
Your second example uses
gameArea.maxX
as well, when it probably should use gameArea.minX
. This shouldn't necessarily be the source of the crash, but if there was a floating point error then both of them might fire in the same loop.– n_b
Nov 22 at 16:25
Your second example uses
gameArea.maxX
as well, when it probably should use gameArea.minX
. This shouldn't necessarily be the source of the crash, but if there was a floating point error then both of them might fire in the same loop.– n_b
Nov 22 at 16:25
if player.position.x > gameArea.maxX - player.size.width { player.position.x = gameArea.maxX - player.size.width } if player.position.x < gameArea.minX + player.size.width { player.position.x = gameArea.minX + player.size.width }
– Genti Malm
Nov 22 at 16:35
if player.position.x > gameArea.maxX - player.size.width { player.position.x = gameArea.maxX - player.size.width } if player.position.x < gameArea.minX + player.size.width { player.position.x = gameArea.minX + player.size.width }
– Genti Malm
Nov 22 at 16:35
i fixed it it works with these combinations of if statements instead
– Genti Malm
Nov 22 at 16:35
i fixed it it works with these combinations of if statements instead
– Genti Malm
Nov 22 at 16:35
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53434710%2fcgrect-maxx-gamearea-maxx%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
1
What is the error message from the crash? That should provide some useful information, or at least indicate what line the crash is on.
– Craig Siemens
Nov 22 at 16:12
Your second example uses
gameArea.maxX
as well, when it probably should usegameArea.minX
. This shouldn't necessarily be the source of the crash, but if there was a floating point error then both of them might fire in the same loop.– n_b
Nov 22 at 16:25
if player.position.x > gameArea.maxX - player.size.width { player.position.x = gameArea.maxX - player.size.width } if player.position.x < gameArea.minX + player.size.width { player.position.x = gameArea.minX + player.size.width }
– Genti Malm
Nov 22 at 16:35
i fixed it it works with these combinations of if statements instead
– Genti Malm
Nov 22 at 16:35