Java Game Development TUTORIAL - Episode #31 - Game Paused!

preview_player
Показать описание
Timestamps:
00:00 Intro
00:20 Creating the button
02:28 Adding the function
04:28 Testing pause
06:08 toggle pause for unpause/pause switch
08:25 Changing the button text
09:36 Adding "Game is paused!"
11:01 Fixing color flickering
11:38 Outro and next episode

**Useful information:**
If you decide to, NOT allow the player to interact with the towers while paused. Just make sure you can still unpause the game in actionbar.
Might need to add the ... "&& !gamePaused"... part in the actionbar mouse events on all buttons BUT the pausebutton.

Links:

-----------------------------

Wish to support me?
You can do so on:
Everything is appreciated!

-----------------------------
Рекомендации по теме
Комментарии
Автор

EDIT - UPDATE A link to the discord server is in the video description.

Hey all.
Hope you are all doing well.
Got a shorter video for you today. Adding the pause button so you can take a break when needed =)
Good luck!

KaarinGaming
Автор

For anyone that wants to disable the action bar while the game is paused:
You will need to add a special case to your playing.mouseClicked() method that if will let you be able to click the 'Menu' and 'Unpause' buttons and would look something like this:

if (!gamePaused && y >= 640)
actionBar.mouseClicked(x, y);
else if (gamePaused
&& (actionBar.bPause.getBounds().contains(x, y) || actionBar.bMenu.getBounds().contains(x, y))) {
actionBar.mouseClicked(x, y);
} else if (y < 640) {
[rest of code]

You will also need to change bMenu and bPause to public in order for this to work.
Also, to fix the bug of the player selecting a tower, pausing the game, and then placing the tower:

Change selectedTower to public in the Playing class

in the ActionBar.togglePause() method, add this line under if (playing.isGamePaused()):
playing.selectedTower = null;

EDIT: You could also use the setter instead and use the line:

(Don't forget to add your brackets)

A quick and chunky fix, but I hope it helps someone!

TatexRedding
Автор

Once i paused everything, i found out it display snapping grid. i think this part for controlling disable to display it or not. in the playing class

public void mouseClicked(int x, int y) {
if(y >= 640 && !gamePaused) {
actionBar.mouseClicked(x, y);
} else if(gamePaused && actionBar.getButtonPause().getBounds().contains(x, y)
|| actionBar.getButtonMenu().getBounds().contains(x, y)) {
actionBar.mouseClicked(x, y);
}

wonderzhou
Автор

this piece of code seems like not working on my side. so I changed into ActionBar class

// Sell Button
if(!playing.isGamePaused()) {
sell.draw(graphics);
drawButtonFeedback(graphics, sell);
}

// Upgrade Button
if(displayedTower.getTier() < 3 && gold >= && !playing.isGamePaused()) {
upgrade.draw(graphics);
drawButtonFeedback(graphics, upgrade);
}

wonderzhou
join shbcf.ru