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

2016/10/20

I'm getting an actor not in world error plz help

1
2
hockeydude1155 hockeydude1155

2016/10/20

#
int x = getX(); int y = getY(); if(Greenfoot.isKeyDown("left"){ setLocation(x-2, y) }else if(Greenfoot.isKeyDown("right"){ setLocation(x+2, y) }
danpost danpost

2016/10/20

#
The snippet of code given may be where the exception occurred; but, the cause could be anywhere. Please show the entire class code and use code tags (see here).
Nosson1459 Nosson1459

2016/10/20

#
need source code to look for problems
danpost wrote...
and use code tags (see here).
hockeydude1155 hockeydude1155

2016/10/20

#
public class Person extends Actor { int x = getX(); int y = getY(); public void act() { if(Greenfoot.isKeyDown("left")) { setLocation (x-2, y); } else if(Greenfoot.isKeyDown("right")) { setLocation(x+2, y); } } }
Super_Hippo Super_Hippo

2016/10/20

#
Put lines 3 and 4 at the beginning of the act method.
hockeydude1155 hockeydude1155

2016/10/20

#
that worked thank you
Nosson1459 Nosson1459

2016/10/21

#
An easy explanation of why it didn't work is basically you were making x be equal to that getX() so the position should move 2 pixels once and then not work any more cause getX() is changing but not x so for it to work it will generally be like this:
public class Person extends Actor
{
    int x;
    int y;
    public void act()
    {
        x=getX();
        y=getY();
        if(Greenfoot.isKeyDown("left"))
        {
            setLocation(x-2,y);
        }
        if(Greenfoot.isKeyDown("right"))
        {
            setLocation(x+2,y);
        }
    }
}
same for up and down (y) but you don't need variables x and y cause you could just do this:
public class Person extends Actor
{
    public void act()
    {
        if(Greenfoot.isKeyDown("left"))
        {
            setLocation(getX()-2,getY())
        }
        if(Greenfoot.isKeyDown("right"))
        {
            setLocation(getX()+2,getY())
        }
    }
}
and I wouldn't think you need the else because when the left key is pressed it will go 2 pixels left, when right key is pressed, move right 2 pixels. Two different ifs (In the future type the code by using the blue word "code" at the bottom of the typing window in discussions (as mentioned above) for more info see here.)
Nosson1459 Nosson1459

2016/10/21

#
danpost wrote...
The snippet of code given may be where the exception occurred; but, the cause could be anywhere. Please show the entire class code and use code tags (see here).
where did you get this link from and how do you know about it? when I click on the link it works but when i just go to here there is nothing to press on to bring me to code-tags the only way I know how to get to code-tags is through your (danpost's) link, no links in Documentation
danpost danpost

2016/10/21

#
Nosson1459 wrote...
where did you get this link from and how do you know about it?
Look under the 'Post a reply' box (on this page when signed in). There are two links there -- one for embedding and the other for posting code.
Super_Hippo Super_Hippo

2016/10/21

#
Nosson1459 wrote...
An easy explanation of why it didn't work is basically you were making x be equal to that getX() so the position should move 2 pixels once and then not work any more
Well, the easy explanation why it didn't work is that you can't use methods which require the actor to be in the world outside methods because they are executed when the object is created, so before it was added to the world.
Nosson1459 Nosson1459

2016/10/21

#
Super_Hippo wrote...
Well, the easy explanation why it didn't work is that you can't use methods which require the actor to be in the world outside methods because they are executed when the object is created, so before it was added to the world.
well naturally you can't do things with an actor that doesn't exist BUT the reason I went wrong is because Nosson1459 is 2 people the one in this discussion (which is me of course) doesn't program with Greenfoot (this account isn't under my name) so I program with Java not Greenfoot but I can still help the other person and him help me
Nosson1459 Nosson1459

2016/10/21

#
danpost wrote...
Look under the 'Post a reply' box (on this page when signed in). There are two links there -- one for embedding and the other for posting code.
thanks, in the url it says www.greenfoot.org/doc/code-tags so when I backspaced the code-tags it brought me to Documentation. Do you know if it is possible to post an image that I have on my computer
danpost danpost

2016/10/21

#
Nosson1459 wrote...
Do you know if it is possible to post an image that I have on my computer
Absolutely NOT. But, if you upload it to an image hosting site, then you can reference it from there.
Nosson1459 Nosson1459

2016/10/21

#
why so Absolute you just said I can do it if I upload it.
danpost danpost

2016/10/21

#
Nosson1459 wrote...
why so Absolute you just said I can do it if I upload it.
You cannot reference the one on your computer.
There are more replies on the next page.
1
2