Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1] 2
1  Game Development / Newbie & Debugging Questions / Re: Best way to iterate through ArrayList? on: 2014-12-11 12:13:46
- be able to remove any element at random without knowing about it (not just the current one, an entity might remove another entity)

  • Your Entity class should have an extra boolean field in order to indicate it's "dead"!
  • This way, any <u>Entity</u> can "kill" another, or even itself, by setting it to true.
  • Now when iterating over a List of them, we can issue remove() upon any "dead" Entity!
2  Game Development / Newbie & Debugging Questions / Re: does return return the actual object or a copy of the object? on: 2014-11-26 19:21:29
I wanna know is there anyway that I can edit the original variable with another variable in the same way that c++ does it or should I just build setting methods.

Java creators, by purpose, have left any explicit address & indirection operators outta it! Shocked
We've only got the member reference operator, which is a simple dot!
And therefore, we can't use a variable/field to directly change the value stored in another variable/field, ever!  Lips Sealed
3  Game Development / Newbie & Debugging Questions / Re: does return return the actual object or a copy of the object? on: 2014-11-26 19:05:30
Excellent articles KevinWorkman! Now I don't have to explain this confusing but important concept by myself anymore!  Grin
However I've found out some glitches on 'em: Lips Sealed

Quote
(-2 to the 7th) through (2 to the 7th) -1. Why that little -1 on the end? Because zero is in there, and zero counts as negative.

Value 0 counts as positive in computation. After all, its sign bit is 0 too!   Roll Eyes

Quote
boolean - ... you're not supposed to ask. It holds a value of true or false, but it's really stored as a numeric value, probably in a byte-sized cup.

AFAIK, in Oracle, OpenJDK, Dalvik Java implementations, boolean primitive type is a whole 8-bit byte. 7 bits go to waste!  Cry

Quote
... remember that the object is created out on the garbage-collectible heap. Always.

That's gonna surprise many Java veterans but, since Java 6, it features an ├╝ber feature called "Escape Analysis":
https://en.wikipedia.org/wiki/Escape_analysis

Java can decide to allocate a whole object in the stack instead of regular heap if it's sure its reference won't escape/survive its local scope boundary!  Pointing
4  Game Development / Newbie & Debugging Questions / Re: does return return the actual object or a copy of the object? on: 2014-11-26 17:56:33
In the same manner Java's methods receive arguments by value, it also returns 1 as value.
Keyword return sends following evaluated expression back to its callee.  Roll Eyes
For example in return myobject; it evaluates myobject instance variable, which results in the reference value stored in it.
So it's the reference value stored in myobject which ends up being returned in that statement.   Wink
5  Game Development / Newbie & Debugging Questions / Re: ArrayList error on: 2014-11-16 04:47:12
To circumvent the issue without changing the rest of the code, use a rawtype:

That's exactly what I've told them some posts above yours!   Yawn
And btW, creating a wrapper array for it isn't the best solution, since it's another redundant & useless indirection!   Angry
Just instantiate it raw!  Tongue
6  Game Development / Newbie & Debugging Questions / Re: ArrayList error on: 2014-11-16 04:29:49
Field grid is already declared as List<Entity>[][].
Why not simply skip the generics part when instantiating it?
grid = new ArrayList[cols][rows];
What matters is that when accessed via grid, it's gonna be treated as List<Entity>[][] after all!   Roll Eyes
7  Game Development / Newbie & Debugging Questions / Re: Trouble making circular boundaries for 2D game on: 2014-11-10 23:36:58
Quote
Where possible, always compare the squares.

As @Abuse mentioned above, no need to use Math.sqrt()! Stopping at the squares is pretty enough!   Tongue

1  
2  
3  
4  
5  
6  
7  
public boolean isWithin() {
  return sq(Mouse.getX() - circle.x) + sq(Mouse.getY() - circle.y) < sq(circle.radius);
}

public static final int sq(int n) {
  return n*n;
}
8  Game Development / Newbie & Debugging Questions / Re: All about arrays! on: 2014-10-28 14:40:04
I also find Oracle's own tutorials both concise & easy:  Grin
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
9  Game Development / Newbie & Debugging Questions / Re: Best way to iterate through ArrayList? on: 2014-10-23 11:53:33
Have you already tested List's backwards loop remove() at least once:   Huh

1  
2  
3  
4  
5  
for ( int len = projectiles.size(), i = len; i-- != 0; )
  if (projectiles.get(i).removeCriteria()) {
    projectiles.set(i, projectiles.get(--len));
    projectiles.remove(len);
  }


