Win a copy of Bad Programming Practices 101 (e-book) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Interacting with more than one entity not working  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I'm working on a 2D RPG game with some slick2d and I'm having trouble interacting with my entities.

I create my entities in my Map and render them through my EntityManager class no problem.

Now It will only let me interact with and open the first chest I render which contains the MediumGreenHealth.

To interact I check if collision is true and if Input.KEY_A is pressed.

I checked if it could still detect Input and the collision after the first chest was opened and all the other entities do but it won't open the remaining chests.

After some tests, it won't detect keyPressed anymore after being used once on the current map.

I looked at the api and tried isKeyRepeatEnabled(), isControlPressed(), enableKeyRepeat(), clearKeyPressedRecord() and others but still no success.

I feel like its something simple but I just cannot for the life of me see it. Should I use a controller? My Map and Chest classes are below:

 
Marshal
Posts: 59422
187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Short answer: don't know. Longer answer: what happens when you call that pauseXXX method? Are you blocking all your threads? Maybe you need a different thread for each entity? Not sure. Are you using the Event Dispatch Thread? Are you calling Thread#sleep() anywhere, or something similar?
 
Sheriff
Posts: 23594
48
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


It looks like the question has to be interpreted in the context of some game-writing software. I suppose it might help to know more about that software -- is it based on Swing or JavaFX or AWT or SWT, or something else?
 
Tom Hof
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using the slick2d library for setting up some basic functionality. I solved the problem which turned out to be really frustrating. The following lines of code which check if i'm pressing 'A' and collision is true it will open the chest. So it only worked for the first entity rendered on the current map. Any others after that the slick Input was not detecting my key pressed any more.

Anyways so all I did was change the if statement checks and it worked!

Instead of - if (press A && collision):



I put - if(collision && press A)




Now is this common knowledge that you if you check for multiple expressions and one of them is an input that it should come last? I always thought it didn't matter, seems the obvious logic. You're checking if both expressions are true which in my case they were but still didn't work if input was the first expression tested. Or maybe it's just an issue with the Input class from the slick2d library.

I tested the same chest opening scenario with the java event and listener

if(key 'A' pressed && collision)
if(collision && key 'A' pressed)

Both ways worked fine.

Really frustrating! Has anyone else run into this?
 
Paul Clapham
Sheriff
Posts: 23594
48
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general, A && B should be identical to B && A -- unless one of those two has a side effect which modifies the other. For example suppose that evaluating B has the side effect of making A be false the next time it's evaluated. Otherwise suppose that evaluating both A and B would produce true.

Then A && B evaluates A to produce true, and then evaluates B to produce true (but set A to be false). So the expression A && B produces true. But B && A evaluates B to produce true, and as a side effect sets A to produce false. Then evaluating A produces false and therefore the expression B && A produces false.

Now, people really frown on expressions which have side effects when they are evaluated and from what I wrote there you can see why. But it looks like that might be what is happening to you. (You could put in some debugging statements to see if I'm right.)
 
Tom Hof
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Paul Clapham

That's what I first thought too. But it didn't make sense to me because I instantiated each chest so even though I duplicated the same chest they would of had their own variables. No global variables.

I did put println statements in to see if there was conflict. I tested my boolean interact and gotItem variables and they passed. I also tested just the collision, just key pressed and both in println statements. Collision worked fine but input was playing up.

I had this test above my code:

if(key 'A' pressed)
System.out.println("A Pressed");

This test worked fine but now it now it wouldn't let me open the first chest. So basically its reading the first getInput() call and then doesn't want to read another one.

If I put the println test below the code then it will let me open the first chest only and still continue to detect keypressed and not let me open the other chests. So it's detecting im pressing 'A' but it won't let me open another chest. Doesn't make any sense.

Anyway I've spent many an hour trying to make sense of it. Maybe ill just let it be. Slick is obsolete and I won't be using it long term
 
Paul Clapham
Sheriff
Posts: 23594
48
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So it's the getInput() method which has the side effect, most likely. I guess that's not that bad from a design point of view, the getInput() method returns you the last input, but only once. Otherwise you'd have to have another method which you ran after getInput() to say that you'd used that input and you'd be pleased to receive the next input when you asked for it.

Or something like that. Anyway it sounds like you've got the problem under control now.
 
Tom Hof
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It does look like the getInput() method is the problem. I tried other methods in the Input class to reset or clear my original input: clearkeyPressed, enablerepeatKeyPressed, resetInput, isAccpetingInput.

But nothing worked. There isn't a lot of documentation for slick, I guess because it's been discontinued and not super popular.

But anyways yeah I got it working so all good. Thanks for the discussion!
 
Water! People swim in water! Even tiny ads swim in water:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!