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

2017/4/30

Can anyone see my Mistake, why doesn't it work?

1
2
Unknown6415 Unknown6415

2017/4/30

#
I have an actor, and I want him to usa a void from his upper class. It already worked once, but this time it doesn't although I made it exacly the same way.
public class Attacker extends Actor
{
    private int time1=0;
    private int time2=30;

    public  void Reload()
    {
        if (time1!=time2) {time1++;}
    }
}
and the subclass:
public class Gunner extends Attacker
{
     private int time1=0;
     private int time2=30;

     public void act()
     {
        Reload();
     }
}
But he doesn't do anything, why???
Yehuda Yehuda

2017/4/30

#
Unknown6415 wrote...
But he doesn't do anything, why???
You don't have any coding for any action. The only coding you have is calling the reload method every act cycle, all that does is increases the 'time1' variable until it's equal to 'time2'. What is supposed to happen (there is no indication of there being anything else that's supposed to happen - in code or words).
Unknown6415 Unknown6415

2017/4/30

#
It is supposed to increase the time1 by 1, but it doesn't !
Unknown6415 Unknown6415

2017/4/30

#
If I write " if (time1!=time2) {time1++;}" instead of "Reload();" it works, but why not with Reload(), it is a public void
danpost danpost

2017/4/30

#
Unknown6415 wrote...
It is supposed to increase the time1 by 1, but it doesn't !
Remove lines 3 and 4 from the Gunner class. Those fields are overshadowing the ones in the Attacker class.
Unknown6415 Unknown6415

2017/4/30

#
Yes, now it increases the time1 of the Attacker class, but I need it to increase the time1 of the Gunner class, I only wrote the lines 3 and 4 in the Attacker class because there would be a syntax error without them. Why can't the void Reload() use the integers of the current class?
Yehuda Yehuda

2017/4/30

#
A method in a class can use variables from its own class and from its parent classes. If you want to change the variables in Gunner then that's where the method should be. I don't see why you want to change the variables in Gunner from Attacker.
Unknown6415 Unknown6415

2017/4/30

#
I also tried "public void Reload(int time1, int time2)" in Attacker class and Reload(time1, time2); int Gunner class but it still doesn't work
Unknown6415 Unknown6415

2017/4/30

#
I wanted to write the method in the Attacker class so I can use it for all Attackers and not only for the Gunner, so this doesn't work?
Yehuda Yehuda

2017/5/1

#
If you want to have a method which will add one variable until it's equal to a second then you can do:
    /**
     * This method will increase the <code> currentTime</code> by one with each
     * call if it's less than <code>reloadTime</code>
     *
     * @param currentTime the amount of time that passed
     * @param reloadTime the amount of time before reloaded
     */
    public static void reload(int currentTime, int reloadTime) {
        if (currentTime < reloadTime) {
            currentTime++;
        }
    }
But this method seems kind of simple to have to be called from another class.
Unknown6415 Unknown6415

2017/5/1

#
Yehuda wrote...
If you want to have a method which will add one variable until it's equal to a second then you can do:
    /**
     * This method will increase the <code> currentTime</code> by one with each
     * call if it's less than <code>reloadTime</code>
     *
     * @param currentTime the amount of time that passed
     * @param reloadTime the amount of time before reloaded
     */
    public static void reload(int currentTime, int reloadTime) {
        if (currentTime < reloadTime) {
            currentTime++;
        }
    }
But this method seems kind of simple to have to be called from another class.
Doesn't work (and I also said that I tried this)
Unknown6415 Unknown6415

2017/5/1

#
So voids can't change the integers in the subclasses? Is the no possibility?
danpost danpost

2017/5/1

#
Unknown6415 wrote...
Yes, now it increases the time1 of the Attacker class, but I need it to increase the time1 of the Gunner class, I only wrote the lines 3 and 4 in the Attacker class because there would be a syntax error without them.
Then remove lines 3 and 4 of the Gunner class and change lines 3 and 4 of the Attacker class from 'private' to either 'protected' or 'public'.
Why can't the void Reload() use the integers of the current class?
As 'protected' or 'public', the fields in the Attacker class can be used in either of the Gunner or Attacker class.
danpost danpost

2017/5/1

#
Unknown6415 wrote...
So voids can't change the integers in the subclasses? Is the no possibility?
Primitive type parameters (like 'int') only pass the values. As an Integer object, the value is contained and any change to the value within the object does not change the object.
Unknown6415 Unknown6415

2017/5/1

#
But I have many Attackers, not only the Gunner, who should use this method, and they should have their own time1 and time2.
There are more replies on the next page.
1
2