I have followed Eagle1100's YouTube tutorial on how to build a simple iPhone game to get an idea of how that would work. In the series a PapiJump-like game is created. I've got it al running now but want to work further on it.
I've increased the number of platforms from 5 to 20 which works fine. However, when the initial 20 platforms move off screen I want 10 of them to show in a random spot. They do appear but often one or more overlap each other.
I have cracked my newb-brains over this, hope someone can help. How can I make the platforms appear random but not overlap each other?
This is a part of the code used to make the platforms appear in a random spot at the top of the screen.
//If the platforms move off the screen, then reset them at a random spot at the top
if (platform1.center.y >= (self.view.bounds.size.height + 8)) {
float x = random() % iViewWidthMinusPlatformWidth;
x = x + 25.0f;
float y = (random() % 20)-8;
platform1.center = CGPointMake(x,y);
}
if (platform2.center.y >= (self.view.bounds.size.height + 8)) {
float x = random() % iViewWidthMinusPlatformWidth;
x = x + 25.0f;
float y = (random() % 20)-8;
platform2.center = CGPointMake(x,y);
}
I have followed Eagle1100's YouTube tutorial on how to build a simple iPhone game to get an idea of how that would work. In the series a PapiJump-like game is created. I've got it al running now but want to work further on it.
I've increased the number of platforms from 5 to 20 which works fine. However, when the initial 20 platforms move off screen I want 10 of them to show in a random spot. They do appear but often one or more overlap each other.
I have cracked my newb-brains over this, hope someone can help. How can I make the platforms appear random but not overlap each other?
This is a part of the code used to make the platforms appear in a random spot at the top of the screen.
//If the platforms move off the screen, then reset them at a random spot at the top
if (platform1.center.y >= (self.view.bounds.size.height + 8)) {
float x = random() % iViewWidthMinusPlatformWidth;
x = x + 25.0f;
float y = (random() % 20)-8;
platform1.center = CGPointMake(x,y);
}
if (platform2.center.y >= (self.view.bounds.size.height + 8)) {
float x = random() % iViewWidthMinusPlatformWidth;
x = x + 25.0f;
float y = (random() % 20)-8;
platform2.center = CGPointMake(x,y);
}
... and so on for 10 platforms.
Thanks!!
you need to have a global variable (prevX/prevY) that saves the x (and/or y) info that is randomly generated by the first platform. then set a nested if statement for the next platform to be placed randomly, and make it create a random number until it does all of these: not outside screen width, not at more than (random() % 20)-8 above screen, and not within prevX/prevY + width of platform.
also make sure you stagger the height (the y placement) of the generated platforms or they will overlap.
p.s. Is this just a proof of concept? Or are you actually making another game like this?
Last edited by missing_no; 02-14-2011 at 07:02 PM.
you need to have a global variable (prevX/prevY) that saves the x (and/or y) info that is randomly generated by the first platform. then set a nested if statement for the next platform to be placed randomly, and make it create a random number until it does all of these: not outside screen width, not at more than (random() % 20)-8 above screen, and not within prevX/prevY + width of platform.
also make sure you stagger the height (the y placement) of the generated platforms or they will overlap.
p.s. Is this just a proof of concept? Or are you actually making another game like this?
Thanks for your reply! I get what you're saying but how would I put this in code?
This is just a POC, or do you see room for another jump game? ;-)
Thanks for your reply! I get what you're saying but how would I put this in code?
This is just a POC, or do you see room for another jump game? ;-)
Hey if you bring something new to plate there is always room and people will buy it. Doodle Jump ousted PapiJump because it had more charm, style, and support, so right now it's king of that category. But Kings are meant to be overthrown right?
post some pics of the overlap problem, so I know how to approach setting the variables. i can't promise anything since I'm also working feverishly on my game right now, but I'll try. Plus someone else might be able to help with some visual aid.