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

2012/12/10

variable not changing..is something wrong in the code. the value of variable random is not changing even after envoking the function.?

1
2
vagisha vagisha

2012/12/10

#
public class equation extends mover { public int flag=0,random=10,i; private GreenfootImage image1; private GreenfootImage image2; private GreenfootImage image3; private GreenfootImage image4; private GreenfootImage image5; private GreenfootImage image6; private GreenfootImage image; /** * Act - do whatever the equation wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void movingEquation() { setLocation(getX()+2,getY()-2);} public int starting() {random=Greenfoot.getRandomNumber(6); flag=flag+1; return random; } public void act() { // Add your action code here. image= new GreenfootImage("background.PNG"); setImage(image); image1= new GreenfootImage("equation1.PNG"); image2= new GreenfootImage("equation2.JPG"); image3= new GreenfootImage("equation3.png"); image4= new GreenfootImage("equation4.png"); image5= new GreenfootImage("equation5.png"); image6= new GreenfootImage("equation6.png"); if(Greenfoot.isKeyDown("enter")) i=starting(); if(i==0) setImage(image1); else if(i==1) setImage(image2); else if(i==2) setImage(image3); else if(i==3) setImage(image4); else if(i==4) setImage(image5); else if(i==5) setImage(image6); if(flag!=0) movingEquation(); if(atWorldEdge()) { World W = getWorld(); W.removeObjects(W.getObjects(equation.class)); } } }
erdelf erdelf

2012/12/10

#
do you have a problem? (btw. next time you are posting code, use the code button at the bottom of the textbox)
vagisha vagisha

2012/12/10

#
random=Greenfoot.getRandomNumber(6);
              flag=flag+1; 
              return random;
vagisha vagisha

2012/12/10

#
public class equation extends mover
{

public int flag=0,random=10,i;
    private GreenfootImage image1;
    private GreenfootImage image2;
    private GreenfootImage image3;
    private GreenfootImage image4;
    private GreenfootImage image5;
    private GreenfootImage image6;
     private GreenfootImage image;
   
  
    /**
     * Act - do whatever the equation wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
   public void movingEquation()
     { setLocation(getX()+2,getY()-2);}
     
     public int starting()
     {random=Greenfoot.getRandomNumber(6);
              flag=flag+1; 
              return random; } 
    
     public void act() 
     {
       
       // Add your action code here.
        image= new GreenfootImage("background.PNG");
        
        setImage(image);
        
        image1= new GreenfootImage("equation1.PNG");
        image2= new GreenfootImage("equation2.JPG");
        image3= new GreenfootImage("equation3.png");
        image4= new GreenfootImage("equation4.png");
        image5= new GreenfootImage("equation5.png");
        image6= new GreenfootImage("equation6.png");
   
         
        
          
        
              if(Greenfoot.isKeyDown("enter"))
               i=starting();
           
                
                if(i==0)
                setImage(image1);
                else if(i==1)
                setImage(image2);
                else if(i==2)
                setImage(image3);
                else if(i==3)
                setImage(image4);
                else if(i==4)
                setImage(image5);
               else if(i==5)
                setImage(image6);
                
                
                if(flag!=0)
             movingEquation();
                
             
            
                
                    if(atWorldEdge())
            {  World W = getWorld();  
W.removeObjects(W.getObjects(equation.class));    }
  
    
}
}
vagisha vagisha

2012/12/10

#
yea.thanks..i dint know abt that.. yeah..my variable random is not changing its initialized value.
erdelf erdelf

2012/12/10

#
you are never calling the method starting
vagisha vagisha

2012/12/10

#
sorry! random is always getting zero as its value
vagisha vagisha

2012/12/10

#
line no. 46
nccb nccb

2012/12/10

#
i will be zero until you press enter, after which you've got a 5/6 chance of it changing value (1 in 6 times it will get zero again). And when you press enter, the equation will move, once it hits the edge all equations will vanish from the world. Is that different to what you are seeing?
vagisha vagisha

2012/12/10

#
yeah..see..i press enter.. and i definitely get a different image(AS mentioned in the code). and after this i am using this value in another class to do something(like turn ). its like if(i==1) ,then object 1 turns. and if(i==0) then object2 turns. and the problem is.. everytime object2 is turning. wich certainly means that i has value zero.. i m not getting this.,
vagisha vagisha

2012/12/10

#
this is the code where i m using the variable i.. its a different class.
vagisha vagisha

2012/12/10

#
public class four extends numbers
{ public int ran=new equation().i;
    /**
     * Act - do whatever the four wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if(ran==0)
      { if(numbersee(you.class))
       turn(-2);
    }// Add your action code here.
    }    
vagisha vagisha

2012/12/10

#
there are other subclasses where i define this code with change in line no.9.. i.e. ex: if(ran==1) or, if(ran==2), or if(ran==3)...etc.
vonmeth vonmeth

2012/12/10

#
The problem is that it is not passing a reference of 'i' to 'ran', it is a passing the value of 'i' (only one time) to 'ran'. So 'ran' will get set to '0', and will continue to be '0'. You will want to create a way for class 'four' to get the current value of 'i' from 'equation'.
nccb nccb

2012/12/10

#
i is what we call an instance variable. That means each instance of the class (each object) has its own version of i. So when you create a new equation and access the value of i, it will be zero, because that's a different version of i to the original equation class. You either need to get a reference to the equation object you are interested in, or make the variable static (so that there is one value of i for all equation objects, not one for each). It depends if you want to allow each equation to have its own version of i, or not.
There are more replies on the next page.
1
2