Kilobolt
  • Home
  • Tutorials
    • Game Development Tutorial >
      • Unit 1: Beginning Java >
        • Before you begin...
        • Day 1: Setting Up
        • Day 2: Java Basics
        • Day 3: More Basics
        • Day 4: Java Math
        • Day 5: More Math
        • Day 6: If... else...
        • Day 7: More Control Flow
        • Day 8: Looping
        • Day 9: More on Looping
        • Day 10: Inheritance, Interface
        • Day 11: Threads and Graphics
      • Unit 2: Creating a Game I >
        • Day 1: Foundations
        • Day 2: Basic Framework
        • Day 3: Taking User Input
        • Day 4: Enter the Robot
        • Day 5: Background and Sprites
        • Day 6: Adding Enemies
        • Day 7: Shooting Bullets
        • Day 8: Animations
        • Day 9: 2D-Arrays
        • Day 10: Painting the Tilemap
      • Unit 3: Creating a Game II >
        • Day 1: Level Creation - Part 1
        • Day 2: Level Creation - Part 2
        • Day 3: Level Creation - Part 3
        • Collision Detection Basics
        • Day 4: Collision Detection Part 1
        • Day 5: Collision Detection Part 2
        • Day 6: Collision Detection Part 3
        • Day 7: Health System & Death
        • Day 8: Basic AI & Final Touches
      • Unit 4: Android Game Development >
        • Day 1: Introduction to Android
        • Day 2: Setting up for Development
        • Day 3: Creating our First Android Application
        • Day 4: Parts of an Android Application
        • Day 5: The Android Game Framework: Part I
        • Day 6: The Android Game Framework: Part II
        • Create an Android Game From Scratch (or port your existing game)
        • Day 7: Creating an Android Game (From Start to Finish)
      • Reference Sheet
    • Zombie Bird Tutorial (Flappy Bird Remake) >
      • Unit 1: Building the Game >
        • Introduction
        • Day 1: Flappy Bird - An In-depth Analysis
        • Day 2: Setting up libGDX
        • Day 3: Understanding the libGDX Framework
        • Day 4: GameWorld and GameRenderer and the Orthographic Camera
        • Day 5: The Flight of the Dead - Adding the Bird
        • Day 6: Adding Graphics - Welcome to the Necropolis
        • Day 7: The Grass, the Bird and the Skull Pipe
        • Day 8: Collision Detection and Sound Effects
        • Day 9: Finishing Gameplay and Basic UI
        • Day 10: GameStates and High Score
        • Day 11: Supporting iOS/Android + SplashScreen, Menus and Tweening
        • Day 12: Completed UI & Source Code
    • Android Application Development Tutorial >
      • Unit 1: Writing Basic Android Apps >
        • Before you begin...
        • Day 1: Android 101
        • Day 2: Getting to Know the Android Project
        • Day 3: The Development Machine
        • Day 4: Building a Music App - Part 1: Building Blocks
        • Day 5: Building a Music App - Part 2: Intents
        • Day 6: Building a Music App - Part 3: Activity Lifecycles
  • New Forum
  • About Us
    • Contact Us
  • Our Games
    • TUMBL: FallDown
  • Facebook
  • Twitter

GAME DEVELOPMENT TUTORIAL: DAY 2-6: Adding Enemies

10/23/2012

75 Comments

 
Picture
Hello everyone, and welcome to Day 6 of Unit 2. In today's lesson, we will be adding an enemy to our game, because our main character is getting a bit lonely.
 
Our first enemy will look like this:
Picture
The Menacing Heliboy
Too add Heliboy to our game, we will first create an Enemy class (which will serve as the superclass for most enemy types), and also a Heliboy class, which will be the subclass of the Enemy class.
Lesson #2-17: Creating the Enemy Class
The Enemy class will be pretty simple. Since this will be a superclass, we will define commonly used variables and methods, so that our subclasses can inherit them.

Here's how we will proceed:

1. Create the Enemy class.
2. Declare variables
3. Create behavior related methods
4. Create helper methods (Getters and Setters)

I. Creating the Enemy Class

