Java Programming: Let's Build a Game #7

preview_player
Показать описание

In this video we look at fixing the sticky keys glitch and creating another enemy AI. If you have any questions then be sure to leave a comment or contact me on twitter; realtutsgml. If you learned something then be sure to leave a like, comment, and favorite.

Have you ever wanted to create games? Have you ever gotten fed up with it being to difficult? Well now is the time to thank me and possibly subscribe because you have just found the channel for you! Game Maker Tutorials, Java Game Programming, Unity3D Tutorials, Batch, C++ and much much more! This is the channel for you, the one stop shop for an exploration of your hidden talent as a game developer. Unleash your potential and go wild with imagination when you finally figure out how to make any game you want!

Visit CodingMadeSimple for more exclusive tutorials and get the help you need to succeed as your very own indie game developer!

Follow me on twitter for exclusive content and interaction with me!

Follow me on Google+ to keep updated with all of my tutorials

Game Maker Studio: Programming
Game Maker Studio: Tutorial
Java Programming
Game Programming
Game Tutorial
Programming Tutorial

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

If anyone is still confused and their SmartEnemy is barely moving and then stopping, make sure that you cast velX and velY in SmartEnemy to float. It should look like this:

velX = (float) ((-1.0/distance) * diffX);
velY = (float) ((-1.0/distance) * diffY);

This was the problem I had; they were cast to int instead of float. Hope this helps.

manderbandit
Автор

Just a suggestion, when you say "cast all integers to float", just remember you cant be that general because its computer science. There were a few ints which you actually did keep as ints and it took me more than an hour to get my program to work because of this. I would suggest actually going through each one and reminding us which ones NOT to change.. not that you didn't do a great job though.. keep it up!

nikhilkarnam
Автор

🔥Bro, at 14:06 there is no need to use floats at all. I changed Your code lines 30-35 as following:

double diffX = x - player.getX() - myWidth/2;
double diffY = y - player.getY() - myHeight/2;
double distance = Math.sqrt(diffX*diffX + diffY*diffY);

velX = (int) (-1.5*diffX/distance);
velY = (int) (-1.5*diffY/distance);

And it all works perfect with no need of changing every int into float anywhere.
Just the magic -1.5 coefficient makes a difference, because -1.0 is too low for calculating efficient velX or velY, so the SmartEnemy won't move properly. -2.0 would make the SmartEnemy move fast to the right and down side, and slow to the left and up side.

Anyway, thanx a lot for your tutorials, bro!
Peace!

Кукусик-еэ
Автор

This episode had an extra challenge. While it was indeed a pain, it was also a great lesson in learning Java. I opted to not change everything to float but to just modify the SmartEnemy with a round as suggested in the comments. NOW onto the next part!

vivanecrosis
Автор

I know this is a pretty old video, but when he said about the smart enemy, I coded it slightly differently; to those who don't want to change everything and are fine with the enemy moving only along the axis and not curving, try this:

int px=player.x;
int py=player.y;

if(x-px<0) {
x+=velX;
}
else {
x-=velX;
}

if(y-py<0) {
y+=velY;
}
else {
y-=velY;
}


also, you can set whatever velocity you want for this method

amuzak
Автор

Thank you so much for fixing the glitch upon my request. Im glad to see a youtuber take feedback as well as you do. Keep up the great work, and have a good day!

skully
Автор

For everyone too lazy to change all ints to floats, just add * 3 when setting your velocity. velX = (int) ((-1/distance) * diffX *3)

Camer
Автор

I know this is an old video but I'm still learning concepts from it, however for anybody in the future watching it and reading this, loading the false variables in the beginning can be soooo much easier. Like one line.

for(boolean b:keyDown)b=false;

ohbugish
Автор

10:00 okay, the problem in here is, that the farther away the player is, the faster the enemy is. It could be, that you'd like that behaviour to be, but a little fixed version is using:

float px = target.getX(), py = target.getY();
float diffX = x-px, diffY = y-py;
float angle = (float) Math.atan2(diffX, diffY);

sy = (float) -Math.cos(angle);
sx = (float) -Math.sin(angle);

this will just go into the direction of the player.

cactuscoder
Автор

Thanks! For showing how to fix the sticky keys glitch.

mikaelkolehmainen
Автор

For those who having problem on when the smart enemy stops when reaching the players position. Just move the for loop made public smart enemy to tick() method.

PAUL-kydq
Автор

I just wanna say that this is the first java game that I have ever created and fixing those errors without the video was very annoying but yet it helped me learn more, so thanks I guess.

desiredpenguin
Автор

This has to be the most difficult thing I've done so far learning how to code. It was also the biggest learning experience I've had. I'm glad he didn't think ahead.

dbz
Автор

I've upgraded key-releasing function so now there is no problem with it. The part of the function from 4:50 looks like this:

if (tempObject.getID() == ID.Player) {
if (key == KeyEvent.VK_UP) { keyDown[0] = false; if(keyDown[1]) tempObject.setVelY(5); else tempObject.setVelY(0); }
if (key == KeyEvent.VK_DOWN) { keyDown[1] = false; if(keyDown[0]) tempObject.setVelY(-5); else tempObject.setVelY(0); }
if (key == KeyEvent.VK_RIGHT) { keyDown[2] = false; if(keyDown[3]) tempObject.setVelX(-5); else tempObject.setVelX(0); }
if (key == KeyEvent.VK_LEFT) { keyDown[3] = false; if(keyDown[2]) tempObject.setVelX(5); else tempObject.setVelX(0); }
}

mynor
Автор

I know this is late, but you can substitute the messy distance equation with something more clean like:

float distance = (float)(Math.sqrt(Math.pow(x - player.getX(), 2) + Math.pow(y - player.getY(), 2)));

MrMegajack
Автор

I want to say thank you for this series!  It has been a huge help to me for my class.  You've helped me solve so many problems I've had on my own program.   I honestly cannot thank you enough for doing this!

leftysonstage
Автор

2:41
Bool (like pool but with a b)
ee (as in he, she, we)
in (as in... well, in.)
Bool-ee-in.
Boolean.

adjoint_functor
Автор

About sticky keys bug, your solution works fine but if you press D and then press A and release it while still hodling D key, then rectangle moves still to the left side. I know it's a minor issue but some users might find it irritating.

Zeriver
Автор

With this keys fix what happens when you press both left and right / up and down? Does it bug? It bugged to me so I managed to get a solution like:

                if (key == KeyEvent.VK_LEFT) {
                    keyPressed[0] = false;
                    if(keyPressed[1]) tempObject.setVelX(5);
                }

in KeyReleased.

Also, it's not as accurate but instead of changing everything to float you can just use integers and instead of -1 use something between -2 and -your Player speed (player speed already makes it almost impossible), since it's an int the smartEnemy will go either fully Vertical/Horizontal a lot of times (because that calc with int's is easy to get 0) but it will work (just not as pretty). Other solution would probably change only the velX/velY to float or using a float as aux in the middle because the (-1/distance=]0, -1] is always 0 as an int (ints always "take the first ").


You could have also just said that distance is simple triangulation(Pitagoras Theorem) so who is on school start realizing that things they learn there are really useful in programming.

pedrodavid
Автор

It's easier to use float distance = (float) Math.hypot(x - player.getX(), y - player.getY()); instead of Math.sqrt

andrijaada