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

2016/9/30

Objects get stuck at top and right of screen?

JarJarBanks7 JarJarBanks7

2016/9/30

#
I have some fruit that randomly choose a direction to move in (up,down,left,right) and whe n they reach the edge the are supposed to turn around and go the other direction. but they only turn at the edge at the bottom and left of the screen then get stuck when they reach the top or the right. how do i fix? This is one of the fruits code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Apple here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Apple extends Actor
{
    int d=Greenfoot.getRandomNumber(4);
    int fruitClicked=0;
    
    /**
     * Act - do whatever the Apple wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        movement();
        removeOnClick();
   }    
    private void movement(){
        if (d==1){
            if(1==1){
                setLocation(getX()+5,getY());
                turnAtEdge();
                
        }
       }
       if(d==2){
           if(1==1){
               setLocation(getX()-5,getY());
               turnAtEdge();
         }
       }
        if (d==3){
            if(1==1){
                setLocation(getX(),getY()-5);
                turnAtEdge();
        }
       }
       if(d==4){
           if(1==1){
               setLocation(getX(),getY()+5);
               turnAtEdge();
         }
       }
   }
    public void turnAtEdge(){
    if(isAtEdge()){
        
        if(d==1){
            d=2;
     }
      if(d==2){
            d=1;
     }
      if(d==3){
            d=4;
     }
      if(d==4){
            d=3;
     }
    }
   }
   public void youWin(){
       if (fruitClicked==16){
          getWorld().showText("You Win" ,400, 300);
          Greenfoot.stop();
        }
    }
    public void removeOnClick()
   {
      if (Greenfoot.mouseClicked(this)) {
        World world;
        world = getWorld();
        world.removeObject(this);
        return;
        
      } 
      if (Greenfoot.mouseClicked(this)) {
        fruitClicked++;
        }
    }
}
danpost danpost

2016/9/30

#
With the code in your 'turnAtEdge' method, if 'd' is odd (1 or 3) then it will remain odd as after changing to even the next 'if' block will change it right back. If 'd' is even, then it will change to odd and then fall into the staying odd category. Use 'else' between the 'if' blocks. There are some other issues that you will eventually find out about. Your 'fruitClicked' field will never have a value more than one for any apple, and for those that turn one, they are removed from the world and assigned for garbage collection.
JarJarBanks7 JarJarBanks7

2016/10/2

#
Im trying to learn as much as i can so could you elaborate on why the fruitclicker wouldnt work and how i would fix it,and if you arent bothered enough already, explain why that solution would work?
danpost danpost

2016/10/2

#
JarJarBanks7 wrote...
Im trying to learn as much as i can so could you elaborate on why the fruitclicker wouldnt work and how i would fix it,and if you arent bothered enough already, explain why that solution would work?
Line 12 declares a field. A field with that name will be given to each Apple object created and each of these objects will have a unique value for its field (the value of 'fruitClicked' for one apple can be different from the value of another). The field is considered "non-static" and is called an "instance field" -- it belongs specifically to an object (or instance) of the class. Now, you have just one Apple class and you can, by making the field static (by adding the modifying keyword "static" to the line) make the field a "class field" (meaning it belongs to the class and not to each object of the class. It will retain a single value and that one value will be what all objects of the class will (or can) use.
JarJarBanks7 JarJarBanks7

2016/10/2

#
Thank you so so much
JarJarBanks7 JarJarBanks7

2016/10/3

#
One problem though,using an else statement would only work if there were only two directions; if an else is used, then if it isnt 1 it would run the d==2 code .same goes for any other d= statement that you add an else to.If this isnt what you meant then explain further or if willing,give example code
JarJarBanks7 JarJarBanks7

2016/10/3

#
also if i edited the turnatedge code to set the location back a little after changing the d value so that it is no longer at the edge, would that release it from the loop?
JarJarBanks7 JarJarBanks7

2016/10/3

#
Final question, why are the fruits able to bounce off the bottom and left?
JarJarBanks7 JarJarBanks7

2016/10/3

#
I know you hate me already for the separate replys, but the fruit doesnt glitch out and constantly try to move left and right a the same time, it just stops, sometimes before the edge
Super_Hippo Super_Hippo

2016/10/3

#
You can use 'if.... if else... if else...'. As an alternative, you could use a switch.
JarJarBanks7 JarJarBanks7

2016/10/3

#
what do you mean?
Super_Hippo Super_Hippo

2016/10/3

#
if (d==1) d=2;
else if (d==2) d=1;
else if (d==3) d=4;
else if (d==4) d=3;
The same as
switch (d)
{
    case 1: d=2; break;
    case 2: d=1; break;
    case 3: d=4; break;
    case 4: d=3; break;
}
JarJarBanks7 JarJarBanks7

2016/10/3

#
like this?
 private void movement(){
        if (d==1){
            if(1==1){
                setLocation(getX()+5,getY());
                turnAtEdge();
                
        }
       }
       else if(d==2){
           if(1==1){
               setLocation(getX()-5,getY());
               turnAtEdge();
         }
       }
        else if (d==3){
            if(1==1){
                setLocation(getX(),getY()-5);
                turnAtEdge();
        }
       }
       else if(d==4){
           if(1==1){
               setLocation(getX(),getY()+5);
               turnAtEdge();
         }
       }
   }
danpost danpost

2016/10/3

#
JarJarBanks7 wrote...
One problem though,using an else statement would only work if there were only two directions; if an else is used, then if it isnt 1 it would run the d==2 code .same goes for any other d= statement that you add an else to.If this isnt what you meant then explain further or if willing,give example code
You could just add an 'else' at the beginning of lines 56 and 62. That way each axis is treated separately.
the fruit doesnt glitch out and constantly try to move left and right a the same time, it just stops, sometimes before the edge
This was because of the lack of 'else' in the 'turnAtEdge' method. The value was changing and immediately changing back. So, the movement was never engaged in any different direction. The 'if (1 == 1)' conditions are unnecessary code. The execution of the 'movement' method is equivalent to this:
private void movement(){
    if (d==1) setLocation(getX()+5,getY());
    if (d==2) setLocation(getX()-5,getY
    if (d==3) setLocation(getX(),getY()-5);
    if (d==4) setLocation(getX(),getY()+5);
    turnAtEdge();
}
You need to login to post a reply.