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

2018/1/4

Set object location on run

gilbert1992 gilbert1992

2018/1/4

#
i've got a project that has a preset start location for an object in a maze however you can still move the object before you press start to "cheat" is there a way to so set the object to the start location when you click run to remove the option to "cheat"? thanks in advance for any help.
Super_Hippo Super_Hippo

2018/1/4

#
If you want to make it impossible in Greenfoot, then you can either make sure that you can't pause the scenario
1
2
3
4
5
6
7
8
//in world constructor
Greenfoot.start();
 
//and as a method in world
public void stopped()
{
    Greenfoot.start();
}
Or you can save the location of the object at the end of the act cycle and compare it at the start of the next act circle. If it changed, place it back to where it was:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
private int x, y;
 
protected void addedToWorld(World w)
{
    x = getX();
    y = getY();
}
 
public void act()
{
    if (x != getX() || y != getY()) setLocation(x, y);
    //...
    //... rest of you act method
    //...
    x = getX();
    y = getY();
}
If you want to upload it here to the website, you can set it so it's not possible to change speed or move objects around when the scenario is paused. Then you won't need any additional code.
xbLank xbLank

2018/1/4

#
As always: Provide some code. I assume that the Start Button Object gets removed when its clicked? If that's the case you can use that as an condition to your movement functions.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public void act()
  {
     if(buttonClicked())
     {
         if(Greenfoot.isKeyDown("right"))
         {
             move(1);
         }
         /*Other Movement Functions
            .
            .
            .
            .
         */
     }
  }   
  private boolean buttonClicked()
  {
      if(getWorld().getObjects(StartButton.class).size() == 0)
          return true;
      return false;
       
  }
1
2
3
4
5
6
7
8
9
10
public class StartButton extends Actor
{
    public void act()
    {
        if(Greenfoot.mouseClicked(this))
        {
            getWorld().removeObject(this);
        }
    }   
}
Super_Hippo Super_Hippo

2018/1/4

#
I think what he meant is that you can drag objects around using your mouse when the scenario itself is not running.
xbLank xbLank

2018/1/4

#
Not when you compile it?
gilbert1992 gilbert1992

2018/1/4

#
unfortunately this is for a university assignment so i can't upload the code else i get plagiarism problems otherwise i would. yes super hippo i mean about the dragging around while it's not running. am about to try what you put.
xbLank xbLank

2018/1/4

#
I don't really understand your problem. Just compile it into a jar file once you are done with your Project. No dragging. No cheating.
gilbert1992 gilbert1992

2018/1/4

#
thanks super_hippo solved with your solution
Super_Hippo Super_Hippo

2018/1/4

#
일리아스 wrote...
I don't really understand your problem. Just compile it into a jar file once you are done with your Project. No dragging. No cheating.
But the code is not visible directly and can't be evaluated then.
xbLank xbLank

2018/1/4

#
Exactly. Thats why you send your teacher the Project File and a compiled jar file. You dont have to mess with your code just so you cant move objects in the IDE. Thats just stupid.
You need to login to post a reply.