No need to have any extra array copy or whatever!   Tongue
10  Game Development / Newbie & Debugging Questions / Re: Best way to iterate through ArrayList? on: 2014-10-17 13:09:31
What about:      for (Enemy e : getEnemies())  enemies.add(e);   Tongue
or even 133t:   enemies.addAll( java.util.Arrays.asList(getEnemies()) );
11  Game Development / Newbie & Debugging Questions / Re: Best way to iterate through ArrayList? on: 2014-10-17 13:00:15
For me it's either regular Array[], ArrayList or ArrayDeque:  persecutioncomplex
http://docs.oracle.com/javase/8/docs/api/java/util/ArrayDeque.html  
12  Game Development / Newbie & Debugging Questions / Re: Best way to iterate through ArrayList? on: 2014-10-15 16:56:55
Nah! Best quickest algorithm is this:   Tongue  

1  
2  
3  
4  
5  
for (int len = projectiles.size(), i = len; i-- != 0; )
  if (projectiles.get(i).removeCriteria()) {
    projectiles.set(i, projectiles.get(--len));
    projectiles.remove(len);
  }
13  Game Development / Newbie & Debugging Questions / Re: New Member Trying to Choose a Path on: 2014-09-25 23:40:53
Besides the aforementioned Processing: http://forum.Processing.org/two/discussions, there's Greenfoot too: http://www.Greenfoot.org/home.
14  Discussions / General Discussions / Re: Are Java applets really "dead" for indie developers? on: 2014-09-22 16:52:17
I would say GWT is equally dead :-)  It's a beast that even Google abandoned long ago.

Actually, GWT has become Dart: https://www.dartlang.org.   Wink 
15  Discussions / General Discussions / Re: Are Java applets really "dead" for indie developers? on: 2014-09-22 15:59:44
Processing also offers an approach to working in Java and deploying to JS, with 3D (WebGL) support - http://processingjs.org/

By default, it doesn't use WebGL! We've gotta pass P3D or OPENGL as the 3rd argument for size()!

16  Game Development / Newbie & Debugging Questions / Re: Did you know? Hints and tips on: 2014-07-14 22:31:24
For circle shaped objects:

1  
2  
3  
public boolean isColliding(Actor a) { // CIRCLE shapes.
  return sq(a.x - x) + sq(a.y - y) < sq(a.rad + rad);
}


1  
2  
3  
public static final double sq(double val) {
  return val*val;
}
17  Game Development / Newbie & Debugging Questions / Re: Did you know? Hints and tips on: 2014-07-14 22:23:50
Another good 1... Test for even or odd: (value & 1) == 0.
It's faster than value % 2 == 0!   Wink
18  Game Development / Newbie & Debugging Questions / Re: Did you know? Hints and tips on: 2014-07-14 22:21:39
I believe these versions are more succinct:   Roll Eyes  
P.S.: rx is w/2 & ry is h/2!

1  
2  
3  
public boolean isColliding(Actor a) { // CENTER coords.
  return x >= a.x-rx & x < a.x+rx & y >= a.y-ry & y < a.ry;
}


1  
2  
3  
public boolean isColliding(Actor a) { // CORNER coords.
  return x >= a.x & x < a.x+w & y >= a.y & y < a.y+h
}


19  Game Development / Newbie & Debugging Questions / Re: How to get the size of a primitive/object? on: 2014-07-12 21:37:20
According to the link I've provided, an object instance w/o any instance variables consumes 12 bytes.  
But due to 8 byte alignment, it jumps to 16 bytes. Also, generally we're gonna need to store its reference.
It means more 4 extra bytes. In short 16 bytes for each object at minimum + 4 bytes for each of its reference!   persecutioncomplex

1  
2  
3  
int bytes = 30;  // 12 overhead + 18 instance variables.
int align = (bytes-1)/8*8 + 8;
System.out.println(align); // 32 bytes consumed.
20  Game Development / Newbie & Debugging Questions / Re: How to get the size of a primitive/object? on: 2014-07-12 21:06:12
http://docs.Oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
http://Java-Performance.info/overview-of-memory-saving-techniques-java/
21  Game Development / Newbie & Debugging Questions / Re: For advice on where beginners should go to start making games on: 2014-07-06 19:47:43
Perhaps you should take a look at http://www.GreenFoot.org/doc or even http://Processing.org .   Smiley 
22  Game Development / Newbie & Debugging Questions / Re: [Solved] ConcurrentModificationException - How to avoid it on: 2014-06-25 20:11:11
Don't get me wrong, I love using the "enhanced" for-"each" loop version!
Even though it can be 10% slower for non-array structures than a c-style for loop! Grin
However, we can't use remove() on such loops. Either c-style or Iterator/ListIterator style for such occasions!  Roll Eyes
23  Game Development / Newbie & Debugging Questions / Re: [Solved] ConcurrentModificationException - How to avoid it on: 2014-06-25 07:36:55
I daresay that a backwards loop would still be a bit faster than an Iterator version.
B/c the condition is against a literal 0 rather than a method call like hasNext()!   persecutioncomplex
And the 2nd example using a remove()/set() combo is unbeatable! Averting left-shifting collateral is hard to ignore IMO!   Roll Eyes
On the other hand, if I was dealing w/ a structure like a LinkedList or similar, going w/ an Iterator or a ListIterator is a no-brainer!   Tongue
24  Game Development / Newbie & Debugging Questions / Re: [Solved] ConcurrentModificationException - How to avoid it on: 2014-06-24 23:46:28
I prefer this slightly more compact format using the faster overloaded index-based version of the remove() method:   Roll Eyes

