Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (575)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Custom Prompts?  (Read 2144 times)
0 Members and 1 Guest are viewing this topic.
Offline cubemaster21
« Posted 2012-09-04 20:26:55 »

With the game that i'm currently working on, once you discover something, you have the ability to name it. I have my custom typeable text boxes working, but I don't know how to incorporate that into the Objects that I'm creating at different times as their names. How can this be accomplished? I'm really looking for a concept here on how to do this. Any help is appreciated.

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline ReBirth
« Reply #1 - Posted 2012-09-04 23:17:52 »

Quote
how to incorporate that into the Objects that I'm creating at different times as their names
Doesn't really clear here. Reflection?

Offline actual

JGO Coder


Medals: 23



« Reply #2 - Posted 2012-09-04 23:36:16 »

It's exactly clear what the problem is. Are you having trouble getting the value in your text box into an object property? Are you trying to figure out how to store the name so that it can be used later when an instance of that object comes up?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline cubemaster21
« Reply #3 - Posted 2012-09-04 23:38:49 »

Yeah, sorry if that's all a bit confusing. I need to figure out how to get the info from that prompt into the specific Object that i've created once the user hits enter.
I hope that this diagram helps

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline ReBirth
« Reply #4 - Posted 2012-09-04 23:50:06 »

So like you type "human" and all instance of Human objects show up?

Offline cubemaster21
« Reply #5 - Posted 2012-09-04 23:51:48 »

So like you type "human" and all instance of Human objects show up?
Not quite, I'd have a new object that I want to name, and on Initialization, the consructor requires a String for a name,  how do i go about taking that info from the prompt and sticking it as one of the parameters?

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline ReBirth
« Reply #6 - Posted 2012-09-04 23:55:12 »

prepare a field like name:String in your object class, then
1  
2  
3  
4  
public youObject(String name){ this.name = name; }
...
String newName = //read the input
new yourObject(newName);


Wrong again? correct me. Posting is free.

Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #7 - Posted 2012-09-04 23:56:43 »

You should probably post code that demonstrates the problem you're having, otherwise about the only useful thing we can say is 'pass the name as a parameter to the object'.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline cubemaster21
« Reply #8 - Posted 2012-09-04 23:59:43 »

You should probably post code that demonstrates the problem you're having, otherwise about the only useful thing we can say is 'pass the name as a parameter to the object'.
I don't have any code to show for this part, because i had no idea where to start.
prepare a field like name:String in your object class, then
1  
2  
3  
4  
public youObject(String name){ this.name = name; }
...
String newName = //read the input
new yourObject(newName);


Wrong again? correct me. Posting is free.

The problem with this is that I create the object before i get the name, so it can be shown to the player for them to name.
But, if I remove the name from the constructor, and create the object. Then set the name, what would be the best way for it to remember the object at hand?

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #9 - Posted 2012-09-05 00:01:11 »

I don't have any code to show for this part, because i had no idea where to start.
Post whatever you've got. We're not mind readers here.

Again, we can only offer simple advice with limited info. Want to 'remember' an object? Store it as a member variable in your gui class.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Ultroman

JGO Knight


Medals: 25
Projects: 1


Snappin' at snizzes since '83


« Reply #10 - Posted 2012-09-05 00:01:58 »

Why not just do a simple object.setName("myObjectName") after you've created it?

- Jonas
Offline Ultroman

JGO Knight


Medals: 25
Projects: 1


Snappin' at snizzes since '83


« Reply #11 - Posted 2012-09-05 00:03:53 »

Or you could store the settings or whatever as variables, and instantiate the object when you hit Enter after choosing the name.

Sorry if I'm not seeing exactly what the problem is here...kinda hard with no code

- Jonas
Offline sproingie

JGO Kernel


Medals: 202



« Reply #12 - Posted 2012-09-05 00:05:36 »

Create the object with a blank name, set it again when they enter a name in the GUI.  This is GUI 101 here, any swing tutorial that covers a textbox will cover that.  Since it sounds like you've got your own widgets going, the only one who knows how those work is you.

Offline ReBirth
« Reply #13 - Posted 2012-09-05 00:05:56 »

what would be the best way for it to remember the object at hand?
If the object amount is small, you can use HashMap<String, YourObject> or HasMap<String, List> if the name isn't unique.

Offline cubemaster21
« Reply #14 - Posted 2012-09-05 00:06:26 »

I don't have any code to show for this part, because i had no idea where to start.
Post whatever you've got. We're not mind readers here.

Again, we can only offer simple advice with limited info. Want to 'remember' an object? Store it as a member variable in your gui class.
Well, this is my GuiManager class.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
package com.toastedpixel.gui;

import java.awt.Graphics2D;
import java.util.ArrayList;

public class GuiManager {
   public enum guiState {
      NONE,
      TYPE,
      MENU;
   }
   public guiState currentState = guiState.TYPE;
   public String typeString = "";
   public ArrayList<TextBox> textBoxes = new ArrayList<TextBox>();
   public TypableTextBox typeBox = new TypableTextBox(0,0);
   public GuiManager(){
      textBoxes.add(new TextBox(2,2));
   }
   public void addTextToNewestBox(String text){
      if (!(textBoxes.get(textBoxes.size() - 1) == null))
         textBoxes.get(textBoxes.size() - 1).addLine(text, false);
   }
   public void drawAll(Graphics2D g){
      for (TextBox e:textBoxes)
         e.Draw(g);
      if (currentState == guiState.TYPE);
         typeBox.Draw(g);
   }
   public void createNewtextBox(){
      TextBox t = new TextBox(0,0);
      t.placeBelow(textBoxes.get(textBoxes.size() - 1));
      textBoxes.add(t);
   }
   public void createNewTextBoxAt(int x, int y){
      textBoxes.add(new TextBox(x, y));
   }
   public void putTypeBoxAt(int x, int y){
      typeBox.x = x;
      typeBox.y = y;
   }
   public void createTextBoxFrom(String[] args,int x ,int  y){
      TextBox t = new TextBox(x,y);
      for (String e: args){
         if (e != null)
            t.addLine(e, false);
      }
      textBoxes.add(t);
   }
   public void clearAllBoxes(){
      textBoxes.clear();
      typeBox.clearLastLine();
   }
   public void tickAll(long deltaTime){
      for(TextBox e:textBoxes)
         e.tick(deltaTime);
      if(currentState == guiState.TYPE)
         typeBox.tick(deltaTime);
   }
}

And this is my Typable text box class.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
package com.toastedpixel.gui;

import java.awt.Graphics2D;

import com.toastedpixel.andromeda.Display;


public class TypableTextBox extends TextBox{
   boolean hasFocus = true;
   public Line text = new Line(":", true);
   public String finalText;
   public TypableTextBox(int x, int y) {
      super(x, y);
     
   }
   public void tick(long deltaTime){
   text.tick(deltaTime);
      if (hasFocus){
         if(!(Display.GUI.typeString == null))
            text.setText(":" + Display.GUI.typeString);
         if(Display.input.isKeyDown(Display.input.ENTER) && text.getText().length() != 2);
            finalText = text.getText().substring(1, text.getText().length() - 1);
           
      }
   }
   public int getWidth(){
      return text.getText().length() * 6 + 10;
   }
   public void Draw(Graphics2D g){
      g.drawImage(Font.getTextBG(),x, y, getWidth(), 9,null);
      int x1 = x + 2;
      int y1 = y+2;
      Font.RenderText(g, text.getText(), x1, y1);
   }

}

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline cubemaster21
« Reply #15 - Posted 2012-09-05 00:07:41 »

Create the object with a blank name, set it again when they enter a name in the GUI.  This is GUI 101 here, any swing tutorial that covers a textbox will cover that.  Since it sounds like you've got your own widgets going, the only one who knows how those work is you.


Yeah, I haven't taken any classes on java, nor have I practiced a lot int Swing. But, yeah these are custom widgets

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline Ultroman

JGO Knight


Medals: 25
Projects: 1


Snappin' at snizzes since '83


« Reply #16 - Posted 2012-09-05 00:15:39 »

Yeah, like sproingie said. Create the object with a blank name-string (remove it from the constructor).
Then when the user hits Enter (that closes your box, right?), then before you destroy the box, do:
1  
myNewObject.setName(myBox.getFinalString())

Obviously this is pseudo-code, but you should get the picture.

If you need the constructor to be able to set the name with a parameter for creating other objects, just overload the constructor, like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
String name = "", information = "";
int type;

public MyObject(String name, String information, int type){
    this.name = name;
    this.information= information;
    this.type= type;
}

public MyObject(String information, int type){
    this.information= information;
    this.type= type;
}

- Jonas
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #17 - Posted 2012-09-05 00:17:41 »

Ok, so you have a text box but no way of tying in custom logic to the 'enter' press. The obvious approach would be to use a listener/observer pattern: http://en.wikipedia.org/wiki/Observer_pattern

In short, whatever created the text box would register a listener and be notified when the user pressed enter. You should probably take a look at how swing handles this (see http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/event/ActionListener.html ) which should make things clearer.

Reading a few swing tutorials to get a grasp on how it handles this kind of thing would probably be helpful if you're trying to write your own gui system. Swing may not be perfect, but it gets a lot of the basics right.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline cubemaster21
« Reply #18 - Posted 2012-09-05 02:42:02 »

So what i've gathered from this and a tutorial or two, is that I have to
1:create a listener from the new Object that I want to name
2:create a textbox for that name
3:fire an event when the enter key is pressed
4:have the listener deal with the event
5:the listener would store the text to the object's name?
 if that's the basic concept, then i have that much(apart from some errors that i don't understand). Or I'm completely wrong....

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (35 views)
2014-10-17 03:59:02

Norakomi (26 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (26 views)
2014-10-15 16:18:58

TehJavaDev (50 views)
2014-10-14 00:39:48

TehJavaDev (50 views)
2014-10-14 00:35:47

TehJavaDev (40 views)
2014-10-14 00:32:37

BurntPizza (63 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (75 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!