Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (521)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (589)
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  
  Random positioning at random times help  (Read 668 times)
0 Members and 1 Guest are viewing this topic.
Offline Huw

Senior Devvie


Medals: 1
Projects: 2



« Posted 2012-10-05 18:12:02 »

Hi, I have a small game in which the player has to shoot targets which pop up in the windows of a house. So far, I have all the shooting etc working, but I have written the code myself for when and where the targets are being placed. I only have 4 targets which pop up at the moment, so obviously its not a very long game and I know there must be a much easier and faster way than typing out loads of code for everytime a target comes up.

So what I'm asking is if anyone could help me by showing me how I could randomly select a position from 2 arrays(one for both x and y positions) which I can then use to draw Images from.
This may seem obvious, but I'm still new to java and I'm not very good with arrays and such.
Thanks in advance to anyone who can help me, also if you need any more information I can tell you.

Source:
http://pastebin.java-gaming.org/2d782976c23

I'm a java noob, but I'm learning. My little blog. My Games.
Offline teletubo
« League of Dukes »

JGO Ninja


Medals: 48
Projects: 4
Exp: 8 years



« Reply #1 - Posted 2012-10-05 18:19:23 »

You should read about arrays and for loops. You shouldn't be doing this:

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  
     if(target1){
         if(time/200>=2){
            targ1x=125;
            targ1y=150;
            target50.draw(targ1x, targ1y);
         }
      }
      if(target2){
         if(time/20>=35){
            targ2x = 275;
            targ2y = 500;
            target50.draw(targ2x, targ2y);
         }
      }
      if(target3){
         if(time/20>=43){
            btargx=310;
            btargy=125;
            target.draw(btargx,btargy);
         }
      }
      if(target4){
         
         if(time/20>=60){
            targ1x = 65;
            targ1y = 450;
            target50.draw(targ1x, targ1y);
         }


You should be doing something like :

1  
2  
3  
4  
5  
6  
7  
8  
9  
         if(time/20>=60){

         for (int i=0;i<NUM ;i++) /* this will repeat NUM times for all your targets */
              {    
               targx[i] = 65;
               targy[i] = 450;
               targy[i].draw(  targx[i], targy[i]);
               }
         }


This is still not the best way, since X and Y should be members of class, but let's leave as it is for now.
Read about what I told you and you'll dramatically reduce your code.

Offline Huw

Senior Devvie


Medals: 1
Projects: 2



« Reply #2 - Posted 2012-10-05 18:25:54 »

I'll read about them now, I've taught myself from the internet so I don't know loads about the most efficient ways of doing things.

Also, for the code you posted, would that work for different times too?

I'm a java noob, but I'm learning. My little blog. My Games.
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.

xFryIx (55 views)
2014-11-13 12:34:49

digdugdiggy (34 views)
2014-11-12 21:11:50

digdugdiggy (29 views)
2014-11-12 21:10:15

digdugdiggy (23 views)
2014-11-12 21:09:33

kovacsa (46 views)
2014-11-07 19:57:14

TehJavaDev (50 views)
2014-11-03 22:04:50

BurntPizza (49 views)
2014-11-03 18:54:52

moogie (65 views)
2014-11-03 06:22:04

CopyableCougar4 (63 views)
2014-11-01 23:36:41

DarkCart (148 views)
2014-11-01 14:51:03
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!