This site requires JavaScript, please enable it in your browser!
Greenfoot back
Minion1
Minion1 wrote ...

2012/12/22

Making a world map.

1
2
Minion1 Minion1

2012/12/22

#
So, I've designed a simple 9 panel world map for my character to play around in. The problem is, getting him to travel inside of it accurately is giving me some trouble. The concept is simple. There are nine areas, shaped like a box with nine smaller boxes: 1 2 3 4 5 6 7 8 9 And here is the code for him to travel inside it: if(level == 1) { if(getX() > 697) { level = 2; } if(getY() > 497) { level = 4; } } if(level == 2) { if(getX() > 694) { level = 3; } if(getX() < 4) { level = 1; } if(getY() > 494) { level = 5; } } if(level == 3) { if(getX() < 4) { level = 2; } if(getY() > 494) { level = 6; } } if(level == 4) { if(getX() > 694) { level = 5; } if(getY() < 4) { level = 1; } if(getY() > 494) { level = 7; } } if(level == 5) { if(getX() > 694) { level = 6; } if(getX() < 4) { level = 4; } if(getY() > 494) { level = 8; } if(getY() < 4) { level = 2; } } if(level == 6) { if(getX() < 4) { level = 5; } if(getY() < 4) { level = 3; } if(getY() > 494) { level = 9; } } if(level == 7) { if(getX() > 694) { level = 8; } if(getY() < 4) { level = 4; } } if(level == 8) { if(getX() < 4) { level = 7; } if(getX() > 694) { level = 9; } if(getY() < 4) { level = 5; } } if(level == 9) { if(getX() < 4) { level = 8; } if(getY() < 4) { level = 6; } } Now, a pro can spot the problem immediately. But I'm NOT a pro, so it took me a while. When he travels from an area of a greater number to an area of a lesser number, all works well because the if conditions for the lesser numbers has already been checked and found to be false. However, if he is traveling from say, area 1 and heads downward, then it checks the condition for area 1 and then takes him to area 4, and THEN in the same act cycle checks the condition for 4 and takes him to area 7. Crap. Anyone know of a way around this? Is there a way to have him only execute only ONE of these if statements per act cycle? A solution would be to break up each statement into it's own method, but... talk about sloppy programming. that's a LOT of excessive methods to stick into the act method. Can anyone help me? TIA -Minion
darkmist255 darkmist255

2012/12/22

#
Minion1 wrote...
Is there a way to have him only execute only ONE of these if statements per act cycle?
instead of "if(level == 3)" and so on, use 'else if' So your code would be
if(level == 1) { if(getX() > 697) { level = 2; } if(getY() > 497) { level = 4; } } 
	if(level == 2) { if(getX() > 694) { level = 3; } if(getX() < 4) { level = 1; } if(getY() > 494) { level = 5; } } 
	else if(level == 3) { if(getX() < 4) { level = 2; } if(getY() > 494) { level = 6; } } 
	else if(level == 4) { if(getX() > 694) { level = 5; } if(getY() < 4) { level = 1; } if(getY() > 494) { level = 7; } } 
	else if(level == 5) { if(getX() > 694) { level = 6; } if(getX() < 4) { level = 4; } if(getY() > 494) { level = 8; } if(getY() < 4) { level = 2; } } 
	else if(level == 6) { if(getX() < 4) { level = 5; } if(getY() < 4) { level = 3; } if(getY() > 494) { level = 9; } } 
	else if(level == 7) { if(getX() > 694) { level = 8; } if(getY() < 4) { level = 4; } } 
	else if(level == 8) { if(getX() < 4) { level = 7; } if(getX() > 694) { level = 9; } if(getY() < 4) { level = 5; } } 
	else if(level == 9) { if(getX() < 4) { level = 8; } if(getY() < 4) { level = 6; } } 
That should work! 'Else if' means it will only check if the 'if' right before it was false. There is a "cleaner" way to do it, but that would require your code being more dynamic (not hard-coded for each level) and with only 9 levels isn't that bad.
vonmeth vonmeth

2012/12/22

