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

2014/10/31

messing around with actors again

Alwin_Gerrits Alwin_Gerrits

2014/10/31

#
Hey guys, I want to thank you for helping me out lately. I've been able to get quite a bit of coding for my project done thanks to you. Now I'm running into a new problem and I'm not even sure how to discribe it... This is my code:
    public void EnemyWalker(int x, int y)
    {
        list.add(new DESERTWALKER());
        coor.add(x);
        coor.add(y);
        //addObject(new DESERTWALKER(), x, y); 
    }
    
    public void EnemyThrower(int x, int y)
    {
        list.add(new DESERTTHROWER());
        coor.add(x);
        coor.add(y);
    }
    
    public void addEnemies()
    {
        int counter;
        
        for(counter=0; counter<list.size(); counter++)
        {
            Actor help = list.get(counter); //which should make 'help' into 'new DESERTWALKER' or 'new DESERTTHROWER'
            
            if(help.die) //for some reason this doesn't work. it doesn't know die
            addObject(help, coor.get(counter*2), coor.get(counter*2+1)); //0=x, 1=y, 2=x, 3=y and so on
        }
    }
And this is the code of 'DESERTWALKER'
public class DESERTWALKER extends WALKER
{
    public static boolean die=false;
    
    boolean goingLeft=true;
    int dead=0;
    int speed=5;
    
    public void act() 
    {
        Move(speed);
        Die();
    }
}
So my problem is in 'addEnemies'. Somehow it doesn't know help.die, however the code does place the enemies correctly and they use the code they should. Any ideas on the source of the problem or a way to fix it? Please advice.
Super_Hippo Super_Hippo

2014/10/31

#
If you only have one 'DESERTWALKER' object, then you could just use 'DESERTWALKER.die'. If not, it shouldn't be saved as 'static' and you need to cast the Actor 'help' to an DESERTWALKER or DESERTTHROWER object. What exactly do you want to do with it? Btw, it is still helpful to stick with the java notation. So method names start with a lowercase letter, class names start with uppercase and are not all uppercase and so on.
danpost danpost

2014/10/31

#
First, how is your 'list' List object defined in your World subclass? Next, what is the code for your WALKER class? Finally, does your 'list' List object contain other Actor sub-types other than WALKERs?
davmac davmac

2014/10/31

#
The problem lines:
        Actor help = list.get(counter); //which should make 'help' into 'new DESERTWALKER' or 'new DESERTTHROWER'  
          
        if(help.die) //for some reason this doesn't work. it doesn't know die  
It doesn't know die, because you declared 'help' as a variable of type Actor, and there is no 'die' in Actor. I'd like to ask: can any type of WALKER die or only DESERTWALKER? If both types of WALKER can die, then the 'die' variable should be declared in the WALKER class and not the DESERTWALKER class. Then you just need a cast:
        WALKER help = (WALKER) list.get(counter); //which should make 'help' into 'new DESERTWALKER' or 'new DESERTTHROWER' 
Perhaps your list can be declared as a list of WALKER, rather than a list of Actor, so that you remove the need for the cast. However, you've declared die as a static variable in DEATHWALKER. Is that really what you want? Do you understand that this means each DEATHWALKER will share a single common value for the 'die' variable? If so, then you could possibly change your problem line to:
 if(DEATHWALKER.die) //for some reason this doesn't work. it doesn't know die 
 // well it will work now! :)
As a general rule, you should try to avoid using 'static'. It is usually an indication of bad design.
Alwin_Gerrits Alwin_Gerrits

2014/10/31

#
Actually, I'm using the first part of the code within a subclass of World to create the setup for a level, so changing it to a list of WALKER probably isnt the best idea. DESERTWALKER is indeed part of subclass WALKER and all of them should be able to die, though changing this shouldn't help much...... Right. I just changed the code to WALKER and somehow it now works perfectly. Yet I don't know why... ow well guess it's part of beïng a beginning programmer.... Anyway, thanks davmac. Seems like you were spoton. (may have had something to do with the (WALKER) you added)
Alwin_Gerrits Alwin_Gerrits

2014/10/31

#
I do have one question by the way. If using 'static' should be avoided then do you mean using the function of 'static' is bad or just the 'static' method is bad? Because if you shouldn't use static but something ells then what should I use instead? Or if I shouldn't be using 'static' at all does that mean I shouldn't be making variables that have the properties of a 'static' ?
danpost danpost

2014/10/31

#
I think what davmac is saying is that using a 'static' field that holds information about a specific object is bad design. If the value held by the field is specific to one object of the class only, then the field should NOT be a 'static' one. If the field is shared by all objects created from the class, then 'static' would be appropriate. Think of it this way, if you had two objects, both created from the class, would each object have its own value for the particular field or would the value always pertain to both. Only if the value always pertained to both (or all objects of the class) would you then use 'static'.
Alwin_Gerrits Alwin_Gerrits

2014/10/31

#
I think I get it. So if I'm using the static variable not only in one class, but allso in a number of subclasses then it's appropiate. But if i'm changing it from other classes I should make a method to be called from the other class instead of making it a static. In short: Don't use statics as a solution for everything. Is that getting close to what you mean?
danpost danpost

2014/10/31

#
@Alwin, no. It does not deal with classes and subclasses; it deals with a class and the objects created from that class. For example, let us say we had a Gun class that creates a Gun object that can shoot Ball objects into the air at different angles; and the Ball objects will bounce around and eventually come to a stop. Every ball object will have the same initial speed when shot by the gun; but, each will have its own current speed and direction. The initial speed can be stored in a static field because its value is the same for each and every ball; whereas the current speed and direction fields must remain non-static. Values that describe the current state of an object should be non-static. General values that do not describe current state can be.
Alwin_Gerrits Alwin_Gerrits

2014/10/31

#
K, I think I get it now. Thanks danpost.
davmac davmac

2014/11/1

#
Right. I just changed the code to WALKER and somehow it now works perfectly. Yet I don't know why...
I tried to explain this. When you declare a variable like this:
        Actor help = list.get(counter);
You are saying that there is a name, 'help', which is a variable of type 'Actor'. That is, it holds a reference to an Actor (which may be an instance of a subclass of Actor). When you do this:
if(help.die)
You are trying to access the member called 'die' within the type of the object in 'help'. In this case you have declared 'help' to be of type Actor. There is no 'die' in Actor, so you get an error. Even though the variable might hold an instance of a subclass which does have a 'die' member, the compiler cannot resolve the 'die' member during compilation, because during compilation the variable doesn't really hold anything. So, you get an error.
Alwin_Gerrits Alwin_Gerrits

2014/11/1

#
Ow dang, so that's what's up. K, thanks davmac. I'm prety sure I get what caused my problem now.
You need to login to post a reply.