Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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  
  Problem with detecting 2 different things!  (Read 1013 times)
0 Members and 1 Guest are viewing this topic.
Offline syszee

Junior Member


Medals: 1



« Posted 2014-06-20 21:35:00 »

Hi there,

I'm having the worst luck coming up with a system for this project. Basically, I need a simply algorithm that detects if player1 is in a door, and player2 is in a door.. then proceed to next level. Else, if player1 is in the door but player 2 isn't, then don't go. (& vise versa).

I've tried many things. Something simple please!

Thanks!
- A


(Yes, this is like beginner, I don't know why I can't get it)
Offline Longarmx
« Reply #1 - Posted 2014-06-20 21:58:25 »

&&
?

I don't understand what you're having trouble with. You just have to make exactly what you described:

1  
2  
3  
4  
if(player1.inDoor() && player2.inDoor())
{
     nextLevel();
}

Offline syszee

Junior Member


Medals: 1



« Reply #2 - Posted 2014-06-20 22:00:39 »

I know, I don't understand it either! It is really weird because I've tried many things and it just keeps failing. Let me give this a try though. Thank you!

Edit: It worked! I don't understand why this worked and nothing else I tried did! Thank you though! Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline PandaMoniumHUN

JGO Coder


Medals: 31
Exp: 3 years


White-bearded OGL wizard


« Reply #3 - Posted 2014-06-20 22:20:06 »

Edit: It worked! I don't understand why this worked and nothing else I tried did! Thank you though! Smiley
&&
is a logical AND operator, it only proceeds to the if block if both conditions around it are true. No offense, but you should learn the basics of programming before you dive into such a deep topic as game development.

My Blog | Jumpbutton Studio - INOP Programmer
Can't stress enough: Don't start game development until you haven't got the basics of programming down! Pointing
Online BurntPizza
« Reply #4 - Posted 2014-06-20 23:16:59 »

For the record,
&&
is not logical AND. It is the short-circuiting logical AND. Only evaluates 2nd operand if the first is true.
&
is the logical AND as well as the bitwise AND.

Offline PandaMoniumHUN

JGO Coder


Medals: 31
Exp: 3 years


White-bearded OGL wizard


« Reply #5 - Posted 2014-06-21 08:03:07 »

For the record,
&&
is not logical AND. It is the short-circuiting logical AND. Only evaluates 2nd operand if the first is true.
&
is the logical AND as well as the bitwise AND.

That's true, but I didn't want him to be confused by short-circuiting/not short-circuiting thing. If he knows that he can use
&&
for logical AND it's all good, he should look up a doc or a book anyways to fully learn how to utilize operators. Smiley

My Blog | Jumpbutton Studio - INOP Programmer
Can't stress enough: Don't start game development until you haven't got the basics of programming down! Pointing
Offline Rayvolution

JGO Kernel


Medals: 197
Projects: 2
Exp: 1 year


Resident Crazyman


« Reply #6 - Posted 2014-06-21 08:49:12 »

For the record,
&&
is not logical AND. It is the short-circuiting logical AND. Only evaluates 2nd operand if the first is true.
&
is the logical AND as well as the bitwise AND.

[slightDerail]
What purpose would you have to use a logical/bitwise &? I mean, if & checks both(or all) conditions, but && checks them 1 at a time, and gives up when it fails, how is that functionally different to your program? I can't think of a reason why you would want to continue checking the rest of the conditions if even one failed, because if one failed the if statement will fail anyway? Kinda seems like a waste of processing.

Having said that, that means I guess the most logical way to order your IF (condition && condition) statements is to have your least CPU intensive check first, and most last.
[/slightDerail]

- Raymond "Rayvolution" Doerr.
Retro-Pixel Castles - Survival Sim/Builder/Roguelike!
LIVE-STREAMING DEVELOPMENT: http://www.twitch.tv/SG_Rayvolution
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #7 - Posted 2014-06-21 10:13:02 »

[slightDerail]
What purpose would you have to use a logical/bitwise &? I mean, if & checks both(or all) conditions, but && checks them 1 at a time, and gives up when it fails, how is that functionally different to your program? I can't think of a reason why you would want to continue checking the rest of the conditions if even one failed, because if one failed the if statement will fail anyway? Kinda seems like a waste of processing.

Having said that, that means I guess the most logical way to order your IF (condition && condition) statements is to have your least CPU intensive check first, and most last.
[/slightDerail]

If all of the things being checked are boolean values then they're functionally identical. You'll get differences if the blocks are function calls instead. Consider:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
class Player
{
  int x, y;
  int vx, vy;
 
  boolean moveAndCheckIfDoor()
  {
    x += vx;
    y += vy;
    return x == 0;
  }
}

// elsewhere
public void update()
{
  if (player1.moveAndCheckIfDoor() && player2.moveAndCheckIfDoor())
  {
    // do door stuff
 }
}


Here because moveAndCheckDoor() has side effects, you'll get different behaviour depending on whether short circuiting takes place or not.

Although personally I'd strongly suggest *not* writing code like this, sometimes this kind of behaviour can creep in more subtly without you noticing.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Online BurntPizza
« Reply #8 - Posted 2014-06-21 11:16:37 »

Yeah precisely, side effects. Even simple stuff like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
if (a > 0 && ++b > 0) {
    ...
}

// can be re-written as

if (a > 0) {
    b++;
    if (b > 0) {
        ...
    }
}


Of course you shouldn't do that, but it's there.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 797
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #9 - Posted 2014-06-21 11:28:40 »

Those two pieces of code are not identical.

Ah, you corrected it.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online BurntPizza
« Reply #10 - Posted 2014-06-21 11:30:43 »

