This site requires JavaScript, please enable it in your browser!
Greenfoot back
Roshan123
Roshan123 wrote ...

2021/1/16

Getting variable frm another class

1
2
Roshan123 Roshan123

2021/1/16

#
How to get variable frm another class Ex:- Tank1 and cannon1 I want to get health variable frm tank1 class but how will i make a reference to tank1???
danpost danpost

2021/1/16

#
Roshan123 wrote...
How to get variable frm another class Ex:- Tank1 and cannon1 I want to get health variable frm tank1 class but how will i make a reference to tank1???
What is cannnon1 and how does it relate to tank1? and, when is it necessary for cannnon1 to get the health of tank1?
Roshan123 Roshan123

2021/1/16

#
Both r subclasses of actor class how does it relate to tank class? Cannon class uses tank class location It moves and turns with tank body to destroy other classes If tank1.health is 150 then it will setRotation(tank1.getRotation()) If tank1.health is 100 then it will turnToward(xyz.class) If tank1.health is 50 then it will shoot bullet
Roshan123 Roshan123

2021/1/16

#
Tank class And before looking at the code remove the public static in line 6. I was trying to improve but i got some problems
danpost danpost

2021/1/16

#
Roshan123 wrote...
Both r subclasses of actor class how does it relate to tank class? Cannon class uses tank class location It moves and turns with tank body to destroy other classes If tank1.health is 150 then it will setRotation(tank1.getRotation()) If tank1.health is 100 then it will turnToward(xyz.class) If tank1.health is 50 then it will shoot bullet
So:
how will i make a reference to tank1???
EnemyTank tank1 = (EnemyTank) gettOneIntersectingObject(EnemyTank.class);
(in cannon1 class).
Roshan123 Roshan123

2021/1/17

#
Its working but why this one was giving me Null pointer exception???
Roshan123 Roshan123

2021/1/17

#
EnemyTank tank1 = (EnemyTank) gettOneIntersectingObject(EnemyTank.class); EnemyTank tank1 =gettOneIntersectingObject(EnemyTank.class); What is the difference
danpost danpost

2021/1/17

#
Roshan123 wrote...
EnemyTank tank1 = (EnemyTank) gettOneIntersectingObject(EnemyTank.class); EnemyTank tank1 =gettOneIntersectingObject(EnemyTank.class); What is the difference
The difference is that the method returns an Actor object (or null). It might be an EnemyTank object, but the calling method will only see it as an Actor object. The "(EnemyTank)" preceding the method tells the calling method that it will indeed be an Actor object of that type, which will allow it to be assigned to the tank1 variable that must, by how it is defined, only be of that type. For clarity, the reference must be of that type, else health, which is defined in that class, will not be found.
Roshan123 Roshan123

2021/1/18

#
Tank u!!!
Roshan123 Roshan123

2021/1/24

#
danpost wrote...
EnemyTank tank1 = (EnemyTank) gettOneIntersectingObject(EnemyTank.class);
(in cannon1 class).
If i will define heath of tank1 as static then it will not give null pointer exception else it gives me null pointer exception I m feeling proud that I realised it today only
danpost danpost

2021/1/24

#
Roshan123 wrote...
If i will define heath of tank1 as static then it will not give null pointer exception else it gives me null pointer exception
Health is a property of a tank1 object and therefore should NOT be static. The line of code given should be followed by:
if (tank1 != null) ...
Roshan123 Roshan123

2021/1/25

#
danpost wrote...
Health is a property of a tank1 object and therefore should NOT be static. The line of code given should be followed by:
if (tank1 != null) ...
Ohh sorry, I forgot to mention that while it gave me null pointer exception, i wrote if(tank1!=null) but the code inside it was not working, so i removed it ( if(tank1!=null) ) and and defined health as static and then it worked but i don't want health to be static!
danpost danpost

2021/1/25

#
Roshan123 wrote...
i wrote if(tank1!=null) but the code inside it was not working
What was the "code inside it"?
Roshan123 Roshan123

2021/1/25

#
I used set image But in between i found the best way to fix it (not sure...wether it will work or not) The only thing now i need to do is to implement it
Roshan123 Roshan123

2021/1/25

#
//ABOUT Text CLASS (the class in which i m using tank1 variable and getting null pointer exception) if(tank1!=null && tank1.health!=0) setImage(new GreenfootImage("mik"....rest of the code); else setImage(new GreenfootImage (null...rest...); //Main motive of using it --- the tank is added after few seconds to world when its health is 0 So if its health is 0 then it should not show any text and when it will be added, then its health will be 150 and obviously it should update the image //ABOUT Tank1 CLASS But i found a best way to fix it I will add a remove statement before the tank1 class remove statement to remove text class And then i will write something like this
protected void addedToWorld(World w)
{
Text text=new Text()
w.addObject(text,44,33);
}
Note that its my 1st time to use addedToWorld method, so it can be wrong also If its wrong then plz rectify my mistake
There are more replies on the next page.
1
2