943,696 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 816
  • Java RSS
May 17th, 2008
0

In-Game Level Editor Help

Expand Post »
I'm trying to make a level that can be edited in game for a pong-like game.
So far, this is what I have:

Java Syntax (Toggle Plain Text)
  1. import java.awt.*;
  2. import java.applet.*;
  3.  
  4. public class LevelEditor extends Board
  5. {
  6. public static final String row1 = "::::gggggggggggggggggg";
  7. public static String row2 = "::::g::::::::::::::::g";
  8. public static String row3 = "::::::::::::::::::::::";
  9. public static String row4 = "::::::::::::::::::::::";
  10. public static String row5 = "::::::::::::::::::::::";
  11. public static String row6 = "::::::::::::::::::::::";
  12. public static String row7 = "::::::::::::::::::::::";
  13. public static String row8 = "::::::::::::::::::::::";
  14. public static String row9 = "::::g::::::::::::::::g";
  15. public static final String row10 = "::::gggggggggggggggggg";
  16. String name = "YOUR LEVEL";
  17. String[] definitions = {row1, row2, row3, row4, row5, row6, row7, row8, row9, row10};
  18. public LevelEditor(Component parent, Applet applet)
  19. {
  20. super(parent, applet);
  21. super.initializeBoard(definitions);
  22. super.setDefinitions(definitions);
  23. super.setName(name);
  24. }
  25. public void resetLevel(String [] definitions)
  26. {
  27. super.initializeBoard(definitions);
  28. super.setDefinitions(definitions);
  29. super.setName(name);
  30. }
  31. }

And in the Board class, the parts you might need to know:

Java Syntax (Toggle Plain Text)
  1. import java.applet.*;
  2. import java.awt.*;
  3.  
  4. public abstract class Board
  5. {
  6. private Elements elementArray [][];
  7. private Component parent;
  8. private Applet applet;
  9. private String[] definitions;
  10. private String name;
  11. public Board(Component parent, Applet applet)
  12. {
  13. this.parent = parent;
  14. this.applet = applet;
  15. }
  16. public void setDefinitions(String [] definitions)
  17. {
  18. this.definitions = definitions;
  19. }
  20. public void setName(String name)
  21. {
  22. this.name = name;
  23. }
  24. public void initializeBoard(String [] definitions)
  25. {
  26. elementArray = new Elements[StaticPong.NUMBEROFLINES][StaticPong.NUMBEROFCOLUMNS];
  27. for (int j=0; j<StaticPong.NUMBEROFLINES; j++) {
  28. char [] elements = definitions[j].toCharArray();
  29. for (int i=0; i<StaticPong.NUMBEROFCOLUMNS; i++) {
  30. if (elements[i] == ':') {
  31. elementArray[j][i] = null;
  32. }
  33. else if (elements[i] == 'g') {
  34. Walls element = new Walls(Color.black, 1);
  35. elementArray[j][i] = element;
  36. }
  37. else if (elements[i] == 'p') {
  38. BWalls element = new BWalls(Color.blue, 2);
  39. elementArray[j][i] = element;
  40. }
  41. else if (elements[i] == 'e') {
  42. EWalls element = new EWalls(Color.red, 3);
  43. elementArray[j][i] = element;
  44. }
  45. else if (elements[i] == 'i') {
  46. Walls element = new Walls(Color.gray, 1);
  47. elementArray[j][i] = element;
  48. }
  49. else if (elements[i] == 'm') {
  50. MWalls element = new MWalls(Color.yellow, 4, 3);
  51. elementArray[j][i] = element;
  52. }
  53. else if (elements[i] == '-') {
  54. Path element = new Path(Color.gray, 5);
  55. elementArray[j][i] = element;
  56. }
  57. }
  58. }
  59. }
  60. public void editLevel(int x, int y)
  61. {
  62. int row = x/StaticPong.NUMBEROFCOLUMNS;
  63. int col = y/StaticPong.NUMBEROFLINES;
  64. if (row!=1 && row!=10) {
  65. char[] line = definitions[row].toCharArray();
  66. if (line[col] == ':') {
  67. line[col] = 'g';
  68. }
  69. else if (line[col] == 'g') {
  70. line[col] = 'p';
  71. }
  72. else if (line[col] == 'p') {
  73. line[col] = 'e';
  74. }
  75. else if (line[col] == 'e') {
  76. line[col] = ':';
  77. }
  78. definitions[row] = line.toString();
  79. }
  80. resetLevel(definitions);
  81. }

The editlevel method is called in Board when you click on a grid box in "leveleditor" mode (a gamestate).
This should change the row/col that you clicked on to a different element, but the applet freezes when you click on something.

Any help making the editor work?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BBaller1211 is offline Offline
7 posts
since May 2008
May 20th, 2008
0

Re: In-Game Level Editor Help

Try debugging !
Code isn't complete ? where is StaticPong?
Do simple step by step debugging using some debugger like Eclipse. Put a break point in the handler.
Otherwise do it old style, put some traces in your handler function and the editxx() function to see where it is hung.
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Problem with slider (jQuery) containing scrool bar in Firefox
Next Thread in Java Forum Timeline: detect when the song has finished playing (JMF)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC