New Posts  All Forums:Forum Nav:

Java game help.

post #1 of 6
Thread Starter 
Hey. I just started java a couple of weeks ago, and I just finished all of TheNewBoston's java tutorials. I enjoy tower defense games a lot, so I decided to attempt to make a TD game. As of now, I have a sidebar with an icon, and when the person drags the icon off of the bar, it will follow the mouse. When the person releases the mouse, the icon will disappear. I have created a "tower" object, and I want to know how i can dynamically create these objects each time I drag the icon onto the main screen.

I googled this, and I haven't found any answers. Is there a better way to do this? Like how does notch get a mini block to drop every time a block is destroyed in miecraft? Does each block use a different object? How can I accomplish this? Also, when i get to enemy spawning, I will have the same problem. I will have to be able to create the enemies without declaring an enemy object thousands of times in my code.


Tl;dr : How can I dynamically create objects in java, and if not, is there another method.


Thanks
post #2 of 6
you should be able to listen to the mouse events to detect when and where the icon is dropped to.

Just setup the event listener for that to add a new tower object to some sort of data structure and have the gui either go through that data structure to draw the tower on the screen or keep a seperate list for the objects on screen.
    
CPUMotherboardOSMonitor
2500k P8P67 Windows 7 Ultimate x64 22" phillips 
KeyboardPowerMouseMouse Pad
Cheap Logitech Antec Earthwatts 650W Razer Deathadder Razer eXactMat 
  hide details  
Reply
    
CPUMotherboardOSMonitor
2500k P8P67 Windows 7 Ultimate x64 22" phillips 
KeyboardPowerMouseMouse Pad
Cheap Logitech Antec Earthwatts 650W Razer Deathadder Razer eXactMat 
  hide details  
Reply
post #3 of 6
Thread Starter 
Quote:
Originally Posted by serge2k;13052094 
you should be able to listen to the mouse events to detect when and where the icon is dropped to.

Just setup the event listener for that to add a new tower object to some sort of data structure and have the gui either go through that data structure to draw the tower on the screen or keep a seperate list for the objects on screen.

My question is, how can i create the objects dynamically? I have searched how to do this, and I don't know how.
post #4 of 6
i think what you want is event driven programming. as serge2k was saying, you can have events happen as the result of a mouse event.

using the minecraft example, when a block is destroyed it triggers mini blocks to fall. the method that handles the block's death probably calls the constructor for the mini blocks. the mini blocks will only be created when the large block is destroyed. you would use an event to trigger other events.

hopefully this is what you meant by creating objects dynamically.
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
post #5 of 6
Thread Starter 
Quote:
Originally Posted by travesty;13066122 
i think what you want is event driven programming. as serge2k was saying, you can have events happen as the result of a mouse event.

using the minecraft example, when a block is destroyed it triggers mini blocks to fall. the method that handles the block's death probably calls the constructor for the mini blocks. the mini blocks will only be created when the large block is destroyed. you would use an event to trigger other events.

hopefully this is what you meant by creating objects dynamically.

I understood what he meant by the even triggers.

I finally made something like this to make several objects at once:
Code:
Tower1 tower1 = new Tower1(mouse.x,mouse.y);
Tower1List1.add(tower1);
It is more complicated, but whenever a specific set of inputs occurs, It will make the object, and add it to the ArrayList.

I added this to the draw method so i can draw each of the objects on the screen:
Code:
for (Tower1 tower1 : Tower1List1){
g.drawImage(tower1.getI(),tower1.getX()-25,tower1.getY()-25,null);
}

My problem, however, is how to individually refer to a tower for something like upgrading or deleting. I have a bunch of objects with the same name saved in an arraylist.
post #6 of 6
Quote:
Originally Posted by Jtvd78;13067661 
Code:
Tower1 tower1 = new Tower1(mouse.x,mouse.y);
Tower1List1.add(tower1);
It is more complicated, but whenever a specific set of inputs occurs, It will make the object, and add it to the ArrayList.

I added this to the draw method so i can draw each of the objects on the screen:
Code:
for (Tower1 tower1 : Tower1List1){
g.drawImage(tower1.getI(),tower1.getX()-25,tower1.getY()-25,null);
}

My problem, however, is how to individually refer to a tower for something like upgrading or deleting. I have a bunch of objects with the same name saved in an arraylist.

since towers have x and y coordinates that distinguish them, maybe you could use a custom defined comparator to find the tower object you want?
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming