what is stopping my screen from rendering as planned? - java

#Override
public void render(float delta) {
Gdx.gl.glClearColor(0,0,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
combattext.setText(combatstring);
stage.act();
stage.draw();
}
combatstring is a string that displays what happened in my game. when enemies attacks, it says what happened. when I attack, it says what happens. As of now my game has a loop when in combat that runs a turncounting instance. When it's time for a turn, it runs that participants turn, and when their turn is over it updates combatstring.
I have 2 combatstrings. One is for my characters, and one is for enemies. Here is an example of what happens when it's an enemies turn.
if(thisencounter.monster1turn){ //monster1's turn goes in here
thiscombat.getTarget(1); //get enemy's target
thiscombat.calculateVars(1); //calculate all combat variables for
// this turn
thiscombat.theyAttack();
combatstring = thisencounter.thisenemy.species+
" "+thiscombat.action+
" "+thiscombat.theirtarget.species+
" for "+thiscombat.effect;
thisencounter.monster1turn=false;
}
This works fine, my combattext label updates instantly when a monster goes.
For my characters, their turns make buttons visible on my screen. The buttons have listeners that determine actions, here is an example:
monster1.addListener(new ClickListener(){ //makes the first enemy's
//image clickable, but only
//when the attack button
//has been clicked
#Override
public void clicked(InputEvent event, float x, float y) {
thiscombat.ChooseTarget(1); //various methods that calculate damage,
//output what happened, and put us back in
thiscombat.calculateVars(4); //the loop to wait for next
//participant's turn
thiscombat.youAttack();
combatstring = thisencounter.thisspirit.species+
" "+thiscombat.action+
" "+thiscombat.yourtarget.species+
" for "+thiscombat.effect;
monster1.clear();
monster2.clear();
monster3.clear();
try {
combatloop();
} catch (InterruptedException ex) {
Logger.getLogger(map1field.class.getName()).
log(Level.SEVERE, null, ex);
}
}});
This also works fine, EXCEPT on the turn just before a monster goes. Since there is no wait between my turn and the monster's turn, the combattext just instantly displays what the monster does.
I assumed 'oh hey this is because there is no delay between the two turns'. So, I threw a thread.sleep(2000) just before where it updates the monsters combatstring.
But that does not fix it. It still does not show the text for my character that goes just before a monster. Also, since fighting starts right when you go to this screen in the game, it waits about 2 seconds before it displays anything on the screen.
For the life of me I don't understand why it behaves like this. Can someone explain what is happening to me? Is it because it isn't rendering before it gets to thread.sleep ?
If that's the case, how do I force it to render before it does thread.sleep, or what should I be using instead of thread.sleep ?

Related

JAVA/Swing (Eclipse) : Program works step by step but not in normal execution

I have a job to do for school. There is a window where there are pigeons, and by clicking the mouse we place some food that pigeons have to eat. To make it simple, I use swing to paint some filled round for food and filled square for pigeons. Pigeons and Foods are Threads that have a PigeonShape or FoodShape object that are JPanels.
When I place my food, pigeons move in the right direction. The problem is that, because I use swing, I have to repaint the PigeonShape each time I change its positions. Here is the code in Pigeon.java :
The run() :
public void run(){
while(true){
try {
sleep(25);
this.observe(); //just to check what is the closest piece of Food, we don't care here
this.move();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
And the move() :
private void move(){
//If there is available food, the Pigeon has to move to it
if(this.closest != null){
/*
* some calculations for the move
* coordonates are stored in xPigeon et yPigeon that are integers
*/
//it makes the PigeonShape disappear because the background is white too
this.draw.setColor(Color.WHITE); //(1)
this.draw.repaint(); //(2)
//it changes coordinates of the Pigeon
//each time those functions are called, it call the same function for this.draw (the PigeonShape)
this.setPosX(xPigeon);
this.setPosY(yPigeon);
//it makes the PigeonShape reappear
this.draw.setColor(CURIOUS); //(3)
this.draw.repaint(); //(4)
}
}
(In the above code, this.draw is the object PigeonShape that represents this Pigeon, and CURIOUSis a private static Color equal to Color.GREEN)
The problem is that in normal execution, the PigeonShape moves, lines (3) et (4) are done but not (1) and (2). In the window, it keeps previous positions the Pigeon had :
(when the PigeonShape is green, it meens that it is curious, and when it is blue, it is asleep)
But when I debug step by step, lines (1) and (2) are correctly done and I don't have the trail of the move !
I don't understand why it works when debugging step by step but not in normal execution... Could you help me please ? Thank you !
it keeps previous positions the Pigeon had :
You need to invoke super.paintComponent(...) as the first statement when you override the paintComponent(...) method. This will clear the background before doing the custom painting.

How to drawString() only once

I have a program where the user is traveling in a grid and has to kill an enemy. When they do so, a message comes up stating that the user has heard a scream.
This message is only supposed to display once when the user fires their weapon. I made the message display if the following method was true:
public boolean deadWumpus()
{
for(int x=0;x<10;x++)
{
for(int y=0;y<10;y++)
{
if(map.grid[x][y].getDeadWumpus()==true)
return true;
}
}
return false;
}
I then have a line in my paint() method which states that if this method is true then display the message. However, this keeps on displaying as there is no regulator to tell it to run only once when the user fires.
I attempted to create an int to ensure it only runs once:
// at beginning of program
int once=0;
//in paint()
if(once==0){
if(deadWumpus()==true)
{
g.drawString("You hear a scream.",300,675);
}
}
once++;
Using this it never displays the message. Is there a way I can get the message to only display once when the user shoots and then disappear once the user makes their next move?
Thanks.
The paint() method is called every time a frame of your game is drawn on the screen. When you make it drawn "only once," you made it literally draw "only once," as in, for only one frame, since frames are being update really fast, the text flashes and then never shows up again. I recommend having something like this:
long userFired;
// when the user fires -> userFired = System.currentTimeMillis();
paint() { /*the "2000" means that the text will display for 2 seconds*/
if(System.currentTimeMillis()-userFired < 2000 && deadWumpus()==true) {
g.drawString("You hear a scream.",300,675);
}
}

How to make a similar animation to Flappy Bird in Libgdx?

I'm creating a game like flappy bird where the bird flaps his wings only when the screen is touched, but I'm having a problem activating the animation when the screen is touched.
batch.draw(animation.getKeyFrame(myTimeState, false), x, y); //the myTimeState is 0 to render the 1st frame only.
Then when the screen is touched I do this:
//myTimeStep is just basically a controllable timeState for the animation only
if(Gdx.input.justTouched){
myTimeState = timeState;
}else if(Gdx.input.justTouched == false && animation.isAnimationFinished(timeState)){
myTimeState = 0;
}
I don't think the animation is able to play all the frames because myTimeStep become 0 immediately after finishing to touch the screen. Also I don't think this is the right way of doing it, if you guys have better ideas or solution please help. Thanks in advance.
There are probably several ways to achieve this. You'll need to increment your timeState, of course, and also it depends how long your animation is and if you want it to loop.
If you've created your animation to play only once, and then stop (until the screen is touched again), you could simply set your myTimeState to 0 when the screen is touched, and then increment it every frame. The animation will run through and then "stop" on its own when it reaches the end (as you said no loop). The next time someone touches the screen, your myTimeState is set back to 0 and the animation starts again.
Firstly, you have to ensure your animation's playmode is set to Animation.PlayMode.NORMAL. It's a default setting, but if you set it somewhere to LOOPED, nothing would work as expected.
Secondly, I wouldn't use Input.justTouched() in this case. Instead, a listener in your input processor would be a great fit. Here's an example with Stage. If you have no idea what input processor is, here's tutorial on event handling and class documentation.
stage.addListener(new ClickListener() {
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
if(button == Input.Buttons.LEFT) {
timeState = 0;
}
return super.touchDown(event, x, y, pointer, button);
}
});
You can pick what's going to be displayed (animation's keyframe or sprite) based on result of animation.isAnimationFinished()
if(animation.isAnimationFinished(timeState)) {
//draw some sprite
} else {
//draw animation keyframe
}
I haven't checked it but there's a possibility, that this could lead to the last frame being cut out, because as soon as it gets displayed, animation.isAnimationFinished() will return true. I may be wrong, so you'll have to check it. If it becomes an issue, you can add your sprite as the last frame of your animation. When animation ends, it frezzes on the last frame, which would be your static sprite.
In both cases you'll get your animation played at the beginning of game because timeStep equal to 0. I see 2 solutions, I advise you to take the second:
Set timeStep to a large number, that is for sure larger than your animation's duration. Animation.isAnimationFinished() will then return true.
Introduce boolean variable isAnimationPlayed that:
is initialized with false,
gets set to true in your click listener,
gets set to false during isAnimationFinished(), which is called each frame only when isAnimationPlayed is true,
is used in draw() method to determine what to display.
You could just set your timeState to the duration of the animation.

Properly implementing a delay in a game

This is a follow up to a previous question I had. I have a Battleships game with two boards. When the user clicks on the computer board an action occurs, along these lines:
public void mouseClicked(MouseEvent e)
// Get coordinates of mouse click
if (//Set contains cell) {
/add Cell to set of attacked cells
//Determine if set contains attacked cell.
// If yes, hit, if no, miss.
checkForWinner();
The checkForWinner method determines if the game has been won yet. If it hasn't it calls a nextTurn method which changes the current turn. If the currentTurn is set to Computer, a ComputerMove() method is automatically called.
When that method finishes, it again checksforWinner, changes turn and waits for the user to click on the grid to start the cycle again.
Ideally, I'd like to have sound effects, or at the very least a pause between moves. However, no matter how I use Thread.sleep, or TimerTask, or anything else, I can't get it to function correctly.
If I use a simple Thread.sleep(500) in the CheckforWinner method, or in the ComputerMove method, all that happens is the human's go is delayed for the set amount of time. As soon as his move is executed the computer's move is completed immediately.
I know very little about threads but I assume this is because all the initiation of the bouncing back and forth between methods begins with a method in the mouse listener.
Given the set up of my system, is there a way to implement a delay without radically changing things?
Edit: May as well include the classes:
public void checkForWinner() {
if (human.isDefeated())
JOptionPane.showMessageDialog(null, computer.getName() + " wins!");
else if (computer.isDefeated())
JOptionPane.showMessageDialog(null, human.getName() + " wins!");
else
nextTurn();
}
public void nextTurn() {
if (currentTurn == computer) {
currentTurn = human;
} else {
currentTurn = computer;
computerMove();
}
}
public void computerMove() {
if (UI.currentDifficulty == battleships.UI.difficulty.EASY)
computerEasyMove();
else
computerHardMove();
}
public void computerEasyMove() {
// Bunch of code to pick a square and determine if its a hit or not.
checkForWinner();
}
Ideally, I'd like to have sound effects, or at the very least a pause between moves. However, no matter how I use Thread.sleep, or TimerTask, or anything else, I can't get it to function correctly.
You should be using a Swing Timer. Something like:
Timer timer = new Timer(1000, new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
currentTurn = computer;
computerMove();
}
});
timer.setRepeats(false);
timer.start();

Why does not Thread.sleep() work in Action Performed?

public void actionPerformed(ActionEvent e) {
String sp1="Player 1's turn. ";
String sp2="Player 2's turn. ";
System.out.println("Mouse entered for rating " + index); //helps me track the cards
ori=new ImageIcon(getClass().getResource(index+1+".png")); //set the image to the card
ori.setDescription("ori"); //It's weird that I cannot directly flip the card, so I used this method to flip the cards.
tail.setDescription("tail");//http://stackoverflow.com/questions/13557561/the-method-about-imageicons-does-not-work
if (((ImageIcon) bt[index].getIcon()).getDescription()=="ori")
bt[index].setIcon(tail);
else
bt[index].setIcon(ori);
count++;
System.out.printf("Action Performed %d times \n",count);
if(count==1){ //if the card is clicked for once, the card should not flip and the index is stored in record.
record=index;
countS++;
}
String turnS=Integer.toString(countS);//parse the int and printout the turn
// text3.setText(sp1+"This is turn "+turnS);
if(count==2){
int match1=record/4; //Since every four cards have the same rank, I used this to get the rank very easily
int match2=index/4;
if(match1==match2&&record!=index){ //compare the two cards clicked
p1++;
score1=Integer.toString(p1);
text1.setText("Player 1: "+score1); //display the score
text3.setText(sp2+"This is turn "+turnS);
bt[index].setEnabled(false);//remove the cards that match
bt[record].setEnabled(false);
}
if(record==index){
text3.setText(sp2+"This is turn "+turnS);//designed for the situation that the user clicks the same card
}
if(match1!=match2){//designed for the situation that the two cards do not match
//time.schedule(taskFlip1,500);//delay the flip so that the user can see the cards
//time.schedule(taskFlip2,500);
try{ **//This part is problematic!**
Thread.currentThread().sleep(4000);
flip(index);
flip(record);
}
catch(Exception ie){
}
}
text3.setText(sp2+"This is turn "+turnS);
}
When I click on the button, the button is supposed to change the ImageIcon. It works fine without the sleep. But after I add sleep, when I click on the button, the program pauses without changing the ImageIcon! Can you tell me why? Thank you!
The actionPerformed() method runs in the event dispatching thread. So does the repaint system. If you sleep, you are deferring painting, and everything else. You should never sleep in this thread. If you want a deferred paint, use SwingWorker or javax.swing.Timer to start a deferred task.
The action is executed by the very thread which also does handle the drawing. You block this thread. You see nothing. Game over.
This is the reason why YOU SHALL NOT BLOCK NOR DELAY THE EVENT-DISPATCHER THREAD.
Searching for the term "Event Dispatcher Thread" and "blocking" you find plenty of stuff explaining the gory details.
ActionPerformed is run in the event-dispatcher thread. Sleeping in there will stop the UI from updating.
You can use a Swing Timer instead to delay an action.

Categories