Picture
Creating the Enemy class.
1. Right-click on our kiloboltgame package >> New >> Class.
2. Name it "Enemy".
3. Press OK!

II. Declaring Variables

All the enemies that we create will probably have the following:

1. Max Health
2. Current Health
3. Power (damage output)
4. Speed
5. X coordinate
6. Y coordinate

In addition, whenever the background scrolls, the enemy should move in the same direction. So we will create a reference to the bg1 object in StartingClass.

Within the class definition, declare the following:

private int maxHealth, currentHealth, power, speedX, centerX, centerY;
private Background bg = StartingClass.getBg1();

III. Creating Behavioral Methods

As with all other game objects, we will need to first create a method that will constantly be running: update(); In addition, we will create a die(); and attack(); method for the enemy.

Add the following code below your variable declarations:

//Behavioral Methods
public void update() {
centerX += speedX;
speedX = bg.getSpeedX();
}

public void die() {
}
public void attack() {
}



TIP: In Eclipse, you can indent multiple lines of code at once by selecting multiple lines and pressing Tab. You can also dedent by pressing Shift + Tab. 

TIP #2: You can auto format your code (fix indents and such) by pressing Ctrl + Shift + F.

IV. Generate Getters and Setters

We will need to create methods that will allow us to retrieve and manipulate the variables declared in this class. 

1. Right-click on your code >> Source >> Generate Getters and Setters.
2. Select All, and press OK.
3. Eclipse will automatically add the following code:


public int getMaxHealth() {
return maxHealth;
}

public int getCurrentHealth() {
return currentHealth;
}

public int getPower() {
return power;
}

public int getSpeedX() {
return speedX;
}

public int getCenterX() {
return centerX;
}

public int getCenterY() {
return centerY;
}

public Background getBg() {
return bg;
}

public void setMaxHealth(int maxHealth) {
this.maxHealth = maxHealth;
}

public void setCurrentHealth(int currentHealth) {
this.currentHealth = currentHealth;
}

public void setPower(int power) {
this.power = power;
}

public void setSpeedX(int speedX) {
this.speedX = speedX;
}

public void setCenterX(int centerX) {
this.centerX = centerX;
}

public void setCenterY(int centerY) {
this.centerY = centerY;
}

public void setBg(Background bg) {
this.bg = bg;
}


And you are done with the Enemy class for now! It should now look like this:

Figure 2-28: Enemy Class

package kiloboltgame;


public class Enemy {


private int maxHealth, currentHealth, power, speedX, centerX, centerY;
private Background bg = StartingClass.getBg1();


// Behavioral Methods
public void update() {
centerX += speedX;
speedX = bg.getSpeedX();


}


public void die() {


}


public void attack() {


}


public int getMaxHealth() {
return maxHealth;
}


public int getCurrentHealth() {
return currentHealth;
}


public int getPower() {
return power;
}


public int getSpeedX() {
return speedX;
}


public int getCenterX() {
return centerX;
}


public int getCenterY() {
return centerY;
}


public Background getBg() {
return bg;
}


public void setMaxHealth(int maxHealth) {
this.maxHealth = maxHealth;
}


public void setCurrentHealth(int currentHealth) {
this.currentHealth = currentHealth;
}


public void setPower(int power) {
this.power = power;
}


public void setSpeedX(int speedX) {
this.speedX = speedX;
}


public void setCenterX(int centerX) {
this.centerX = centerX;
}


public void setCenterY(int centerY) {
this.centerY = centerY;
}


public void setBg(Background bg) {
this.bg = bg;
}



}

Lesson #2-18: Creating the Heliboy class.
We will now be creating the Heliboy class.
We will be creating this class slightly differently, so even if you know how to make classes in Eclipse, read the instructions below!
 
1. Right-click on kiloboltgame >> New >> Class
2. Name it "Heliboy"
3. Now, in the Superclass: option, press Browse.
4. Type "enemy" in Choose a type:
Picture
Choosing the Enemy Superclass
5. Press OK.
6. Check "Constructors from superclass:"
Picture
7. Press Finish.

