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

2017/2/28

How to call int from another class?

1
2
joshuadswa joshuadswa

2017/2/28

#
Hello :) I need help with my project .-. im trying to build something like moba+tower defense but i have problem how to call / get hp from another class, so each class have its own hp, and damage i tried this before just example Minion1 class public int hp = 300; public int damage = 25; Hero1.class //i need help here , how to call that public int hp and damage .-. i dont quite understand in there, Thx a lot before, and sorry for my english ._. im still trying to learn it >.<
danpost danpost

2017/2/28

#
It is not the class that has the int values -- it is the objects (minion objects) created from that class that each has an 'hp' and 'damage' value. So, you need a reference to a particular minion. So, the question is, which minion are you trying to acquire the values from?
Super_Hippo Super_Hippo

2017/2/28

#
You usually wouldn't 'call' these ints. You would call a method on the enemy to damage it when attacking.
//in Hero1's act method
Minion1 minion = (Minion1) getOneIntersectingObject(Minion1.class);
if (minion != null)
{
    minion.loseLife(damage); //damage should be the damage of the Hero
}
//in Minion1
public void loseLife(int dmg)
{
    hp -= dmg;
    if (hp <= 0)
    {
        getWorld().removeObject(this);
    }
}
The minion can itself have the same code to damage the Hero or the Hero could get the damage of the minion and deals it to itself.
joshuadswa joshuadswa

2017/2/28

#
@danpost i only have 1 minion class here, but if what you mean in here is minion's order, then it will be number 1 ._., , also Supper_Hippo thx :) i will try it >.<
joshuadswa joshuadswa

2017/2/28

#
wait i dont really understand about the loseLife(), can you explain it to me? how to get that method
Super_Hippo Super_Hippo

2017/2/28

#
Remember: a class is not the same as an object. Line 7 calls the method on the minion object which is touching the hero.
joshuadswa joshuadswa

2017/2/28

#
so something like this? 1.Minion1 have a public void loseLife(int dmg) >>then 2.The hero call minion1 method in minion.loseLife(damage)
Super_Hippo Super_Hippo

2017/2/28

#
Yes I was hoping that line 1 in both code snippets would make that clear.
joshuadswa joshuadswa

2017/2/28

#
okay thx a lot :D ^^ gonna try it right away :)
joshuadswa joshuadswa

2017/2/28

#
Sorry super_hippo, i tried and it doesnt work .-. is there's any other method? @_@ my god looks like my brain got wrecked
Super_Hippo Super_Hippo

2017/2/28

#
Show how you tried to implement it. What does "doesn't work" mean: Does it compile? What is happening?
joshuadswa joshuadswa

2017/3/1

#
it compile, but the minion immediately got removed after intersecting .-. i tried to put int hp as 300, so if it intersecting, hp will be reduced by 35 but not working xD
Nosson1459 Nosson1459

2017/3/1

#
Show what you've tried.
joshuadswa joshuadswa

2017/3/1

#
for hero class public void minion() { Minion1 minion = (Minion1) getOneIntersectingObject(Minion1.class); if (minion!= null) { minion.kurangHp(35); } } for minion class int hp = 300; public void kurangHp(int dmg)//i tried to change the int with number, not working .-. { hp -= dmg; if (hp <= 0) { getWorld().removeObject(this); } }
Super_Hippo Super_Hippo

2017/3/1

#
Right now, the minion will die after the 9th hit of the Hero. (Which will happen if the intersection lasts about a 6th of a second.) If you change 35 to 1, it will take about 5 seconds. If you want the hero to deal damage just every two seconds (for example) or you only want him to do damage when the intersection "starts" and not as long as he intersects, you have to change the code to your needs.
There are more replies on the next page.
1
2