#
You could use if-else. Once an 'if' in the chain is satisfied, the rest are not evaluated.
if(level == 1) {
	if(getX() > 697)	level = 2;
	if(getY() > 497)	level = 4;
} else if(level == 2) {
    if(getX() > 694)	level = 3;
    if(getX() < 4)      level = 1;
    if(getY() > 494)	level = 5; 
} else if(level == 3) // etc. etc.
Minion1 Minion1

2012/12/22

#
Thanks
Minion1 Minion1

2012/12/22

#
I've modified the code.. And it's only half working.. It now works going to the right, but still jumps levels heading down.. Strange...
danpost danpost

2012/12/22

#
For one thing, you should move the character to the opposite side of the screen when the level changes. If you place the character just one pixel inside the point where he would go to back to the area he came from, you should be safe from him jumping back and forth between the two. I would suggest the following for the level changing code above:
if (getX()>694 && level%3!=0)
{
    level++;
    setLocation(4, getY());
}
if(getX()<4 && level%3!=1)
{
    level--;
    setLocation(694, getY());
}
if(getY()>497 && (level-1)/3!=2)
{
    level+=3;
    setLocation(getX(), 4);
}
if(getY()<4 && (level-1)/3!=0)
{
    level-=3;
    setLocation(getX(), 497);
}
Minion1 Minion1

2012/12/22

#
Yes.. He does jump to the other side of the screen, but there might be some overlap.. I need to check it out.
Minion1 Minion1

2012/12/22

#
So now that you see what I'm trying to accomplish, I need to ask a question: Is there a better way to design a map for my character to play on? I'm coming up with this myself and it just seems... Not as complete as it should be. Does anyone have any experience with this? Is there a better and more complete way?
danpost danpost

2012/12/22

#
Complete in what way? What do you think is missing? As far as a better map design, I do not think you will find one that would help to any major extent.
darkmist255 darkmist255

2012/12/22

#
Minion1, do you mean you want to make your game into a scrolling map, where it moves with your character, instead of just at the edge of the screen? Or do you mean you want to expand the map as it is now? I see no problem with how you're doing it right now.
Minion1 Minion1

2012/12/22

#
I mean I want to expand the map. I'll make a scrolling screen map later. All I'm asking is, is this the best map design method? It just seems so counterproductive. My Hero class (subclass of Actor) is literally holding all of the calls to create the world around him. I just always sort of expected that I could create a world and drop a hero into it, but instead, I created a hero and he is creating the world around him. It's just so backward.
darkmist255 darkmist255

2012/12/22

#
I see what you mean! I used to do this as well, it made things easier for a while! There's no problem with having your World class do the work of changing the levels! You would have to keep a reference to the player in the World class (example: Player player = p1;) and then instead of using 'getX()' you would use 'p1.getX()' and the like. This way the player is still the focus, but the World is doing the work.
danpost danpost

2012/12/22

#
The main creation of the areas should, by all rights, be in the world class (or at least the setup thereof). Either class, the world or the hero, could do the actual creation of the areas, however. Putting the code in the world class would help in keeping what is related to the hero in the hero class and what is not related to the hero, not in the hero class (this would also help in tracing problems).
Minion1 Minion1

2012/12/22

#
Yes. I see what you all mean. Good news! Thanks to you all, I've made great steps forward today! My hero is finally moving around in the world without too much trouble, and even better, I've finally got the world loading up the tiles for the trees and ponds and the like too! I've been trying to get this to happen for weeks! Now I have just one more question before I have to go. Maybe you can help with this one also: Each time the hero moves to another "area", trees and flowers and rocks are added via a "new map" text file. The problem is, those images don't go away when he moves to another area, so they just keep piling up. I'm afraid to clear the world and reconstruct it because it will clear off the hero as well, and then that will reset all his variables (health, items collected.. etc.). Is there a way to clear the world with the exception of ONE object (the hero)?
Minion1 Minion1

2012/12/22

#
Wait.. I think I realized that I could probably set up a method to clear all the tiles in the world.. and another method to clear all the enemies in the world.. and just leave the hero. Yes.. this will probably work. But I'm still interested in the question above. Is there a way to clear everything except for one specific object?
There are more replies on the next page.
1
2