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

2015/9/23

Need general advice for calling/initializing classes

oscarreks oscarreks

2015/9/23

#
I vaguely understand that to use a class in another class, I have to write it in front of the method I want to use. Right now I'm Trying to get the rotation of a class, Turtle3, so I can orient the rotation of the bullets he shoots, which I have set up in the constructor class of class "bullets".
1
2
3
4
5
6
public bullets()
    {
        Turtle3 turtle = Turtle3();
        setRotation(turtle.getRotation());
        move(7);
    }
So again, I vaguely understand that I need to call or initialize the turtle class in the bullet class in order to use it's rotation. But randomly throwing together class names that look like they should work hasn't done any good. Could someone more knowledgeable with this tell me the reason I need to call a class in the first place? I think I'd be better prepared for this in the future if someone could also explain good habits to have, in terms of writing the lines for calling classes, and where to locate the lines. Hope someone can help, thanks in advance :)
davmac davmac

2015/9/23

#
I vaguely understand that to use a class in another class, I have to write it in front of the method I want to use.
You say "class" but I think you mean object. As in, you want to access an object (of class Turtle3) from another class. Looking at your code:
1
2
3
4
5
6
public bullets()
    {
        Turtle3 turtle = Turtle3();  // line 1
        setRotation(turtle.getRotation()); // line 2
        move(7);
    }
The problem here is that you create a new Turtle3 (line 1) and then ask for its rotation (line 2). The rotation of the new object will most likely always be 0. The actual object whose rotation you really want already exists independently (and has its own rotation). Because there could be multiple Turtle3 objects, you need to identify which one you want the rotation of. Creating a new one is pointless, as I have explained. You need a reference to the existing object. If there is only one Turtle3 object in the world (or if there is more than one but you don't care which one you get), you can get it using this code:
1
Turtle3 turtle = (Turtle3) getWorld().getObjects(Turtle3.class).get(0); // replaces line 1
This isn't particularly efficient, but should work. It gets all the Turtle3 objects in the world as a list, and then gets the first object from that list.
Could someone more knowledgeable with this tell me the reason I need to call a class in the first place?
The expression "call a class" doesn't make sense; you don't call classes. So, you'd need to explain what you mean properly before I can help you here. In general: code belongs wherever you want it to take effect. In the 'bullets' method that you have above, it gets the orientation of a turtle, then sets the orientation of the current object to that same orientation, then moves forward. Is this code in the Bullet class? This is probably not what you want. You should set the orientation of the Bullet only when it is created - so, in the constructor. If the Turtle3 actually creates the Bullet then you could move the handling of its direction to that class, which will simplify things a bit - since then the Bullet won't need to obtain a reference to the Turtle3 at all.
oscarreks oscarreks

2015/9/23

#
The expression "call a class" doesn't make sense; you don't call classes. So, you'd need to explain what you mean properly before I can help you here.
I probably over-used the word class, when I meant object. To clarify: I'm making a bullet, and I want to set its rotation to that of the turtle's, of which there is only 1 (the player controls the turtle). What I tried before was this:
1
2
3
4
public bullets()
   {
       setRotation(Turtle3.getRotation());
   }
But I get the error: non-static method getRotation() cannot be referenced from a static context. So what I understand is that because it's a different class/subclass/object, it isn't recognized or something. Is this the right line of reasoning? If not, what aproach should I be taking? I think I'll also try creating bullets through Turtle3, so thanks for that recommendation. Ah, sorry I didn't clarify.
danpost danpost

2015/9/23

#
The class does not have a rotation -- only the objects create from it can possibly have a rotation. A class is capable of creating multiple objects from it. Objects created from an Actor subclass can each have their own distinct rotational value. The class cannot know, even if there is just one object of its type, which object you want the rotation of. Since it is the turtle that determines the rotation of the bullets it create, have the turtle give the bullets it creates its rotational value:
1
2
3
4
// in Turtle3 class
bullets bullet = new bullets();
bullet.setRotation(getRotation());
getWorld().addObject(bullet, getX(), getY());
davmac davmac

2015/9/23

#
So what I understand is that because it's a different class/subclass/object, it isn't recognized or something. Is this the right line of reasoning? If not, what aproach should I be taking?
I tried to explain this above - it's a different object that you want the rotation of, so you need to specify that object. "non-static method getRotation() cannot be referenced from a static context." - means (essentially) that you can't get the rotation of the class Turtle3, because a class doesn't have a rotation. (What you really wanted was the rotation of an object which you know is an object of type Turtle3). As I said before, if there is only one Turtle3 object in the world, there is a way to get a reference to it. But, it is probably better to have the turtle give its rotation to the bullet.
oscarreks oscarreks

2015/9/23

#
@davmac @danpost Thanks so much for the help, I think I understand better However, f I did want to get a specific object's rotation/any variable while in a different class, how would I do so?
1
Turtle3 turtle = (Turtle3) getWorld().getObjects(Turtle3.class).get(0);
I noticed you said that this would do the job, but how would I know what object I'm referencing?
danpost danpost

2015/9/24

#
The 'getObjects' method of the World class will return a List object. This list will contain all objects of the given type (in the case of the line given, of type Turtle3) in the world. The line as given above will cause an error if no Turtle3 objects are in the world because the List will be empty and there would be no element to 'get'. You can use 'isEmpty' on the List object to determine if the list contains any elements or you can use 'size' on the List object to determine how many elements the list contains (which should be the number of objects of the given type that are in the world). The 'get' method can be then used to get any of the elements (provided that the element with the given index exists).
oscarreks oscarreks

2015/9/24

#
1
Turtle3 turtle = (Turtle3) getWorld().getObjects(Turtle3.class).get(0);
I'm only going to have one turtle in my world, so is there a more elegant way to get data from it?
danpost danpost

2015/9/24

#
oscarreks wrote...
I'm only going to have one turtle in my world, so is there a more elegant way to get data from it?
Well, the line given will get you the reference you need. You will then have access to any non-static 'public' method or field in the Turtle3 class. That is, you can refer to any non-static 'public' field with 'turtle.<field name here>' or call any non-static 'public' method with 'turtle.<method name here>()'.
davmac davmac

2015/9/24

#
is there a more elegant way to get data from it?
Again, in general, the bullet shouldn't need information from the turtle - have the turtle set the rotation of the bullet instead, and you avoid the whole issue. If you really do need to refer to the turtle, the most elegant way is to keep a reference to it somewhere (either in the object that you need to access it from, or in another object that you can get access to, such as the world). One of the tutorials covers this in some detail.
You need to login to post a reply.