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

2015/11/6

creating a class to hold shared variables

davemib123 davemib123

2015/11/6

#
Hi all, I have some classes (hero, enemy, floating platform) that share variables (i.e. speed, life, image_directory) how do I go about creating a class where they can share the variables and some specific methods (moveLeft, moveRight) without creating a class under Actor. Not really sure if its possible. Hope that makes sense?
danpost danpost

2015/11/6

#
Just have the specific actor classes subclass, or extend, an intermediate class that extends Actor. An instance of all non-static fields declared in the intermediate class will be given to each object created from any class that falls under its type (or, from that class itself or any class that subclasses it). If the value of the field is to be at all times the same for all said objects, then the field should be made a class field (instead of an instance field) by declaring it 'static'. Methods should be written in a generic way so that an object from any of its subclasses can utilize them.
davemib123 davemib123

2015/11/6

#
ok, I thought about reading up on abstract classes ..
danpost danpost

2015/11/6

#
davemib123 wrote...
ok, I thought about reading up on abstract classes ..
It could very will be made an abstract class. This would prevent an object to be created directly from the intermediate class. This will not alter the class in any way, otherwise.
danpost danpost

2015/11/6

#
I guess I missed the part about you wanting NOT to create a class under Actor. I would think you could create classes that extend Object that would perform basic things like 'position/movement/rotation' control or 'animation' control (I have an example of this, precisely, here in my Animation Support Class scenario.
BearCarlson25 BearCarlson25

2015/11/6

#
danpost you're such a nerd i can't believe how such a nerd can give so much advice
BearCarlson25 BearCarlson25

2015/11/6

#
stfu nerd
davemib123 davemib123

2015/11/6

#
danpost wrote...
I guess I missed the part about you wanting NOT to create a class under Actor. I would think you could create classes that extend Object that would perform basic things like 'position/movement/rotation' control or 'animation' control (I have an example of this, precisely, here in my Animation Support Class scenario.
thanks for the info DanPost. I've checked the scenario you pointed out and it had what I wanted to know.
davemib123 davemib123

2015/11/6

#
Just a quick question relating to the above. I have this class:
1
2
3
4
5
public class SharedMembers 
{
    public int speed;
 
}
How can I add into it methods which relate to Actors like this one:
1
2
3
4
public void moveRight()
    {
        setLocation (getX() + speed, getY());
    }
getX() and getY() are part of the Actor class which I want to put into this abstract class.
danpost danpost

2015/11/6

#
davemib123 wrote...
I have this class: < Code Omitted > How can I add into it methods which relate to Actors like this one: < Code Omitted > getX() and getY() are part of the Actor class which I want to put into this abstract class.
You can have a field to hold the actor (like I did in the Animation class you saw), or you can pass it directly to the method:
1
2
3
4
public void moveRight(Actor actor)
{
    actor.setLocation(actor.getX()+speed, actor.getY());
}
davemib123 davemib123

2015/11/6

#
I get "can not find symbol - Actor".
1
2
3
4
5
6
7
8
9
10
public class SharedMembers 
{
    public int speed;
 
    public void moveRight(Actor actor)
    {
        actor.setLocation(actor.getX()+speed, actor.getY());
    }
 
}
danpost danpost

2015/11/6

#
davemib123 wrote...
I get "can not find symbol - Actor".
Add an import statement:
1
import greenfoot.Actor;
or change the method declaration to this:
1
public void moveRight(greenfoot.Actor actor)
davemib123 davemib123

2015/11/6

#
great. got it working :). thanks again danpost!
davemib123 davemib123

2015/11/8

#
how do I get something like this in the class:
1
public int objectWidth = getImage().getWidth();
danpost danpost

2015/11/8

#
davemib123 wrote...
how do I get something like this in the class:
1
public int objectWidth = getImage().getWidth();
I kind of doubt that you want that in your SharedMembers class. The image of an object could change. Also, as you coded it, it could not be used in your SharedMembers class because there is no 'getImage' method in that class (unless you for some reason added one). It would be coded more like the following in that class:
1
int objectWidth = actor.getImage().getWidth();
and used only when needed -- not as a field in the class (as you seem to want to make it). Of course, this is assuming you set it up like I set up the Animation class -- where a field within the SharedMembers class contains the object that created it.
You need to login to post a reply.