1  
2  
3  
4  
5  
6  
for (int i = popups.size(); i-- != 0;) {
  PopUp p = popups.get(i);

  if (p.isDisposed())  popups.remove(i);
  else                 p.render();
}


And in the case the index order isn't important, this approach averts any left-shifting after remove(): Grin

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
for (int len = popups.size(), i = len; i-- != 0;) {
  PopUp p = popups.get(i);

  if (!p.isDisposed())  p.render();

  else {
    p = popups.remove(--len);
    if (i != len)  popups.set(i, p);
  }
}
25  Game Development / Newbie & Debugging Questions / Re: Teaching Java Game Programming to Children on: 2014-06-15 19:37:41
http://www.GreenFoot.org
https://www.KhanAcademy.org/cs
http://Hello.Processing.org
26  Game Development / Newbie & Debugging Questions / Re: Animated image that only exist for a short period of time. Garbage collection? on: 2014-05-20 01:49:50
Guess it's more specific saying assign null to every variable field or array "slot" which might happen to have a reference for the object we want to be disposed of later on!   Roll Eyes
Alternatively, rather than assign null to a variable, assign another object reference to it. The effect is the same!  Grin 
27  Game Development / Newbie & Debugging Questions / Re: [LibGDX] Removing game objects on: 2014-05-12 02:18:49
Another option would be to have a Boolean in the entity isDead. When it collides with the player, you would set this to true. When you're updating and rendering then you would just ignore the enemies that are dead.

That's my favorite strategy! Although I prefer to save the "dead" object's index value
and then re-use it when requesting a new enemy rather than instantiate another 1!  Cool  

The other option is from this recent thread:
http://www.java-gaming.org/topics/concurrentmodificationexception-when-removing-an-item-from-an-arraylist/32939/msg/309008/view.html

If the order doesn't matter, remove the latest (tail) entry and insert it in the index of the "dead" object, replacing it!   Tongue
This way, we avoid left-shifting the List's entries completely! Shocked

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
static final int BULLETS = 100;
final List<Bullet> bullets = new ArrayList<>(BULLETS);

public void displaySpritesOptimized() {
  stroke(Bullet.COLOUR);
  for (int b = bullets.size(), len = b; b-- != 0;)
    if (bullets.get(b).script()) { // returns true if requesting remove due to outta canvas!
      final Bullet u = bullets.remove(--len);
      if (b != len)  bullets.set(b, u);
      System.out.println("A bullet has strayed too far from view!");
    }
}
28  Game Development / Performance Tuning / Re: Is there a logical reason to stuff 2D array data into one? on: 2014-05-06 16:04:55
Sure you save a couple of array lookups I guess, but I don't think the ry[] would assist much with caching (it's still a pointer).

That's the point, 1 pointer rather than 2! Moreover, values which don't change later in the inner loop are cached right in the outer loop!   Tongue  
29  Game Development / Performance Tuning / Re: Is there a logical reason to stuff 2D array data into one? on: 2014-05-06 15:17:35
Still you're not taking into consideration my tiny optimizations:   Lips Sealed  

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
public static void fill_2d_random() {
  for (int y = 0; y != DIM;) {
    int yi = randX[y++], ry[] = rows[yi], x = 0;

    while (x != DIM) {
      int xi = randX[x++];
      ry[xi] = yi^xi;
    }
  }
}
30  Game Development / Performance Tuning / Re: Is there a logical reason to stuff 2D array data into one? on: 2014-05-06 11:18:23
And for the non-random 2D fill too:   Cool  

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
public static void fill_2d_YX() {
  for (int y = 0; y != DIM;) {
    int yi = linearY[y++], ry[] = rows[yi], x = 0;

    while (x != DIM) {
      int xi = linearX[x++];
      ry[xi] = yi*DIM + xi;
    }
  }
}
Pages: [1] 2
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (29 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (40 views)
2014-12-09 22:41:13

BurntPizza (76 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (50 views)
2014-12-03 16:27:13

CopyableCougar4 (47 views)
2014-11-29 21:32:03

toopeicgaming1999 (114 views)
2014-11-26 15:22:04

toopeicgaming1999 (102 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

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
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!