Those two pieces of code are not identical.

Ah, you corrected it.

Yeah, actually got it wrong twice...  persecutioncomplex

See kids, don't do that!
Offline Rayvolution

JGO Kernel


Medals: 197
Projects: 2
Exp: 1 year


Resident Crazyman


« Reply #11 - Posted 2014-06-21 17:49:07 »

Well, I was thinking more along the lines of actualling using the operator:

1  
2  
3  
if (isTrue) & (isAlsoTrue){
      System.out.println("This is totally true!");
}


compared to..

1  
2  
3  
if (isTrue) && (isAlsoTrue){
      System.out.println("This is totally true!");
}


In pretty much all situations using & and && would be identical behavior in the end, wouldn't it? The only difference is & would check if isAlsoTrue == true, even though isTrue came back false.


- Raymond "Rayvolution" Doerr.
Retro-Pixel Castles - Survival Sim/Builder/Roguelike!
LIVE-STREAMING DEVELOPMENT: http://www.twitch.tv/SG_Rayvolution
Online BurntPizza
« Reply #12 - Posted 2014-06-21 20:33:17 »

Yeah, they are semantically identical when the conditions are pure (have no side effects).
However they are not with impure conditions, which can certainly happen. An important distinction to be aware of IMO, could leave someone unaware completely stumped on a bug otherwise.

That said I rarely ever use the non-short circuiting operator (OR or AND).
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 797
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #13 - Posted 2014-06-21 20:40:01 »

I've had collegues that wrote code like this:
1  
2  
3  
public static boolean update() {
   return (selectX() || selectY()) && updateZ();
}
where updateZ was actually meant to only run if selectX and/or selectY returned true.

Sadly, the code was way more complex: a one-liner of a few hundred characters with a dozen method calls all returning booleans and almost all having side effects, were each was supposed to be executed in very specific conditions. Obviously it was broken.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Online BurntPizza
« Reply #14 - Posted 2014-06-21 20:49:21 »

That sucks.

I say if you want to have a function like that be called only after a condition, it should be done explicitly, i.e. in an if block. Makes it clear what the code does with only a cursory glance. Sure it's more verbose, but that's a feature in this scenario.
Offline Rayvolution

JGO Kernel


Medals: 197
Projects: 2
Exp: 1 year


Resident Crazyman


« Reply #15 - Posted 2014-06-21 21:02:01 »

Yeah, they are semantically identical when the conditions are pure (have no side effects).
However they are not with impure conditions, which can certainly happen. An important distinction to be aware of IMO, could leave someone unaware completely stumped on a bug otherwise.

That said I rarely ever use the non-short circuiting operator (OR or AND).

Yeah, I never use them either. I never ran into a situation where I wanted/needed to. That's the main reason why I was asking, maybe it was more useful than I realized. Tongue

- Raymond "Rayvolution" Doerr.
Retro-Pixel Castles - Survival Sim/Builder/Roguelike!
LIVE-STREAMING DEVELOPMENT: http://www.twitch.tv/SG_Rayvolution
Online BurntPizza
« Reply #16 - Posted 2014-06-21 21:03:58 »

Yeah it's really only useful to guarantee side effects, and if you find yourself in that scenario, you should just invoke them explicitly. Much cleaner IMO.
Code clean, not clever.
Offline Herjan
« Reply #17 - Posted 2014-06-21 21:50:52 »

Code clean, not clever.

Its amazing what people do when they are bored (fan art Wink):


Offline syszee

Junior Member


Medals: 1



« Reply #18 - Posted 2014-06-22 00:04:28 »

Edit: It worked! I don't understand why this worked and nothing else I tried did! Thank you though! Smiley
&&
is a logical AND operator, it only proceeds to the if block if both conditions around it are true. No offense, but you should learn the basics of programming before you dive into such a deep topic as game development.

No offense, but I know what && is. Lol, I was using it before I created the topic. I don't understand why this worked actually, I believe it may have just been a bug. The && wasn't at all the fix.
Creating the nextLevel(); method was generally how I got it working. Wink If I didn't understand java, I certainly wouldn't be bugging people about it on the forum. Cheesy
Offline ctomni231

JGO Wizard


Medals: 99
Projects: 1
Exp: 7 years


Not a glitch. Just have a lil' pixelexia...


« Reply #19 - Posted 2014-06-22 02:00:08 »

Creating the nextLevel(); method was generally how I got it working. Wink If I didn't understand java, I certainly wouldn't be bugging people about it on the forum. Cheesy

Yeah... maybe it isn't understanding Java that is the issue here, maybe it is learning how it actually applies to 2D game programming. Try reading this...

Regardless of what you know, there is a disconnect somewhere. But, just as well, gaming is one of the hardest parts of developing. Researching deeply into the topics, and looking at how other games do things, is the answer to solving most problems you have.

Honestly, it isn't that you don't know Java, I think that the issue is that enough time in researching the topics isn't spent.  Cry


Pages: [1]
  ignore  |  Print  
 
 

 

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

The first screenshot will be displayed as a thumbnail.

Dwinin (23 views)
2014-09-12 09:08:26

Norakomi (56 views)
2014-09-10 13:57:51

TehJavaDev (69 views)
2014-09-10 06:39:09

Tekkerue (34 views)
2014-09-09 02:24:56

mitcheeb (56 views)
2014-09-08 06:06:29

BurntPizza (40 views)
2014-09-07 01:13:42

Longarmx (25 views)
2014-09-07 01:12:14

Longarmx (31 views)
2014-09-07 01:11:22

Longarmx (31 views)
2014-09-07 01:10:19

mitcheeb (38 views)
2014-09-04 23:08:59
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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!