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

2020/10/26

How to create a variable on Initialization

FranzVnH FranzVnH

2020/10/26

#
Greetings! I am trying to create a variable that records the X and Y values of my actors when the program starts without having to hardcode it in with numbers, the "StartX, and StartY" if you would. Thing is I cannot find a way to tie my newly created variable to the initialization X and Y values and not change. If anyone has any ideas on how to do this, that would be great.
danpost danpost

2020/10/26

#
FranzVnH wrote...
I am trying to create a variable that records the X and Y values of my actors when the program starts without having to hardcode it in with numbers, the "StartX, and StartY" if you would.
You will need to show your current codes and point to the actor in question.
FranzVnH FranzVnH

2020/10/26

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    public void moveforward(int startX, int startY)
    {
           setLocation(getX() + 5,getY());
           if (getY() <= getWorld().getHeight()/2)
            {if (getX() >= getWorld().getWidth()-getImage().getWidth())
           {
               setLocation(startX, startY);
            }
            setLocation(getX()+5, getY());
        } else
        {
                if(getX() <= getImage().getWidth())
                {
                        setLocation(startX, startY);
                    }
                    setLocation(getX()+5, getY());
                }
                turn(5);
    }
}
The code of the subclass regarding all actors, was made in a way so that the position would reset after the actor reaches a specific point
1
2
int startX = getX();
         int startY = getY();
This is the code of one actor in particular, but the value will consistently change because X and Y will constantly change.
danpost danpost

2020/10/26

#
FranzVnH wrote...
<< Code Omitted >> This is the code of one actor in particular, but the value will consistently change because X and Y will constantly change.
Where did you put that? Show entire class codes please.
You need to login to post a reply.