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

2014/12/3

Moving 2 actors like they're "stuck"

Bahb Bahb

2014/12/3

#
Hello there, to start off let me say that I am an complete noob with Java or Greenfoot. I have a question about 2 actors moving on the same X-axis like they are "stuck" or "fixed" to eachother. I have 2 actor's lets say actorX and actorY where my actorX moves horizontally with the left and right button. And my actorY moves vertically with the up and down buttons. What i am trying to achieve is to get my actorY move to the left or right at the same time my actorX does. I've tried to use actorX.getX() in my actorY code but it needs an reference or something i cant figure out. I've tried the "How to access one object from another" tutorial but it didn't help me. Basically i need an getX variable from my actorX that i can use in every other actor. If someone could help me out with a small tutorial that would be great, if been searching for hours.
danpost danpost

2014/12/3

#
This may not be the best way to organize it; but, for the sake of you learning and getting something going, I will try to explain one way to accomplish this. When you create your actorX object in your world class, place it in a variable and then add it into the world. That is -- instead of this:
addObject(new ActorX(), /* location coordinates */);
break it down into two separate statements; one creating the actor and the other adding it to the world:
Actor actorX = new ActorX();
addObject(actorX, /* location coordinates */);
or even three lines (declaring the variable first):
Actor actorX;
actorX = new Actor();
addObject(actorX, /* location coordinates */);
The 'actorX' variable in line one is a reference variable and becomes a reference to the object when assigned it in line 2; but, this reference will only be valid for the execution of the method it is in (once the method has been exited, the reference is lost). To make it more persistent, we can move that line (the last line 1 above) outside of the method. This will make the reference valid for as long as the world exists. What we did was expand the scope of the reference by declaring it in an outer block of code (because it is not enclosed between the method brackets, but now between the class brackets, it gains scope). Now we can create a method in the class that returns that reference:
public Actor getActorX()
{
    return actorX;
}
Because we declared the field outside the method above, we can now reference it in other methods. Now, in your ActorY class, you can do this:
WorldClassName wcn;
wcn = (WorldClassName)getWorld();
Actor actorX;
actorX = wcn.getActorX();
int actorXX;
actorXX = actorX.getX();
setLocation(actorXX, getY());
which can be written as:
setLocation(((WorldClassName)getWorld()).getActorX().getX(), getY());
Bahb Bahb

2014/12/3

#
Thanks man! You helped me out alot. It seems to be working now, you make it sound easy though :P You are doing a great job here danpost, respect.
You need to login to post a reply.