Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
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  
  Remove Sprite and Array  (Read 927 times)
0 Members and 1 Guest are viewing this topic.
Offline monopolyli

Senior Newbie




Java games rock!


« Posted 2004-12-10 03:30:22 »

If I have an array to store the sprite in the LayerManager,
for example: c[0], c[1]................c[10],
after I remove Sprite c[2] in the LayerManager, will c[3] automatically become c[2]?
If not, how can I make c[3] to become c[2] after c[2] had been removed?

Thank you very much!!
Offline davidaprice

Junior Member





« Reply #1 - Posted 2004-12-15 10:11:58 »

The LayerManager doesn't know about your array, so removing the sprite from the LayerManager won't have any effect on your array.

If you just do 'c[2] = null;', then c[3] won't automatically become c[2], because null is a perfectly valid value for c[2].

What you could do is (where i = 2 in your case):
 System.arraycopy(c, i+1, c, i, c.length - 1 - i);
but notice that afterwards c.length won't have changed, even though the last entry is now meaningless. You'll have to keep a separate variable called 'cLength' or something and decrease it by one.

A much simpler solution is just to use a Vector instead of an array. Then, yes, if you do 'vector.removeElementAt(2);', afterwards the elements at 3 upwards are moved to being at 2 upwards. (Actually, the Vector's implementation has done a System.arraycopy just as above).
Offline pistachiomonkey

Senior Newbie




Java games rock!


« Reply #2 - Posted 2005-02-17 18:24:28 »

Quote
A much simpler solution is just to use a Vector instead of an array. Then, yes, if you do 'vector.removeElementAt(2);', afterwards the elements at 3 upwards are moved to being at 2 upwards. (Actually, the Vector's implementation has done a System.arraycopy just as above).


i am using this method as described above, but when i call remove element some of the 5 sprites (DROIDS) crash and the sprite stays on the screen!?

monkey = new Monkey(this);
         layerManager.append(monkey);

//DROIDS
         for (int i = 0; i < NUM_DROID; ++i)
         {
           try{
             System.out.println(NUM_DROID);
             Droid newDroid = new Droid(this);
             layerManager.append(newDroid);
             droid.addElement(newDroid);
           }
catch(IllegalArgumentException e)
           {System.out.println("Error" + e);
           }
         }

any ideas as to what else may be necessery to handle this implementation?

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

Junior Member





« Reply #3 - Posted 2005-02-18 05:11:50 »

Did you forget to also remove your Sprite from the LayerManager? Remember that your LayerManager doesn't know about your Vector, and your Vector doen't know about your LayerManager - it's up to you to keep them in sync.

You should remove the Sprites like this:
1  
2  
droid.removeElement(deadDroid);
layerManager.remove(deadDroid);
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.

Riven (17 views)
2014-07-29 18:09:19

Riven (12 views)
2014-07-29 18:08:52

Dwinin (11 views)
2014-07-29 10:59:34

E.R. Fleming (29 views)
2014-07-29 03:07:13

E.R. Fleming (11 views)
2014-07-29 03:06:25

pw (41 views)
2014-07-24 01:59:36

Riven (40 views)
2014-07-23 21:16:32

Riven (27 views)
2014-07-23 21:07:15

Riven (29 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!