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

2013/8/8

Control the speed of the game? help?

1
2
joeschmoe joeschmoe

2013/8/8

#
Hi, I want to make it so when my object eats potatoes the game goes into slow motion for 5 seconds. I have a method for eating the potato by I don't know if there is a way to control the game speed. I looked in the API and didnt see anything but I might have missed it.
 public void Eat(){
     Actor actor = getOneIntersectingObject(Potato.class);
        if(actor != null)
            {
            // When a potato is caught, remove potato, and make slow motion?
            getWorld().removeObject(actor);
             }
What should I add?
8bitcarrotjuice 8bitcarrotjuice

2013/8/8

#
Scratch that.
8bitcarrotjuice 8bitcarrotjuice

2013/8/8

#
public void Eat(){  
    int i;
    boolean slow;
    Actor actor = getOneIntersectingObject(Potato.class);  
       if(actor != null)  
           {  
           slow=true;  
           i=1;
           getWorld().removeObject(actor);  
           return;
            }  
       if(slow==true)
            i++;
       if(i<5000){
            Greenfoot.setSpeed(25); //adjust to your liking
}
       else if(i>5000){
             Greenfoot.setSpeed(50);
             slow=false;
}
Might work, sorry if it doesn't; I'm still a newbie.
joeschmoe joeschmoe

2013/8/8

#
Wow, looks pretty good. the part with "if(slow==true) " is giving me the error "the variable slow might not have been initialized. It won't compile. That means I have to have "slow == false " at the beginning or something right?
8bitcarrotjuice 8bitcarrotjuice

2013/8/8

#
Hmm, I'm not at a machine at the moment, but you wouldn't put "slow == false", that's only if your using it in a statement like if, for, while, etc... Basically, 1x= is when you are setting the value, and 2x= is when you are checking the value. I think you should initilize it as '"boolean slow=false;', but that might not work. Tell me what happens!
joeschmoe joeschmoe

2013/8/8

#
I did this and it compiled by didnt slow the game. maybe I have to change the setSpeed number?
 public void Eat(){    
               int i;  
               boolean slow;
               Actor actor = getOneIntersectingObject(Potato.class);    
                     if(actor != null)    
                    {    
                        slow=true;    
                         i=1;  
                        getWorld().removeObject(actor);    
                          return;  
                     }   
                   else{     slow=false;
                             i=2;
                         }
            
                     if(slow==true)  
                           i--;  
        
        
                     if(i<2){  
                        Greenfoot.setSpeed(25); //adjust to your liking  
                             }  
                            else if(i>2){  
                               Greenfoot.setSpeed(50);  
                                 slow=false;  
                }  
          }
joeschmoe joeschmoe

2013/8/8

#
hmm need some equal signs
8bitcarrotjuice 8bitcarrotjuice

2013/8/8

#
Keep the values I gave you, and then compile. Try this code.
public void Eat(){      
              int i;    
              boolean slow=false;  
              Actor actor = getOneIntersectingObject(Potato.class);      
                    if(actor != null)      
                   {      
                       slow=true;      
                        i=1;    
                       getWorld().removeObject(actor);      
                         return;    
                    }                                
             
                    if(slow==true)    
                          i++;            
         
                    if(i<5000){    
                       Greenfoot.setSpeed(25); //adjust to your liking    
                     }    
                     else if(i>5000){    
                         Greenfoot.setSpeed(50);    
                          slow=false;    
               }    
         }  
8bitcarrotjuice 8bitcarrotjuice

2013/8/8

#
Tell me what happens.
joeschmoe joeschmoe

2013/8/8

#
variable i not initialized
8bitcarrotjuice 8bitcarrotjuice

2013/8/8

#
Ok, try this:
public boolean slow=false; 
public void Eat(){    
    int i;   
    Actor actor = getOneIntersectingObject(Potato.class);    
       if(actor != null)    
           {    
           slow=true;    
           i=1;  
           getWorld().removeObject(actor);    
           }    
       if(slow==true)  
            i++;  
       if(i<5000){  
            Greenfoot.setSpeed(25); //adjust to your liking  
       }  
       else if(i>5000){  
            Greenfoot.setSpeed(50);  
            slow=false;  
}  
8bitcarrotjuice 8bitcarrotjuice

2013/8/8

#
Does it work?
joeschmoe joeschmoe

2013/8/8

#
same error. I did this and it compiled. It doesnt let you slide the speed bar if you havent caught a potato in a while but if you have you can slide it. It goes back after a few Acts though. public boolean slow; public void Eat(){ int i; Actor actor = getOneIntersectingObject(Potato.class); if(actor != null) { slow=true; i=1; getWorld().removeObject(actor); return; } else{i=5000;} if(slow==true) i++; if(i<5000){ Greenfoot.setSpeed(25); //adjust to your liking } else if(i>5000){ Greenfoot.setSpeed(50); slow=false; } }
8bitcarrotjuice 8bitcarrotjuice

2013/8/8

#
So can you compile it? Does it work? Please explain to me what the problen is! Does it go into slow motion?
joeschmoe joeschmoe

2013/8/8

#
No slow motion, I uploaded the game. kinda hard to explain. http://www.greenfoot.org/scenarios/9170
There are more replies on the next page.
1
2