Voila! The following code generates:

Figure 2-28: Heliboy Class

package kiloboltgame;

public class Heliboy extends Enemy {

public Heliboy() {
// TODO Auto-generated constructor stub
}


}

By taking steps 4 and 6 : choosing Enemy as a superclass and adding a constructor, Eclipse automatically generated the code in bold above. No other changes are made. It is just a shortcut. 

Replace the //TODO message with the following code:

setCenterX(centerX);
setCenterY(centerY);


And add the following parameters to the constructor so that we can use centerX and centerY:

public Heliboy(int centerX, int centerY) {
And now you have the completed Heliboy class. Whenever you create a new Heliboy object, you will pass in two parameters, which will define the central coordinate of your enemy.
Lesson #2-19: Displaying Heliboy
To display Heliboy in our game, we must do the following.

1. Create an Image object for Heliboy.
2. Define hb variables that will be objects created using the Heliboy constructor.
3. Call the update() method for these objects.
4. Paint hb objects with the Image object created in step 1.

I. Create Image Object - Heliboy

heliboy.png
File Size: 11 kb
File Type: png
Download File

1. Download the above image, place it in your data folder, and rebuild the project by going to Project >> Clean >> Clean All Projects >> OK.

2. Open StatingClass.java, and add heliboy to the Image declarations.

private Image image, currentSprite, character, characterDown, characterJumped, background, heliboy;

3. In the // Image Setups section, define the heliboy Image object:
 heliboy = getImage(base, "data/heliboy.png");

II. Create HB Variables

1. Below the private Robot robot; declaration, add the following:
private Heliboy hb, hb2;

We will be creating two Heliboy objects.

2. Within the start() method, add the two bolded lines BELOW the Background creation (the Enemy superclass looks for these backgrounds, so if they are not defined, your game will crash).

bg1 = new Background(0, 0);
...
hb = new Heliboy(340, 360);
hb2 = new Heliboy(700, 360);

...

Thread thread = new Thread(this);
...

This will create two Heliboy objects centered at (340, 360) and (700, 360).

III. Call the Update Method

Scroll down to the run() method, and add:

hb.update();
hb2.update();

Like so:
 
 @Override
public void run() {
while (true) {
robot.update();
if (robot.isJumped()){
currentSprite = characterJumped;
}else if (robot.isJumped() == false && robot.isDucked() == false){
currentSprite = character;
}
hb.update();
hb2.update();
bg1.update();
bg2.update();
repaint();
try {
Thread.sleep(17);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

You might notice that we never defined update() in the Heliboy class; however, since the Heliboy class inherited the Enemy class, calling update() here will automatically call the update() method in the Enemy class. This is inheritance in action. :)

IV. Paint the HB objects

Finally, we have to make the newly created Heliboy objects appear on the screen.
1. Go to the paint() method and add the following at the end:

g.drawImage(heliboy, hb.getCenterX() - 48, hb.getCenterY() - 48, this);
g.drawImage(heliboy, hb2.getCenterX() - 48, hb2.getCenterY() - 48, this);


The Heliboy.png image you downloaded has dimensions 96 x 96. So, if we paint 48 pixels lower in both X and Y (by subtracting 48), then whatever numbers that we input in the constructor (e.g. hb = new Heliboy(340, 360);) will represent the centerX and centerY coordinates of the newly drawn images.

And that's it for Day 6! I hope you guys are beginning to pick up on the patterns, so you can add your own ideas to the game as they pop up! We will be adding a shooting mechanic in the next lesson. :)

Thank you for supporting Kilobolt and tell your friends about us! :)
Picture
kiloboltday6.zip
File Size: 711 kb
File Type: zip
Download File

Picture
Go to Day 5: Background and Sprites
Go to Day 7: Shooting Bullets
75 Comments
Brendan
10/23/2012 03:39:47 pm

How would we go about painting an enemy that is off the screen but will come into view as the background scrolls? Would we paint the enemy outside of the 800x480 area and then double his speed so that he actually moves into the viewing area instead of staying stationary outside of it? I probably have some of the logic mixed up but this is how I imagine it would work.

Reply
James
10/23/2012 04:07:39 pm

Brendan, our current setup already handles that. The enemy's speed is going to be the background scrolling speed plus their actual speed, so it will work out.

So yes, you are right about painting outside the area, and yes we do mess with their speed a little bit, but not quite like you said.

Reply
Brendan
10/23/2012 04:10:30 pm

My previous question was dumb, I was reading from my phone and thinking this out in my head. I should have just went downstairs and tried it on the computer.

Changing the x coordinate was the only thing I had to change to get the enemy into view. Look forward to the next unit!

Reply
knightkill
10/23/2012 07:34:39 pm

That's it.This tutorial is what I was searching for all over the net.
Dude,You really rock at teaching.
Hope u,carry on forward 2 teaching it as video tutorial.

Waiting for more... thanks for this nice tutos...!!

Reply
BMATroid
10/26/2012 09:46:39 pm

Excellent tutorial.

I'm just a beginner but I after I played around with the codes, game development tend to be so easy for me.

Do you have tutorials for Unity3D? I have a license but I don't know how to use since I'm new to gamedev.

Thank you so much.

Reply
Reece
11/19/2012 04:01:00 pm

Really good stuff again thank you!

Why do you declare everything as a private variable only to create getters and setters? -wouldn't making them public save you having to do that?

Cheers much!!

Reply
James C.
11/25/2012 05:25:42 am

You are right; however, it's just good practice in Java to keep variables private so that unwanted functions do not mess with them accidentally. :)

This is a more advanced topic that you learn, but it's good to get into the habit of doing that now.

Reply
Faheem
11/25/2012 05:17:11 am

Hey! First of all, FANTASTIC tutorials, I've been hitting at Java for ages, trying to get my head around it, only at a slow pace though. With your tutorials, I'm breezing through it! Thank-you! A donation is definitely coming your way from me!
I'm having a slight issue with Day 6. When ever I run the Applet, the 'heliboy' enemy doesn't seem to update and move along with the background when I move the character. I've checked it through letter by letter 3 times now, there are no errors. Then downloaded the source, copied and pasted it over to see if that would resolve it, but still the same issue? Any suggestions? I'm real confused right now :S

Reply
James C.
11/25/2012 05:24:35 am

Thanks for reading the tutorials! I am glad that you are getting something out of it.

If you could take the following steps, I can help you out more easily:

1.Right click your project folder, select Export >> General >> File System.

2. Make sure your project folder is checkmarked, and export it to a directory of your choice.

3. Compress the folder to a .zip or .rar and email it to jamescho7@kilobolt.com.

Thanks!

Reply
Aman
12/19/2012 09:31:45 pm

Hi james
i also have the same problem. my enemy doesnt move along with the background and once i press the right key for some time the enemies go out of view.... i hve checked my code again and again and i dont seem to get any solution,,,

plz help...

Reply
James C.
12/20/2012 02:07:56 am

Hey Aman, this is normal behavior. We don't want enemies fixed to the background. Their movement should be independent. Please watch the video of Metal Slug to see what we are trying to accomplish.


-

Reply
ade
12/20/2012 10:23:58 am

Hi James,

Thanks for the great tutorial. I was wondering if you can detail a bit on the this statement:

private Background bg = StartingClass.getBg1();

Is it normal to access a method from a class without instantiating it? Also intrigues me the how is possible to use a method in declaring a variable.

Reply
James
12/20/2012 04:29:43 pm

StartingClass in this example refers to the class, and that is why we do not need to instantiate it. It does not refer to a particular StartingClass object.

As for methods, getter methods simply return a value and there is nothing in Java that prevents its usage in particular locations.

Hope that helps.

Reply
ade
12/20/2012 10:10:18 pm

I understand. Thanks for your answer!

Ziff Sedgewick
12/26/2012 03:28:11 pm

This is a great series, thanks a lot!

My major was CS but I haven't worked in the field for a long time, this is a great refresher!

Reply
Muhammad Babar
12/29/2012 09:50:52 pm

Hi james im bit confused understanding you said enemy is 98x98 and we do hb.getCenterX()-48 and hb.getCenterY()-48 to get the center of the image,but this actually draws it a upper and towards X-axis,,please reply quick so i can understand,i have read drawImage(Image,x,y,observer) method it says x,y coordinates are the top left corners of the image to be drawn,

Reply
James C.
1/1/2013 12:12:42 pm

Muhammad, sorry for the late reply!
g.drawImage(heliboy, hb.getCenterX() - 48, hb.getCenterY() - 48, this);

centerX() refers to the middle of the heliboy object. So when we place the top left corner at middle - 48, it creates the intended effect.

This is the same thing as drawing:

g.drawImage(heliboy, hb.getTopLeftX(), hb.getTopLeftY(), this);

And since TopLeftX and TopLeftY would be centerX - 48 and centerY - 48, respectively, it works just as intended.

Try drawing a diagram! :)

Reply
Mukul
1/5/2013 08:49:03 pm

hey,thank you vey much for the great tutorial,but I am having a little problem.my enemies won't repaint.i just have two heliboys printed.and as my robot moves to the right they are gone with the background.but no new enemies are printed again.tried copy pasting your source,but didn't work.

Reply
James C.
1/5/2013 09:46:54 pm

This is normal. We do not want the enemies to teleport back.

Reply
sowmya
7/30/2015 05:18:04 am

as we are repainting ,why do the enemies dont appear again?

Dennis
1/6/2013 10:26:07 am

excelent I'm so excited for learning more :D thanks a lot

Reply
Jesse
1/7/2013 12:21:33 pm

Great tutorials! You Explain very well and for that i thank you

Reply
Vlad
1/9/2013 11:45:13 pm

Great Tut, my contribution we´ll be getting to you soon ;) . I have all the code from day 6. The applet draws background enemies and the robot but the only thing it does is jump... nothing else

