Jump to content

  • Log In with Google      Sign In   
  • Create Account

Enemy only shoots once. Help


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Gearslayer360   Members   -  Reputation: 170

Like
0Likes
Like

Posted 17 October 2012 - 09:50 PM

Ok so my problem is in my game I have two enemies. One of them is human controlled and can shoot at will. The AI however, seems to be a little broken. In my shoot method in this class, which is the enemy class, I try to use the system for shooting that I used for the player and I call the shoot method in run so it continuously updates. I can get one shot off right at the beginning, but I can't shoot any more after that. I've tried everything I know...and now I'm basically stuck. I want to see if I can do more with this game but for weeks I've struggled to no avail trying to figure this out. Help please? It would be very appreciated. I can include the other classes if needed just ask. So maybe you can run the game to see what I mean.



[source lang="java"]package game;import java.awt.Color;import java.awt.Graphics;import java.awt.Rectangle;import java.util.Random;public class Enemy implements Runnable{ int x; int y; int xDirection; int yDirection; int health; int p1Score; int pHealth = 10; int bx; int by; boolean shot = false; public static boolean readyToFire; Rectangle enemy; Rectangle bullet; Player p1 = new Player(290, 15, 10); public Enemy(int x, int y, int health) { p1Score = 0; this.x = x; this.y = y; this.health = health; Random r = new Random(); int rDir = r.nextInt(1); if(rDir == 0) { rDir--; setXDirection(rDir); } int yrDir = r.nextInt(1); /*if(yrDir == 0) { yrDir--; setYDirection(yrDir); } */ enemy = new Rectangle(this.x, this.y, 100, 30); } public void draw(Graphics g) { //draw the enemy initially g.setColor(Color.BLUE); g.fillRect(enemy.x, enemy.y, enemy.width+5, enemy.height); g.fillRect(enemy.x+45, enemy.y+25, 15, 30); if(shot) { g.setColor(Color.BLUE); g.fillRect(bullet.x+5, bullet.y-10, bullet.width, bullet.height); } //if the enemy dies remove it //Need to delete the current one and make a new enemy if(health <= 0) { enemy = new Rectangle(x, y, 100, 30); health = 10 ; } } /* * Always seems to create the enemy bullet in the same place *...need to figure a way for it to always fire * if within range of the player */ public void shoot() { readyToFire = true; if(!shot && enemy.x >= p1.x) { while(readyToFire) { shot = true; readyToFire = false; bullet = new Rectangle (enemy.x+45, enemy.y+65); by = enemy.y+65; bx = enemy.x+45; bullet = new Rectangle(bx, by, 3 , 15); System.out.println("Hi"); if(bullet.y >= 595) { bullet = new Rectangle(0, 0, 0, 0); shot = false; readyToFire = true; } } } bullet.y+=3; } public void collision() { //Collision between the bullet and the enemy. //Checks first if the player bullet is null //Checks if the bounds of bullet and enemy intersect if(enemy.getBounds().intersects(p1.player)) { setYDirection(-1); } if(p1.bullet != null && enemy.getBounds().contains(p1.bullet.getBounds())) { //setYDirection(-1); p1Score+=100; health-=1; p1.bullet = new Rectangle(0, 0, 0, 0); } } public void move() { enemy.x+=xDirection; enemy.y+=yDirection; //Checks if the enemy hits the left side of the screen //if so reverse the direction to the right if(enemy.x <= 0) { setXDirection(1); } //Checks if the enemy hits the right side of the screen //if so reverse the direction to the left if(enemy.x >= 700) { setXDirection(-1); } //Checks if the enemy hits the top of the screen //if so reverse the direction to the bottom if(enemy.y <= 30) { setYDirection(1); } //Checks if the enemy hits the bottom of the screen //if so reverse the direction to the top if(enemy.y >= 570) { setYDirection(-1); } } public void run() { try { while(true) { move(); shoot(); collision(); Thread.sleep(3); } } catch(Exception e){System.err.println(e.getMessage());} } public void setXDirection(int direction) { xDirection = direction; } public void setYDirection(int direction) { yDirection = direction; } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public int getHealth() { return health; } public void setHealth(int health) { this.health = health; } public int getP1Score() { return p1Score; } public void setP1Score(int score) { p1Score = score; } public Player getP1() { return p1; } public void setP1(Player p1) { this.p1 = p1; } public Rectangle getEnemy() { return enemy; } public void setEnemy(Rectangle enemy) { this.enemy = enemy; } public int getXDirection() { return xDirection; } public int getYDirection() { return yDirection; } public int getPHealth() { return pHealth; } public void setPHealth(int health) { pHealth = health; }}[/source]

Edited by Gearslayer360, 17 October 2012 - 09:51 PM.


Sponsor:

#2 Freya   Members   -  Reputation: 801

Like
0Likes
Like

Posted 18 October 2012 - 01:56 AM

What are the values of bx and by at line 78 when the enemy shoots?
Working on a game "Burnt islands".
Dev blog       
"Burnt islands" on Indie DB

#3 Scorpie   Members   -  Reputation: 199

Like
4Likes
Like

Posted 18 October 2012 - 04:34 AM

First of all, i notice that you use a lot of " = " operators in your if-statements (line 69, 120, 127), this is wrong and should be replaced with the compare operator " == " like you use in other places (line 36, 44). Check the rest of your code for this mistake also because when you use it here, chances are you use it in other places too.

I don't have time to check everything in your code at the moment (@work) but there might be some more problems, for example comment in lines 67,68 state:
//if the enemy dies remove it
//Need to delete the current one and make a new enemy
There is no enemy deletion going on there so i have no idea why it is there

Anyways, for your shooting problem:
the scope of the while-loop in this code is wrong:
if(health = p1.x)
	    {
		    while(readyToFire)
		    {
			    shot = true;
			    readyToFire = false;
			    bullet = new Rectangle (enemy.x+45, enemy.y+65);
			    by = enemy.y+65;
			    bx = enemy.x+45;
			    bullet = new Rectangle(bx, by, 3 , 15);
			    System.out.println("Hi");
			
		    if(bullet.y >= 595)
		    {
			    bullet = new Rectangle(0, 0, 0, 0);
			    shot = false;
			    readyToFire = true;
				
		    }
	    }
	    }

You need to move the if-statement outside the while in order for it to be checked (after all, you set readyToFire to false inside the while)
Change it to:
if(health == p1.x)
	    {
		    while(readyToFire)
		    {
			    shot = true;
			    readyToFire = false;
			    bullet = new Rectangle (enemy.x+45, enemy.y+65);
			    by = enemy.y+65;
			    bx = enemy.x+45;
			    bullet = new Rectangle(bx, by, 3 , 15);
			    System.out.println("Hi");
			 }
		    if(bullet.y >= 595)
		    {
			    bullet = new Rectangle(0, 0, 0, 0);
			    shot = false;
			    readyToFire = true;
				
		    }
	    }

ps. i changed the " = " to " == " in that code but i have no idea what is going on there so you might need to move the pieces around some more there are some weird things going on in your code (why are you comparing health with the x coordinate of the player??? )

#4 Gearslayer360   Members   -  Reputation: 170

Like
0Likes
Like

Posted 18 October 2012 - 12:41 PM

I'm sorry the box to where I copied my code didn't seem to pick up my > and < signs...that's what those single = signs were. Also it seems to have left out huge chunks.....I'm very sorry..but that wasn't my code. Also @Freya the values of bx and by are where the bullet is drawn. It is the x and y position of the enemy plus a little because they are basically tanks made of two rectangles and I want the bullet to look like it comes out of their cannon/gun. Also I just as of right now managed to get an odd version of this working. Here's the new code. I can include the other two classes as well if you want to see how it looks when it runs.

package game;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Random;

public class Enemy implements Runnable
{
int x;
int y;
int xDirection;
int yDirection;
int health;
int p1Score;
int pHealth = 10;
int bx;
int by;
boolean shot = false;
public static boolean readyToFire;

Rectangle enemy;
Rectangle bullet;
Player p1 = new Player(290, 15, 10);

public Enemy(int x, int y, int health)
{
p1Score = 0;
this.x = x;
this.y = y;
this.health = health;

Random r = new Random();
int rDir = r.nextInt(1);

if(rDir == 0)
{
rDir--;
setXDirection(rDir);
}

int yrDir = r.nextInt(1);

/*if(yrDir == 0)
{
yrDir--;
setYDirection(yrDir);
}
*/

enemy = new Rectangle(this.x, this.y, 100, 30);
}

public void draw(Graphics g)
{
//draw the enemy initially
g.setColor(Color.BLUE);
g.fillRect(enemy.x, enemy.y, enemy.width+5, enemy.height);
g.fillRect(enemy.x+45, enemy.y+25, 15, 30);

if(shot)
{
g.setColor(Color.BLUE);
g.fillRect(bullet.x+5, bullet.y-10, bullet.width, bullet.height);
}

//if the enemy dies remove it
//Need to delete the current one and make a new enemy
if(health <= 0)
{
enemy = new Rectangle(x, y, 100, 30);
health = 10 ;
}
}

/*
* Always seems to create the enemy bullet in the same place
*...need to figure a way for it to always fire
* if within range of the player
*/
public void shoot()
{
readyToFire = true;
if(!shot && enemy.x >= p1.x)
{
if(readyToFire)
{
shot = true;
readyToFire = false;
bullet = new Rectangle (enemy.x+45, enemy.y+65);
by = enemy.y+65;
bx = enemy.x+45;
bullet = new Rectangle(bx, by, 3 , 15);
System.out.println("Hi");
}



}

if(bullet.y >= 595)
{
bullet = new Rectangle(0, 0, 0, 0);
shot = false;
readyToFire = true;

}
bullet.y+=5;
}

public void collision()
{
//Collision between the bullet and the enemy.
//Checks first if the player bullet is null
//Checks if the bounds of bullet and enemy intersect

if(enemy.getBounds().intersects(p1.player))
{
setYDirection(-1);
}

if(p1.bullet != null && enemy.getBounds().contains(p1.bullet.getBounds()))
{
//setYDirection(-1);
p1Score+=100;
health-=1;
p1.bullet = new Rectangle(0, 0, 0, 0);
}
}

public void move()
{
enemy.x+=xDirection;
enemy.y+=yDirection;

//Checks if the enemy hits the left side of the screen
//if so reverse the direction to the right
if(enemy.x <= 0)
{
setXDirection(1);
}

//Checks if the enemy hits the right side of the screen
//if so reverse the direction to the left
if(enemy.x >= 700)
{
setXDirection(-1);
}

//Checks if the enemy hits the top of the screen
//if so reverse the direction to the bottom
if(enemy.y <= 30)
{
setYDirection(1);

}

//Checks if the enemy hits the bottom of the screen
//if so reverse the direction to the top
if(enemy.y >= 570)
{
setYDirection(-1);

}
}

public void run()
{
try
{
while(true)
{
move();
shoot();
collision();
Thread.sleep(3);
}
}
catch(Exception e){System.err.println(e.getMessage());}
}

public void setXDirection(int direction)
{
xDirection = direction;
}

public void setYDirection(int direction)
{
yDirection = direction;
}

public int getX()
{
return x;
}

public void setX(int x)
{
this.x = x;
}

public int getY()
{
return y;
}

public void setY(int y)
{
this.y = y;
}

public int getHealth()
{
return health;
}

public void setHealth(int health)
{
this.health = health;
}

public int getP1Score()
{
return p1Score;
}

public void setP1Score(int score)
{
p1Score = score;
}

public Player getP1()
{
return p1;
}

public void setP1(Player p1)
{
this.p1 = p1;
}

public Rectangle getEnemy()
{
return enemy;
}

public void setEnemy(Rectangle enemy)
{
this.enemy = enemy;
}

public int getXDirection()
{
return xDirection;
}

public int getYDirection()
{
return yDirection;
}

public int getPHealth()
{
return pHealth;
}

public void setPHealth(int health)
{
pHealth = health;
}
}[/source]

Edited by Gearslayer360, 18 October 2012 - 01:42 PM.


#5 Gearslayer360   Members   -  Reputation: 170

Like
2Likes
Like

Posted 18 October 2012 - 01:43 PM

I seem to have fixed my issues. Thank you for the help.

#6 Scorpie   Members   -  Reputation: 199

Like
0Likes
Like

Posted 19 October 2012 - 10:44 AM

Ah, it seems like there are quite some issues with the [ source ] tags, i always type [ code ] which seems to work fine, maybe a bit less fancy but it get's the job done :)

Good to hear that you fixed the problems!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS