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

2013/10/16

Help with getX()

tartags11 tartags11

2013/10/16

#
How would I make an if statement where getX() is equal to a certain number? This is what I thought would work but I think I am missing something but I'm not sure what... This is in an actor class btw.
    public void getXA()
    {
        if ( getX() == 4897)
        {
            //code omitted
        }
    }
Gevater_Tod4711 Gevater_Tod4711

2013/10/16

#
This if statement is alright. It will be true when getX() is 4897. So what is the problem with this code?
tartags11 tartags11

2013/10/16

#
The code itself (right now a Greenfoot.stop(); ) does not run.
danpost danpost

2013/10/16

#
An x-coordinate of 4897 would be quite far off the right edge of the world (about 3 to 4 screen-widths). Try using a number between zero and 'getWidth()-1'.
tartags11 tartags11

2013/10/16

#
4897 is within the world (its a side scrolling world). Now, the program wont compile...
import greenfoot.*;  

public class HorseA extends Horses
{
    private GreenfootImage horse1;
    private GreenfootImage horse2;
    private int location = getX();

    public HorseA()
    {
       //code omitted
    }
    public void act()
    {
        move();
        switchImage();
    }
    
    public void getLocation()
    {
        if ( location == 4897)
        {
            Greenfoot.stop();
        }
    }
    public void switchImage()
    {
        //code omitted
    }
    
}
java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed.
	at greenfoot.Actor.failIfNotInWorld(Actor.java:663)
	at greenfoot.Actor.getX(Actor.java:157)
	at HorseA.<init>(HorseA.java:13)
	at Racetrack.<init>(Racetrack.java:20)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
	at greenfoot.core.Simulation.newInstance(Simulation.java:581)
	at greenfoot.platforms.ide.WorldHandlerDelegateIDE$3.run(WorldHandlerDelegateIDE.java:409)
	at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:468)
	at greenfoot.core.Simulation.maybePause(Simulation.java:281)
	at greenfoot.core.Simulation.runContent(Simulation.java:212)
	at greenfoot.core.Simulation.run(Simulation.java:205)
danpost danpost

2013/10/16

#
Line 7 is using a method that requires that the object be in a world; however, calling 'new HorseA()' does not add the object into the world and must be executed before the object can be placed into a world. You probably need to utilize the Actor method 'addedToWorld(World)' to set that value in. except you probably need to take into account any scrolled amount; otherwise, it is set by world coordinates, not by scroll coordinates.
Gevater_Tod4711 Gevater_Tod4711

2013/10/16

#
The problem in this code is that you call the getX() method before the actor is added to the world. And if the actor is not existing in a world it hasn't got an x coordinate that the getX() method could return. You should change the code back to the one you had before. This code will work. But I still don't get what is the problem or what you want your code to do. Can you explain the problem? That would make it easier to help you.
danpost danpost

2013/10/16

#
'getX()' will always return a value between zero and 'getWorld().getWidth()-1', unless the method is overridden. In my Scrolling SuperWorld scenario, I wrote 'getUnivX' and 'getUnivY' methods to return the coordinates within the scrolling area.
tartags11 tartags11

2013/10/17

#
Still not sure why it isnt working... it needs to define the variable after the horse is placed in the world (not before).
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class HorseA extends Horses
{
    //code omitted
    private int locationA = getX();
    
    public HorseA()
    {
        //code omitted
    }
        
    public void act()
    {
        // code omitted
    }
    
    public void addedToWorld(World Racetrack)
    {
        getLocationA();
    }
    
    public void getLocationA()
    {
        if ( locationA == 4897)
        {
            Greenfoot.stop();
        }
    }
    
    public void switchImage()
    {
        //code omitted
    }
    
}
java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed.
	at greenfoot.Actor.failIfNotInWorld(Actor.java:663)
	at greenfoot.Actor.getX(Actor.java:157)
	at HorseA.<init>(HorseA.java:35)
	at Racetrack.<init>(Racetrack.java:20)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
	at greenfoot.core.Simulation.newInstance(Simulation.java:581)
	at greenfoot.platforms.ide.WorldHandlerDelegateIDE$3.run(WorldHandlerDelegateIDE.java:409)
	at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:468)
	at greenfoot.core.Simulation.maybePause(Simulation.java:281)
	at greenfoot.core.Simulation.runContent(Simulation.java:212)
	at greenfoot.core.Simulation.run(Simulation.java:205)
Gevater_Tod4711 Gevater_Tod4711

2013/10/17

#
Like I said you can't call the getX() method before you added the actor to the world. So you can't initialise a variable with getX() and you also can't use it in a constructor. If you want the variable locationA to get the value of getX() you need to do it this way:
public class HorseA extends Actor {
    
    private int locationA;
    
    public void addedToWorld(World world) {
        locationA = getX();
    }

}
But then the variable locationA will be the starting x position not the current x position.
danpost danpost

2013/10/17

#
Again, the assignment of 'locationA', rather the use of 'getX()', cannot be done before the object is added into the world. You should have something like this:
import greenfoot.*;

public class HorseA extends Horses
{
    //code omitted
    private int locationA;
    
    public HorseA()
    {
        //code omitted
    }
        
    public void act()
    {
        // code omitted
    }
    
    public void addedToWorld(World Racetrack)
    {
        locationA() = getX()+ /* current horizontal scrolled value */; // this assigns the initial value
    }
    
    public void getLocationA()
    {
        if ( locationA == 4897)
        {
            Greenfoot.stop();
        }
    }
    
    public void switchImage()
    {
        //code omitted
    }
}
You need to login to post a reply.