Reply
Vlad
1/10/2013 07:22:03 am

Sorry, my mistake... the gamer in me trying to use w,a,s,d to move the robot... duh :D works just fine

Reply
Joe
2/22/2013 10:37:02 pm

Hey James, great tutorials, I'm really enjoying them. I have a bit of a problem with this weeks code. The window is just empty and black when I run it.
I can see the background, the robot and a heliboy if I maximize the window but I can't move the robot at all. I've downloaded your source code and I can't find any differences.

Reply
Joe
2/23/2013 07:11:25 am

Wait, fixed it, I forgot to add the 2 to the second hb in the start method. Its what I get for programming on such little sleep. Anyways, keep up the good work!

Reply
S-Markt
3/2/2013 04:35:27 pm

"You might notice that we never defined update() in the Heliboy class; however, since the Heliboy class inherited the Enemy class, calling update() here will automatically call the update() method in the Enemy class. This is inheritance in action. :)"

instead of using a superclass, would it be possible to copy and paste the code and use it in the new class? of course it would but it will mean less occupied memory when you use a superclass, right? are there any other advantages? or does java do the same when compiling the sourcecode? therefore it would be less confusing not to uses superclasses.

Reply
JonNal
3/15/2013 07:19:29 am

How can you make a combo. For example i want to make the robot jump and the boost forward. How do i do that?

