Hi farrel, thank you for this tutorial. I'm a pure newbie at Java, and am trying to learn how to place images instead of the rectangles. Also, I've looked around on how to set the background of the JFrame( or is it JPanel ?) , but I get confused on which class to include/create the background in. Would really appreciate it if you could show how to use an img for background and for the rectangles, based on this tutorial.
Also I tried adding another rect ( enemy2 = new Entity) and added the collision codes respectively, but it still doesn't detect.
///////////////////////////////////////////////
public void checkCollision()
{
if (player.getBounds().intersects(enemy.getBounds()))
player.collision = true;
if (player.getBounds().intersects(enemy2.getBounds()) )
player.collision = true;
else
player.collision = false;
///////////////////////////////////////////////
What makes it worse, the first "enemy" doesn't detect collision too once I add collision codes for "enemy2". Could you explain how about doing so if I would like to add say.. 4 more enemies? Thank you in advance!![]()
You have to understand that code I posted is very sloppy. There are better ways to check for collisions and to draw. The code is meant to show basic principles.
You can add images for the enemies by creating an Image like so:
Image enemyImage = Toolkit.getDefaultToolkit().getImage("enemy.jpg");
Stick that somewhere at the top of the DrawPanel class, and then find the drawBuffer() method and instead of the code that draws the player rectangle ' b.fillRect(x,x,x,x)', use b.drawImage(enemyImage, player.getX(), player.getY(), null);
As far as you adding extra enemies, duplicating what I did for the first two, if done correctly, will produce the same results. In checkCollision(), you need to make sure that every enemy is checking for each other.
PM your code to me, or make it available for download somewhere, and I'll look at it for you when I have time.
Thank you again farrell2k.
I managed to add more enemies to this, and the collisions now work by using "else if" repeatedly. Can you explain to me how I can change the background of the Panel as well ? Thank you thank you.
Thanks a ton @All .... nice ideas![]()
This is a cool tutorial, but i have one problem. my java compiler can't find Graphics2D. i copied the code exactly but when it gets to "Graphics2D b = buffer.createGraphics();" i keep getting a cannot find symbol error. please help. i even tried imported the Graphics class and Graphics2D class.
use the package= import java.awt.*;
There are currently 4 users browsing this thread. (0 members and 4 guests)
Bookmarks