how to send variable to different class by using extends
problem is that picture is not being printed on screen from enemy_1 class.
i have a class enemy where i am setting up main enemy stuff. for ex collision and move bc all enemy will have the same move and collision.
than i want to create another class called enemy_1 where i will upload different images.
my goal is to set variable from Enemy class to Enemy_1 class.
first here is a Enemy class
Code :
public class Enemy
{
private int x;
private int y;
private int width = 30;
private int height = 40;
public Enemy(int ix, int iy)
{
x = ix;
y = iy;
}
public int getX()
{ return x; }
public void setX(int value)
{ this.x = value; }
public int getY()
{ return y; }
public void setY(int value)
{ this.y = value; }
public int getWIDTH()
{ return width; }
public void setWIDTH(int value)
{ this.width = value; }
public int getHEIGHT()
{ return height; }
public void setHEIGHT(int value)
{ this.height = value; }
/*** update method ****/
/*** player move ***/
public void ENEMY_MOVE(Main m, Sprite_Sheet s)
{
Random r = new Random();
//x -= 1;
if(x+width < 0)
{
x = m.getWidth() + r.nextInt(200);
}
}
/*** player + enemy collion ***/
public void PLAYER_ENEMY_COLLISION(Main m, Player p, Enemy e)
{
int playerX = p.getX();
int playerY = p.getY();
int playerW = p.getWIDTH();
int playerH = p.getHEIGHT();
if(playerX + playerW >= x && playerX <= x + width)
{
if(playerY+playerH >= y && playerY <= y+height)
{
if (playerX <= x) //player on left
{
//player has to be touching enemy and player has to be on left of enemy
p.setX(x - playerW);
}
else if(playerX >= x)//player on right
{
//player has to be touch enemy and player has to be on right of enemy
p.setX(x + width);
}
}
}
}/*** end of PlAYER_ENEMY_COLLISION METHOD ***/
/*** paint method ***/
public void paint(Graphics g, Enemy e)
{
}/*** paint method ***/
}
now i am extending the main enemy class.
Code :
public class Enemy_1 extends Enemy
{
private static ImageIcon enemy_image = new ImageIcon("Image/enemy/ch2.png");
public Enemy_1(int ix, int iy)
{
super(ix,iy);
}
/*** get/set methods ***/
public static Image getEIMAGE()
{ return enemy_image.getImage(); }
@Override
public void enemy_action(Player p, Score s)
{
}
@Override
public void paint(Graphics g, Enemy e)
{
g.drawImage(this.getEIMAGE(), e.getX(), e.getY(), e.getWIDTH(), e.getHEIGHT(), null);
super.paint(g, e); //call enemy paint method
}
}
when i put every thing in enemy class than it works fine but when i create two class it doesnt work.
problem is in enemy_1 class. when i run this picture is not being printed. i am not sure but i think i am not sure paint method in enemy_1 class.
paint from enemy
Code :
/*** paint method ***/
public void paint(Graphics g, Enemy e)
{
}/*** paint method ***/
paint from enemy_1
Code :
@Override
public void paint(Graphics g, Enemy e)
{
g.drawImage(this.getEIMAGE(), e.getX(), e.getY(), e.getWIDTH(), e.getHEIGHT(), null);
super.paint(g, e); //call enemy paint method
}
let me now if you need more information
Re: how to send variable to different class by using extends
Quote:
how to send variable to different class by using extends
The keyword extends does not have the ability to send anything.
When a class extends another class, say B extends A for example, it is like doing a copy of everything inside class A and a paste into class B. All variables and methods are completely visible in B and B is everything A ever becomes, plus what is added in B. So if you are trying to pass a value from Enemy to Enemy_1, it is already there.
Quote:
when i run this picture is not being printed.
This is a symptom, not a problem. Many problems can cause this symptom. Do some troubleshooting to see what the problem is. Use printlns to verify the values of variables as well as to verify when certian methods are being called, if they ever do get called.
Quote:
i am not sure but i think i am not sure
..No one else is sure either. Perhaps some proofreading and a well thought out questin the future will make a reply easier.
Quote:
let me now if you need more information
You made some statements and assumptions about what you think is wrong, but you never really asked a question in the entire post. What reply do you expect from this? A complete working program? What if I just typed "Mickey Mouse". That is as likely to fit the unasked question as well as any other phrase I can think of. Please read "How to ask questions the smart way" and your experience on this site will be greatly improved.
Re: how to send variable to different class by using extends
I see so lets say if i have 2 class. A and B:
class A
private int ax = 2;
class B extends A
//here B has every thing from A also B
//and i want to get the value of ax here. than i can just call ax? ex:
ax = 4;
//also i think i might have to change private to protected or get rid of private
Re: how to send variable to different class by using extends
..and what happened when you tried it?
Re: how to send variable to different class by using extends
got it working now. i forgot to call class b in main.
thanks