Reply
JonNal
3/15/2013 07:21:04 am

How do can you do combos. For example, i want the robot to jump and the boost forward a little bit. How do i do that?

Reply
Brad
4/7/2013 10:11:01 pm

Hey, first fantastic tutorial, i've been looking at getting into java game development for a while alongside my current C++ that i'm doing at University and i'm finding your tutorial extremely easy to follow and easy to learn.

Also you probably already know about this but you can make your code slightly easier to read by editing the syntax for some of your conditions, for example:

"robot.isJumped() == false" is the same as "!robot.isJumped()" likewise "robot.isJumped() == true" is the same as "robot.isJumped()"

just trying to help out :)

Brad

Reply
Mike
5/6/2013 03:12:56 am

Great Series James! Question though. Is there a particular reason the currentSprite value is tracked by StartingClass? I would think this to be more a job for the Robot class, perhaps in the update() method?

Reply
rushi
5/31/2013 12:27:10 am

setCenterX(centerX);
setCenterY(centerY);

on setting these methods in heliboy class it has red underline saying to change private to protected centerX,centerY

Reply
babita
12/3/2013 03:38:11 pm

same problem here want to know the answer. how it can be clear? fast reply pls..

Reply
jangrie
5/26/2014 08:59:10 pm

I know that this was asked a year ago, but for people who are currently doing this tutorial, having the same problem:

