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

2016/2/14

Why wont my actor fall through the bottom and appear at the top

23204 23204

2016/2/14

#
public void move()
    {
       World myWorld=this.getWorld();
       int myWorldWidth=myWorld.getWidth();
       int myWorldHeight=myWorld.getHeight();

        if( this.getY() == myWorldHeight-1)
        {
            setLocation (getX(),1);
        }
        
        if (this.getY() ==0)
        {
            setLocation(getX() , myWorldHeight-1);
        }    
            
    }
As i am new to Greenfoot i would appreciate any help in figuring out why this code will not let my Actor fall through the bottom of the world and appear at the top at the same x-location.
danpost danpost

2016/2/14

#
23204 wrote...
< Code Omitted > As i am new to Greenfoot i would appreciate any help in figuring out why this code will not let my Actor fall through the bottom of the world and appear at the top at the same x-location.
Line 14 places the actor at the bottom edge -- not slightly off the bottom edge. So it is moved back to the top when lines 7 through 10 are executed again. Change 'myWorldHeight-1' on line 14 to 'myWorldHeight-2'. The name of the method does not correspond to what the method actually does. Either the name should be changed to something like 'wrap' or the movement code should be moved into the method so that the method moves and performs any necessary wrapping.
23204 23204

2016/2/14

#
Thank you so much, now if i put multiple actors in my world, one one of them can wrap. Do you know how i could fix this? Also, is there any way to make the actor just fall through the bottom to the top, and not fall through the top back to the bottom? Here is what i have so far, i am sure it looks a little messy as this is my first time using greenfoot. I know i repeat some code but if i delete it anywhere the wrap no longer works. Your help is so appreciated!
public void act() 
    {
       World myWorld=this.getWorld();
       int myWorldWidth=myWorld.getWidth();
       int myWorldHeight=myWorld.getHeight();
       if (this.getX() <= 0 || this.getX()>=myWorldWidth-1)
        this.turn(2);
       
       
       if (this.getX()<=50)
       {
           this.move(1);
       }
       
       if ( Greenfoot.getRandomNumber(5) < 2)
        {
            turn(Greenfoot.getRandomNumber(10)-5);
        }
       this.move(1);
       if(this.getY() == myWorldHeight-2)
        {
            setLocation (getX(),1);
        }
       if (getY() ==0)
        {
            setLocation(getX() , myWorldHeight-2);
        }    
    }
    public void wrap()
    {
       World myWorld=this.getWorld();
       int myWorldWidth=myWorld.getWidth();
       int myWorldHeight=myWorld.getHeight();
        if(this.getY() == myWorldHeight-2)
        {
            setLocation (getX(),1);
        }
        if (getY() ==0)
        {
            setLocation(getX() , myWorldHeight-2);
        }    
            
    }
danpost danpost

2016/2/14

#
You can totally remove the 'wrap' method (lines 29 through 43). The code is duplicated in your 'act' method. The 'wrap' method was not even being executed because it was not being called from any active method (like the 'act' method). Then, change '2', only in line 20, to '1'.
23204 23204

2016/2/14

#
Im so close to getting it to work! Am i correct to say that: this.getY() <=0 means if the actor hits the top of the world? I cant get my actor to turn 2 degrees when it hits the top of the world so they all just get stuck up there when i put multiples of them in the world.
danpost danpost

2016/2/14

#
23204 wrote...
Am i correct to say that: this.getY() <=0 means if the actor hits the top of the world?
You are correct in what the given line of code means.
I cant get my actor to turn 2 degrees when it hits the top of the world so they all just get stuck up there when i put multiples of them in the world.
I believe your actors are changing their rotations (or turning), but their direction of movement is limited by the speed at which they are moving. If you let it run long enough, you will probably start seeing them take off from the top edge. When moving one cell at a time, they are limited to moving to only one of the eight surrounding cells from where they are currently located. The only way to compensate for this is to keep track of the location (x and y coordinates) of these actors in double type fields and use the java.lang.Math class trig functions to determine the changes in their location coordinates; then, in turn, use the new coordinates to set the new location of the actors.
You need to login to post a reply.