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

2012/6/6

Get Location from an other Actor

1
2
schepieli schepieli

2012/6/6

#
My problem is that i really don´t know how to hand over the position from one actor to the other. They are all in the same subclass. The background idea is that some buttons appears around an object. And yes, i used google about 30 minutes... Thank you for help :)
davmac davmac

2012/6/7

#
You need a reference to the other actor (i.e. some way to identify it). For instance, if you have a variable called 'actor' containing the reference to the actor whose position you need, you can use "actor.getX()" and "actor.getY()" for example to get its position. (You haven't given much information about what you're really trying to do, so that's about as much help as anyone can give...)
lostinjava lostinjava

2012/6/7

#
i'm also trying the same thing , the only thing i get in return is a nullpointerexeption . public class Actor1 extends Actor { Actor2 actor2; public void getPosactor2() { int x = actor2.getX(); int y = actor2.getY(); System.out.println("x+y"); } }
SPower SPower

2012/6/7

#
That's because you never initialize the actor2 object. Do this in your constructor, just add this code:
1
2
3
public Actor1 {
    this.actor2 = (something);
}
But, what is actor2? What does it represent?
SPower SPower

2012/6/7

#
Oops, this is the right code:
1
2
3
public Actor1() {
    actor2 = (something);
}
Of course, (something) is not valid code.
davmac davmac

2012/6/7

#
This line: Actor2 actor2; Declares a variable - which can hold a reference to an object, but initially does not. It doesn't create an object. You need to assign something to the variable. As to what - well, I don't know, because you still haven't explained what object you want to get the location of...
lostinjava lostinjava

2012/6/8

#
the actor2 is just an example i was using to test the above out .
davmac davmac

2012/6/8

#
Sure. But you don't assign anything to it, so it is null, so you get a NullPointerException.
schepieli schepieli

2012/6/9

#
So... I have a character which walks on a map. If he is at the maps end, i would like that buttons will apear around him. Then you can click on these buttons for choosing in wich direction the character is going to walk. My teacher told me that these buttons should be actors. And i have really no idea how i can assign their position in dependence to the character. Actually i only want to understand how to create a variable with the actor as contant. Or i don´t have to do that?...
niklaskar niklaskar

2012/6/9

#
its fairly easy ;) put this piece of code into your actor ;)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
if(getX()<10){
    getWorld().addObject(new Button(),getX(),getY()-10);
    getWorld().addObject(new Button(),getX(),getY()+10);
    getWorld().addObject(new Button(),getX()+10,getY());
}
else if(getX()>getWorld().getWidth()-10){
    getWorld().addObject(new Button(),getX(),getY()-10);
    getWorld().addObject(new Button(),getX(),getY()+10);
    getWorld().addObject(new Button(),getX()-10,getY());
}
else if(getY()<10){
    getWorld().addObject(new Button(),getX()-10,getY());
    getWorld().addObject(new Button(),getX()+10,getY());
    getWorld().addObject(new Button(),getX(),getY()+10);
}
else if(getY()>getWorld().getHeight()-10){
    getWorld().addObject(new Button(),getX()-10,getY());
    getWorld().addObject(new Button(),getX()+10,getY());
    getWorld().addObject(new Button(),getX(),getY()-10);
}
of course you have to adjust it to your world ;) this is just for a standard 600x400 world, but it should be no problem for you to change this ;)
danpost danpost

2012/6/9

#
@niklaskar, that would work fine, if the buttons did not need to know which way to point. @schepieli, add an integer parameter to the Button class object constructor to recieve a value, from 0 to 3, indicating how much rotation to apply (* 90). The Button constructor should look something like this:
1
2
3
4
public Button(int btnNum)
{
    getImage().setRotation(btnNum * 90);
}
Then, in the code provided by niklaskar, put the appropriate values in the constructor calls (i.e. 'new Button(3) for 'up'; or 'new Button(2)' for 'left'). Or, instead of his code, you could put in the act method of the actor that walks:
1
if (atWorldEdge()) addButtons();
and add the following method:
1
2
3
4
5
6
7
8
9
10
11
private void addButtons()
{
    for (int i = 0; i < 4; i++)
    {
        int dx = ((3 - i) % 2) * (1 - i); // will be -1, 0, or 1
        int dy = ((4 - i) % 2) * (2 - i); // will be -1, 0, or 1
        if (getX() + dx  * 10 >= 0 && getX() + dx * 10 < getWorld().getWidth() &&
            getY() + dy * 10 >= 0 && getY() + dy * 10 < getWorld().getHeight())
            addObject(new Button(i),getX() + dx * 10, getY() + dy * 10);
    }
}
BTW, the 'atWorldEdge' method or the check you use in place of it should compensate for the size of the image of the actor (i.e. 'if (getX() <= getImage().getWidth() / 2)', or 'if (getX() >= getWorld().getWidth() - getImage().getWidth() / 2 - 1)'). By not compensating for it, your button objects will be half cut off by the edges of the screen.
SiggyxLeGiiT SiggyxLeGiiT

2012/6/10

#
SPower wrote...
Oops, this is the right code:
1
2
3
public Actor1() {
    actor2 = (something);
}
Of course, (something) is not valid code.
davmac wrote...
This line: Actor2 actor2; Declares a variable - which can hold a reference to an object, but initially does not. It doesn't create an object. You need to assign something to the variable. As to what - well, I don't know, because you still haven't explained what object you want to get the location of...
I'm understanding it needs to be referenced, but what would it look like if I wanted to put my "gun" actor as actor2? How do I understand I'm talking about another actor? I feel like I should know this and I'm drawing blank..
davmac davmac

2012/6/10

#
See this tutorial, which explains a common way to do it. You can also add an actor parameter to another actor's constructor, or, you can have one actor create another. So for instance if your player actor always has a gun, and the gun is a separate actor, you could create the gun in the player's constructor (or in its 'addedToWorld' method).
barnstorm3r barnstorm3r

2014/5/8

#
I am making a Tron-style game, and I am finding it difficult to create trail instances at the location of the character. I am relatively new to Greenfoot so please keep any explanations simple. The trail instance is a subclass of the character. The character has no code other than basic arrow key control scripts. I have spent an hour or so trying to work out how to make this work using google, but I cannot seem to find a relatively simple way of making this work. Is there no way to make public variables accessible by other classes? Thanks, barnstorm3r
danpost danpost

2014/5/8

#
@barnstorm3r, please do not rekindle discussion that are not recent. Also, it would be best if instead of jumping into the middle of a discussion that you start your own that will deal with your issue. Mixing issues in one discussion could get confusing for someone who later reviews it. I encourage you to start a new discussion on this matter.
There are more replies on the next page.
1
2