those methods are underlined because they are part of the constructor of the heliboy class. Eclipse doesnt know that you are actually trying to set centerX and centerY (which are not declared in this class as they are automatically inherited by the enemy class) of this class through the constructor. the constructor is missing the 2 parameters for centerX and centerY. I hope this was somehow not too confusing. your constructor of the class heliboy should look like this:

public Heliboy(int centerX, int centerY) {
setCenterX(centerX);
setCenterY(centerY);
}

Grae
8/7/2013 04:31:15 am

Has anyone else had the issue of running the applet and the applet screen being completely black? I check my code multiple times and downloaded the source code and compared it with that, and everything matches up, but when I run it, even if I use full screen, It still displays nothing but black.

Reply
Adink
4/24/2014 11:25:01 pm

the applet screen went black? there's no robot or whatsoever? have you declared bg1 and bg2? or in the package explorer on your eclipse window, theres a background.png file?

package exlporer path : KiloboltGame>src>data>background.png

Reply
tom link
8/12/2013 01:01:18 pm

Don't be a n00b. Make enemy abstract.

Reply
Peter
8/16/2013 09:09:58 pm

Hi kilobolt and i wanna thank you for everything. I just wanna say that I DID recomend this to my friends, as i always dreamed of making a game or an android app with a friend of mine and my dream is comming true. I already knew some basics, so im already on unit 2 going to day 7 now (yey) and he is on day 4 or 5 of the first unit, but this rithm, will be ready to create a game in no time lol THANKS YOU A LOT YOU GOT 2 NEW READERS

Reply
Darchyneer
11/7/2013 04:49:49 am

I did everything you wrote but the enemies did not display on the screen. I also copied your source code and the result was desame. What should I do. I have already sent you an email of the codes I wrote

Reply
Rohit Tuli
1/11/2014 12:56:10 pm

sir, i followed day 6 step by step but i just cant see the heliboy.
tell me what to do.

Reply
Tom
1/30/2014 02:43:06 am

images need to be in KiloboltGame/bin

Rob
2/22/2014 02:29:38 am

Hi, I have the same problem; the heliboys just don´t appear. I already checked if the images are in the correct folders und checked the code twice.

Mahmoud
2/3/2015 09:59:35 pm

Thank you Tom, you are great.

Aaryav
12/8/2013 02:31:09 am

Can anyone explain what this line does
private Background bg = Startingclass.bg1();
and also this one
speedX = bg.getSpeedX();

Reply
Quintin
9/9/2015 06:47:59 am

The first line sets the background bg to the one defined in or references to the background of StartingClass.java, the second line sets speedX of the object to that of the background defined above in the first line.

Reply
Nick
2/19/2014 11:09:34 am

In my Heliboy class I'm getting errors on setCenterX(centerX);
setCenterY(centerY); underlining centerX and centerY not sure whats with this or how to fix, but your tutorials have been spot on thus far, gonna be donating for sure when completed, let me know any help
thanks James!!!!

Reply
HiddenHelper
2/21/2014 03:40:38 am

Did you enter them as arguments inside the Heliboy method?
It should look like this:
public Heliboy(int centerX, int centerY){
setCenterX(centerX);
setCenterY(centerY);
}

Reply
Nick
2/21/2014 07:40:32 am

Bingo, that was it, thanks hidden help

Lightning-theif
8/19/2014 06:33:47 pm

thanks man ,,,
it was helpful........ God bless u

Fred
2/23/2014 07:07:14 am

When I run the game, it has various errors. Usually the robot wont move until i restart it, and when it does move it will only go up to a certain point but won't initiate background scrolling. Only once did the game actually work and scroll like it should. Any idea why it runs so inconsistently on my computer? I get these messages in the console when I run it:

