Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (741)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (823)
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  
  cant seem to get my enemies shooting [arraylist]  (Read 1006 times)
0 Members and 1 Guest are viewing this topic.
Offline Norakomi
« Posted 2014-05-07 14:14:46 »

I cant seem to get my enemies shooting & cant figure out how I can get it working.

Currently I'm trying to have my enemies shoot whenever they hit a certain y value(=50).

I've put:
1  
private ArrayList<ProjectileEnemy> projectilesEnemies = new ArrayList<ProjectileEnemy>();

in my Enemy class' constructor to create an arraylist that keeps track of the enemy projectiles.

And i have a method that checks if the enemy needs to shoot which gets called in the Enemy class update method.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
   public void checkEnemyShoots() {

      if (enemyShoots == true) {
         ProjectileEnemy pe = new ProjectileEnemy(x, y);
         projectilesEnemies.add(pe);
         
         System.out.println("nr. of enemy projectiles(enemy)= " + projectilesEnemies.size());
         enemyShoots = false;
      }

   }


I'm checking here to see how many enemies get stored in my arraylist and the return value of .size() never exceeds 1.
If i put the system.out... code in front of my projectilesEnemies.add(pe); code it keeps returning 0 so somewhere my arraylist gets reset...

But even with a size of 1 i havent been able to put an enemyprojectile on screen, because:
1  
2  
3  
4  
   public ArrayList<ProjectileEnemy> getProjectiles() 
   {
      return projectilesEnemies;
   }

(which is in my Enemy class) just keeps returning 0.

I assume the problem here has something to do with that of every instance of my enemyclass the constructor creates a new(empty) arraylist "ProjectileEnemy" and thus erases the previous arraylist.
Although i dont understand why in between the time that the 1st & 2nd Enemy instances are created getProjectiles(); also keeps returning 0.



1  
private ArrayList<ProjectileEnemy> projectilesEnemies = new ArrayList<ProjectileEnemy>();

I also tried to put this code for the creation of this arraylist in my main game class, but also can't get this to work.

....


Am I on the right track here of thinking I have to create my ProjectileEnemy arraylist somewhere else then in my Enemy class?
And if yes: where and how?

And how come getProjectiles() keeps returning 0 even though when I check with
1  
System.out.println("nr. of enemy projectiles(enemy)= " + projectilesEnemies.size());

projectilesEnemies.size() returns 1??

Offline hwinwuzhere
« Reply #1 - Posted 2014-05-07 14:19:07 »

Am I on the right track here of thinking I have to create my ProjectileEnemy arraylist somewhere else then in my Enemy class?

Yes. I once created a game in which I put the projectile list in the mob class, but as soon as a mob got destroyed, all pending projectiles were deleted. It looked really weird.

Can you show the method that actually shoots the projectile?

There are two kinds of people in this world: Those who can extrapolate from incomplete data,
Offline Norakomi
« Reply #2 - Posted 2014-05-07 14:24:25 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
public void update() {
      y += speedY;

       System.out.println("projectileEnemy update");
      enemyProjectileRect.setRect(x, y, enemyProjectileRectX,
            enemyProjectileRectY);

      if (y < -30 || y > 800) {
         visible = false;
         // projectileRect = null;

      }

      // groter dan -30??
      if (y > -30 && y < 801) {
         checkCollision();
      }

   }


This is the update method in my ProjectileEnemy class which actually never gets reached ( System.out.println("projectileEnemy update"); isnt printing...)


In my main game class i try to update my enemyProjectiles arraylist in the same place and in the same way as i update the projectiles shot by my hero Popolon (which works fine):

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
ArrayList projectiles = popolon.getProjectiles();
            for (int i = 0; i < projectiles.size(); i++) {
               Projectile p = (Projectile) projectiles.get(i);
           
               if (p.isVisible() == true) {
                  p.update();
               } else {
                  projectiles.remove(i);
               }
            }
           
            ArrayList enemyProjectiles = enemy.getProjectiles();
            //System.out.println("KMG enemyProjectiles.size()=" +enemyProjectiles.size());
            for (int k = 0; k < enemyProjectiles.size(); k++) {
               
               
               ProjectileEnemy pe = (ProjectileEnemy) enemyProjectiles.get(k);
                     
               if (pe.isVisible() == true) {
                  pe.update();
               } else {
                  //enemyProjectiles.remove(k);
               }
            }


but I guess its not working since enemy.getProjectiles(); is 0

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline hwinwuzhere
« Reply #3 - Posted 2014-05-07 14:35:04 »

Have you checked if the projectile is visible or not? Because it won't update otherwise.

There are two kinds of people in this world: Those who can extrapolate from incomplete data,
Offline Norakomi
« Reply #4 - Posted 2014-05-07 14:39:59 »

yes i did and i also tried calling the update method outside of the if statement, but the update mehtod isnt reached.
I suppose because enemy.getProjectiles() keeps returning 0 hence there are no instances that can be called to update from my maingame class

Offline Gibbo3771

JGO Kernel


Medals: 128
Projects: 5
Exp: 1 year


Currently inactive on forums :(


« Reply #5 - Posted 2014-05-07 15:22:41 »

This is how I would do it.

Have an array of projectiles stored in a class, such as a World class. Have your enemies be constructed, pass them this projectile array.

When they fire, simply add the projectile to that array. Since objects get passed as references this works well.

Update said array in the World class.

I would not recommend having your enemies hold their own array of projectiles, if the enemy gets deleted from the world then he is no longer updating his projectiles, which can end in weird results, such as all projectiles just stopping on the spot or getting swooped up by the GC.

You could also have the Enemy class hold a static array, then in your World class just update it using Enemy.projectilesEnemies.update(). This will save you from passing the array to each enemy, in-case your code is messy and your enemies are being created all over the place (this is bad in its own right).

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline Norakomi
« Reply #6 - Posted 2014-05-07 21:22:19 »


uhhhh woops....
It was that simple?!

I simply moved my arraylist over to my main (knightmareGame-) class
1  
static ArrayList<ProjectileEnemy> projectilesEnemies = new ArrayList<ProjectileEnemy>();

(like i thought i had done before but without result) and add a projectile in my checkEnemyShoots method in my enemy class whenever an enemy shoots:
1  
KnightmareGame.projectilesEnemies.add(pe);


And now my enemies are shooting projectiles.

Thanx for you guys' help anyway. Sometimes I just makes things clearer when someone explains how he/she would do it.

Pages: [1]
  ignore  |  Print  
 
 

 
Ecumene (110 views)
2017-09-30 02:57:34

theagentd (136 views)
2017-09-26 18:23:31

cybrmynd (245 views)
2017-08-02 12:28:51

cybrmynd (241 views)
2017-08-02 12:19:43

cybrmynd (240 views)
2017-08-02 12:18:09

Sralse (254 views)
2017-07-25 17:13:48

Archive (864 views)
2017-04-27 17:45:51

buddyBro (1008 views)
2017-04-05 03:38:00

CopyableCougar4 (1568 views)
2017-03-24 15:39:42

theagentd (1373 views)
2017-03-24 15:32:08
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
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!