Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (531)
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  
  [Solved]Block Collision  (Read 677 times)
0 Members and 1 Guest are viewing this topic.
Offline sugarrushx6

Senior Newbie





« Posted 2013-03-01 00:32:52 »

I already know the problem, but I don't know how to fix it.

So I have code, like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
for (int i = 0; i < units.size(); i++) {
 Unit temp = units.get(i);
  for (int i2 = 0; i2 < blocks.size(); i2++) {
   if (temp.collisionCheck(temp2))
    temp.setFalling(false);
   else (!temp.collisionCheck(temp2))
    temp.setFalling(true);
  }
}


And if the player collides with a block, it would stop falling, and vice versa.
But, since it checks with each block,
it would be true that it collided with one block, but since it didn't collide with the rest,
it would start falling again.

It's very hard for me to solve this problem,
could somebody help?  Smiley
If you need more source code, I'll provide a link.
And this is not the same problem as the "Rectangle.intersects" problem I had.  Wink

Spoiler
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #1 - Posted 2013-03-01 00:41:29 »

1  
break;

break the loop when you find the block. Read up on java keywords.
Offline ReBirth
« Reply #2 - Posted 2013-03-01 03:14:19 »

Some flaws,

a) I see no temp2 assignment
b) the second IF is useless - remember what ELSE means
c) setting a boolean variable by IF-ELSE can be simplified like
1  
temp.setFalling(!temp.collisionCheck(temp2));

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

Senior Newbie





« Reply #3 - Posted 2013-03-01 22:11:27 »

Some flaws,

a) I see no temp2 assignment
b) the second IF is useless - remember what ELSE means
c) setting a boolean variable by IF-ELSE can be simplified like
1  
temp.setFalling(!temp.collisionCheck(temp2));

I didn't copy and paste the code, so instead I typed it quick:
a) it was to be:
1  
Block temp2 = (Block) blocks.get();

b) it was to be:
1  
if else(blah)

c) Thank you
1  
break;

break the loop when you find the block. Read up on java keywords.
I thought break only applied to while loops, and switch.
And also, since that code is in a while loop, I thought that it would break that loop with the for loop.
Anyways, thanks for the solution.

Spoiler
Offline Sammidysam
« Reply #4 - Posted 2013-03-01 22:21:05 »

1  
if else(blah)


is not valid syntax.  I believe you are looking for

1  
else if(blah)


but even with that your statement doesn't need if after the else.  Take this example:

1  
2  
3  
4  
5  
boolean blah = true;
if(blah)
    System.out.println("Blah!");
else
    System.out.println("this is actually important...");


The else block will be run if blah equals false.  Putting in an if after that is useless unless there are other variables involved.  Also, break is used in for loops too.
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #5 - Posted 2013-03-02 00:16:20 »

@sugarrush break can be used with for loops also. If you have nested loops (a loop in a loop) you can break them by naming the loops and then breaking that (for example naming your outer loop 'out' so then you could do break 'out'Wink
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.

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

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

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

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

ctomni231 (43 views)
2014-07-18 06:55:21

Zero Volt (39 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (35 views)
2014-07-16 23:30:00

Cero (50 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
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!