Exception in thread "Thread-3" java.lang.NullPointerException
at kiloboltgame.StartingClass.run(StartingClass.java:80)
at java.lang.Thread.run(Unknown Source)

Thanks for the tutorials!

Reply
Jay
2/26/2014 03:16:43 pm

I am a very big fan of your tutorials!!James
I wanted to know that how to create enemies randomly!! and how to place them on the screens randomly!! Please let me know soon!!

Reply
Quintin
9/9/2015 06:58:59 am

Hey Jay, if you create a while loop, define a Random int and use this int to generate a Hiliboy img randomly. Try using the Random int as a timer for more control.

Reply
Sagar
3/10/2014 12:35:16 am

Hi James !!!
Your tutorials are interesting and a great way to start off with the android game development.
adding enemies chapter
under the startingclass.java
hb = new Heliboy(340, 360);
hb2 = new Heliboy(700, 360);
i am getting the error as
the constructor Heliboy(int,int) is undefined

Reply
Sagar
3/10/2014 12:39:13 am

Sorry James .I found out what the mistake was !!!!

Reply
Yasen E link
3/14/2014 04:08:28 am

Hello Sagar , i have the same problem , can you help me to reslove it ? :) Thanks!

Yasen E link
3/14/2014 04:10:31 am

I fond it too ! :D

varun
3/18/2014 07:06:15 pm

Hi,
As i understand the code, the enemies are moved along with the background. The background a re rolled back but enemies are just updating their position according to the speed of the background. Which means they are always repainted but with a different position every time. So after a while these enemies will not be in the view but still be repainted. Shouldn't there be a check on what is to be painted ?

Reply
Asar
8/19/2014 06:22:06 pm

i have errors in heliboy class on
setCenterX(centerX);
setCenterY(centerY);
and its not removing nor the heliboy is showing up on screen

Reply
Milan
9/6/2014 02:46:05 am

