Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (767)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (854)
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  
  Time interval between movement/shooting  (Read 1551 times)
0 Members and 1 Guest are viewing this topic.
Offline GustavXIII

Junior Devvie


Projects: 1



« Posted 2012-02-28 22:33:45 »

Hey!
So my enemys can move and they can shoot. But they do it on the same time.
Do you know how I could do it that there is a small delay between them?

Here you can watch:
http://www.youtube.com/watch?v=WnWBIQppWUo

This is my method for the enemys to shoot:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
// Time Interval
      if(enemyCount>125)
      {
          for(int enemys = 0;enemys < enemyContainer.size(); enemys++) // Go throught all enemys
          {
             for(int bullets = 0;bullets < 5;bullets++) // Shoot 5 Bullets
             {
                 projectile = new Projectile(enemyContainer.get(enemys).x - (bullets*30),enemyContainer.get(enemys).y + 13,sprites.projectile);  // Create new Projectile
                 enemyBullets.add(projectile);  // Add the bullet the Container
             }
           
          }
          enemyCount=0;  
      }
      else enemyCount++;
Offline H3rnst

Senior Newbie


Medals: 1



« Reply #1 - Posted 2012-02-28 23:36:13 »

Ok, I think this is probably the crappiest, less cpu-friendly solution ever. I'm still a n00b and this was the first thing that came to my mind:


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  
//Code...

int randomness = 30; //If you increase this number, the enemies will shoot less times

Random ran = new Random(); //Creates a new random number generator

//Code...

// Time Interval
      if(enemyCount>125)
      {
          for(int enemys = 0;enemys < enemyContainer.size(); enemys++) // Go throught all enemys
          {
              if(ran.nextInt(randomness) == 0) //This means that this enemy will not shoot unless the random number equals 0 (or whatever value you want)    
              {
                 for(int bullets = 0;bullets < 5;bullets++) // Shoot 5 Bullets
                 {
                     projectile = new Projectile(enemyContainer.get(enemys).x - (bullets*30),enemyContainer.get(enemys).y + 13,sprites.projectile);  // Create new Projectile
                     enemyBullets.add(projectile);  // Add the bullet the Container
                 }
              }
          }
          enemyCount=0;  
      }
      else enemyCount++;

Offline ra4king

JGO Kernel


Medals: 508
Projects: 3
Exp: 5 years


I'm the King!


« Reply #2 - Posted 2012-02-29 05:09:20 »

Use H3rnst's code for random shooting ("== 0" is probably extremely little, you should make it like "< 5" at least).

It is also best to handle shooting inside the Enemy class instead and in the constructor, create a random offset:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
private long tickCount;

public Enemy(....) {
    ....
   
    tickCount = (long)(Math.random() * 126); //0 - 125
}

...

public void update() {
    if(tickCount > 125) {
        for(int bullets = 0; bullets < 5; bullets++)
            getParent().getEnemyBulletList.add(new Projectile(....));
       
        tickCount = 0;
    }
    else
        tickCount++;
}

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline GustavXIII

Junior Devvie


Projects: 1



« Reply #3 - Posted 2012-02-29 10:52:52 »

Great! Thank you it work fine.  Wink
Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (1256 views)
2018-06-10 19:43:48

EgonOlsen (1124 views)
2018-06-10 19:43:44

EgonOlsen (862 views)
2018-06-10 19:43:20

DesertCoockie (1271 views)
2018-05-13 18:23:11

nelsongames (1104 views)
2018-04-24 18:15:36

nelsongames (1335 views)
2018-04-24 18:14:32

ivj94 (2073 views)
2018-03-24 14:47:39

ivj94 (1226 views)
2018-03-24 14:46:31

ivj94 (2169 views)
2018-03-24 14:43:53

Solater (792 views)
2018-03-17 05:04:08
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
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!