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)
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!
[icode]&&[/icode] 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.
For the record, [icode]&&[/icode] is not logical AND. It is the short-circuiting logical AND. Only evaluates 2nd operand if the first is true.
[icode]&[/icode] 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 [icode]&&[/icode] for logical AND it’s all good, he should look up a doc or a book anyways to fully learn how to utilize operators.
[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:
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.
Well, I was thinking more along the lines of actualling using the operator:
if (isTrue) & (isAlsoTrue){
System.out.println("This is totally true!");
}
compared to…
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.
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).
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.
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.
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.
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.
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. If I didn’t understand java, I certainly wouldn’t be bugging people about it on the forum.
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. :’(