Thanks to Hidden Helper for helping on the Heliboy class errors, just a few lines up. That was much appreciated. (Reposting Hidden Helper's comment):
Did you enter them as arguments inside the Heliboy method?
It should look like this:
public Heliboy(int centerX, int centerY){
setCenterX(centerX);
setCenterY(centerY);
}

Reply
Ayesha link
9/6/2014 03:45:34 am

hello This very very good tutorial...But i have many issues
i couldm't see enemy and the doesn't move till now...I dont know why..can you tell me plz

Reply
Quintin
9/9/2015 06:54:15 am

Ayesha make sure you check your StartingClass where you defined the key methods. If you read above someones solved this issue. #staycoding

Reply
Ayesha link
9/6/2014 03:48:50 am

Couldn't see the enemy and the character doesnot move till now *

Reply
Albert
12/28/2014 03:06:43 am

There is a problem with the code, can't find it, in your code it's the same.
the enemies doesn't move, they stuck in the first paint position..

Reply
Mahmoud
2/3/2015 09:57:08 pm

I did all the steps on this lesson but when I execute the code the enemies don't appear ... so I downloaded your version and compare it to my version, it looks to me both do the same steps. I even copy your codes and paste it over my code and it's still unable to draw the enemies.

Any advice. By the way I imported your version and runs fine.
I really want to know where is the problem. I tried to clean the project.

Reply
Mahmoud
2/3/2015 10:09:48 pm

Solution by Tom,
sorry but I found that this issue has been discussed in a previous comments and solved by Tom.
The solution is to browse to your project folders and make sure to copy the image into the "bin/data" folder too.
As a matter of fact I deleted the heliboy image from the "src/data" and the game runs fine and draw the heliboy on the screen.
Anybody know more details about this issue can help us.

Reply
aviel
4/30/2015 01:23:53 am

first of all great guide!!
second, im having a compilation problem :
"Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at kiloboltgame.StartingClass.paint(StartingClass.java:122)
at kiloboltgame.StartingClass.update(StartingClass.java:104)"

line 122: " g.drawImage(heliboy, hb2.getCenterX() - 48, hb2.getCenterY() - 48, this);"

line 104: " paint(second); "

it happened after doing the project>clean.

if you can help me with that ill be thank you alot.

Reply
aviel
4/30/2015 01:32:41 am

fixed it. problem when creating hb2. accedintly wrote hb = .. instead of hb2 = .. (:////)

Reply
Aero
10/30/2015 03:09:37 pm

For anyone using IntelliJ IDEA IDE instead of Eclipse, and is wondering how to make a Heliboy a subclass of Enemy:

-go to the Enemy class
-click on the 'public class Enemy { ' line
-press alt+enter
-click create subclass and call it Heliboy
-press OK and a new Heliboy class will be created with Enemy as its superclass.

Reply
Govind
11/21/2015 01:28:50 am

We have defined empty methods in class Enemy so that it will be inherited and overridden by the subclasses. But using the "abstract" qualifier will be a better approach and it will prevent any accidental errors. Now java makes it compulsory for all subclasses of Enemy(Heliboy for example) to define die() and attack().

public abstract class Enemy {
private int maxHealth, currentHealth, power, speedX, centerX, centerY;
private Background bg = StartingClass.getBg1();

public void update()
{
centerX+=speedX;
speedX=bg.getSpeedX();
}
public abstract void die();
public abstract void attack();
....

Reply
ass
1/14/2016 09:55:09 am

ass

Reply
BonanConan
2/7/2016 04:02:06 am

My problem is, when I run it, console says:
"load: class kiloboltgame.StartingClass.class not found.
java.lang.ClassNotFoundException: kiloboltgame.StartingClass.class
at sun.applet.AppletClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.applet.AppletClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.applet.AppletClassLoader.loadCode(Unknown Source)
at sun.applet.AppletPanel.createApplet(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)"
....
great tuts!

Reply
Ruckkus
2/11/2016 12:06:29 am

If you still look at this site....

Awesome tutorial. Currently I am trying to animate the robot character when he runs in a certain direction. How do I do this?

By animate, I mean making him have footsteps, like a .GIF.

I looked into changing the image to a .GIF, but I needed to change it to an icon, and you cant draw in icons.

I also tried several ways to making a loop of images that would repeat until the if (player.isMovingRight()) statement was no longer true.... but I was unsuccessful.

If you read this, or anyone reads this, and can help, please let me know how to do this.

Thanks!

Reply
Ahmed
3/4/2016 04:12:16 am

Thank you for this great work but i have a question how to add multiple Enemies for exemple in 10 s adding 5 Enemies

Reply
Hardik
11/13/2016 12:40:07 am

First of all i m thanking u for ur tutorials i really enjoying these....i have a question how to move the character back and to scroll background back so that if we miss our enemy then by go back we kill them....rather then of questioning this i tend to solve this personally and i want that others also get some knowledge with this....if u get this cool also reply....i m pasting this in this section both robot and background classes changes...
.
background class
.
public void update() {
bgX += speedX;

if (bgX <= -2160){
bgX += 4320;
}
if (bgX >= 2160){
bgX += -4320;
}
}
.
robot class
.
if (speedX < 0) {
centerX += speedX;
}
if (speedX == 0) {
bg1.setSpeedX(0);
bg2.setSpeedX(0);
}
if (speedX < 0 && centerX < 61) {
bg1.setSpeedX(MOVESPEED);
bg2.setSpeedX(MOVESPEED);

}
if (centerX <= 200 && speedX > 0) {
centerX += speedX;
}
.
these r changes which i made to move robot back and add a scrolling backstep background.....
.
atlast i m again thanking ur team for the tutorials.

Reply
Kishore
1/18/2017 08:54:24 am

Hi friend,
Thank you for this website and very useful. I have done coding and all requirements till adding sprites . There are no errors but still background and robot is not visible on the output

Reply



Leave a Reply.

    Author

    James Cho is the lead developer at Kilobolt Studios. He is a college student at Duke University and loves soccer, music, and sharing knowledge. 


© 2014 Kilobolt, LLC. All rights reserved.