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

2014/3/23

Get int of Actor image size in World

Unicus Unicus

2014/3/23

#
Hello, So I have tried this several ways, the last one being this line in World class: "public int x = actorName.getImage().getWidth();" But obviously, it tells me that non-static method getImage() cannot be referenced from a static context. So, I'm not sure how could transform this into a static method? Thanks in advance!
danpost danpost

2014/3/23

#
What is 'line'? how do you declare it? show the code you are using; use the 'code link below the 'Post a reply' box to insert your code.
Unicus Unicus

2014/3/23

#
This is the code in the actor called line:
1
public int x = getImage().getWidth()-45;
And this is the code in the World:
1
public int x = line.getImage().getWidth();
Unicus Unicus

2014/3/23

#
I also tried with this: Code in actor "line"
1
2
3
4
5
6
public int imageWidth()
{
    GreenfootImage image = getImage();
    int x = image.getWidth()-45;
    return x;
}
And code in World:
1
public int x = line.imageWidth();
danpost danpost

2014/3/23

#
Are you keeping references to the size of the 'line' actor in both the 'line' class and your World subclass? I ask because you have 'public' at the beginning of both those lines. Certainly, your world class cannot assign a value to 'x' if this line is to be declaring a field because how can any actors be present if the world is not yet even created!
Unicus Unicus

2014/3/23

#
danpost wrote...
Certainly, your world class cannot assign a value to 'x' if this line is to be declaring a field because how can any actors be present if the world is not yet even created!
True, but then how can I get the width and/or the height of an Actor class, in the World subclass?
Unicus Unicus

2014/3/23

#
The reason I want this, is because after I manage to get these values, placing the items in the world will be much much easier. Will only have to do the coordinates (x, y), for the first item, (2*x,y) for the second, and so on.
danpost danpost

2014/3/23

#
Try this in the world class:
1
public int x = (new line()).getImage().getWidth();
Unicus Unicus

2014/3/23

#
That's exactly what I needed, thank you very much! Kudos for you!
danpost danpost

2014/3/23

#
You do not have to declare it as a field in your world class (or in your 'line' class, for that matter). Just get the value locally within the method when you need it as follows:
1
2
int x = (new line()).getImage().getWidth();
for (int k=0, k<getWidth()/x; k++) addObject(new line(), k*x, y);
The above was just an example; but it add line objects across the width of the world.
Unicus Unicus

2014/3/23

#
Indeed, thanks :)
Unicus Unicus

2014/3/24

#
Any idea why this won't work?
1
2
3
4
5
6
7
8
for(int j=0; j<getHeight(); j++)
       {
           for(int i=0; i<getWidth(); i++)
           {
               addObject(new Connector(), i*(x+40)+11, j*y+10); //add dots on left
               addObject(new Connector(), i*(x+20)+11, j*y+10); //add dots on right
           }
       }
It copiles successfully, but nothing appears Edit: Nevermind, infinite loop
You need to login to post a reply.