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

2020/10/8

How to Increase Speed Over Time

CuzImMicah CuzImMicah

2020/10/8

#
Hello, I have a school project for APCS and I want to figure out how I can increase the speed of an object over time as it is in the world. It is a game that you need to collect strawberries that fall from the sky randomly. I need the speed of the strawberry to increase as it moves down the screen. Other than that thanks
danpost danpost

2020/10/8

#
Show Strawberry class codes (entire page please).
CuzImMicah CuzImMicah

2020/10/8

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Strawberry here.
 *
 * @author (Micah Alpuerto)
 * @version (v1.0)
 */
public class Strawberry extends Actor
{
    /**
     * Stuff
     */
     
    public void act()
    {
         
        moveDown();
         
        droppedStrawberry();
         
    
     
    private void moveDown() {
        int x = getX();
        int y = getY();
        int newY = getY()+5 ;
         
        setLocation(x,newY );
        }
     
    private void droppedStrawberry() {
         
        if (isAtEdge()) {
             
            removeSelf();
             
        }
    }
     
    public void removeSelf() {
        getWorld().removeObject(this);
    }
     
}
danpost danpost

2020/10/8

#
In line 27, you have a literal "+5" for the speed. To make it variable, add a field to represent the speed (set to initial speed):
1
2
// at line 14
private int speed = 5;
Then replace "+5" with "speed". You can now adjust the value using:
1
2
3
speed++;
// or
speed = speed+1; //(plus any value)
danpost danpost

2020/10/8

#
To have all strawberries maintain like speeds, modify speed declaration line to:
1
public static int speed = 5;
CuzImMicah CuzImMicah

2020/10/8

#
Oh, I didn't realize it was that easy. Thanks lol
danpost danpost

2020/10/8

#
With "public static", you can modify its value from ANY class, at any time:
1
2
3
Strawberry.speed++;
// or
Strawberry.speed = Strawberry.speed+1; // (plus any value)
CuzImMicah CuzImMicah

2020/10/8

#
Ok so its kinda like a global statement?
CuzImMicah CuzImMicah

2020/10/8

#
I also have another question. When I used this command how can I make it so that this is in a sort of while loop thing. Because Its not letting me use addObject in an Actor Class for some reason.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
    /**
     * Write a description of class PenguinWorld here.
     *
     * @author (your name)
     * @version (a version number or a date)
     */
      public class PenguinWorld extends World
     {
     
    
      /**
      * Constructor for objects of class PenguinWorld.
      *
      */
      public PenguinWorld()
      {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
          super(996, 616, 1);
           
          runGame();
          rndmDrops();
           
        }
        
     
        int rndmX = Greenfoot.getRandomNumber(getWidth()) ;
     
      public void runGame() {
         
          int i = 1 ;
         
          while ( i <= 4 ) {
             
            int rndmX = Greenfoot.getRandomNumber(getWidth()) ;
         
            addObject(new Strawberry() , rndmX, 25 );
         
            i++ ;
        }
         
      }
         
      private void rndmDrops() {
             
         if ( Greenfoot.getRandomNumber(100) > 0 ) {
             
            addObject(new Strawberry() , rndmX, 25 );
 
           }
             
         }
    }
danpost danpost

2020/10/8

#
CuzImMicah wrote...
Ok so its kinda like a global statement?
It is called a class variable when using "static". It is not specific to any one instance of the class. Oh, and be careful with them. They are not re-initialized when your scenario is reset, only when re-compiled. Add the following line to your world constructor:
1
Strawberry.speed = 5;
CuzImMicah wrote...
When I used this command how can I make it so that this is in a sort of while loop thing. Because Its not letting me use addObject in an Actor Class for some reason. << Code Omitted >>
Remove line 28 (it is not being used as line 36 declares a local variable that is not in any way related to the instance field line 28 declared -- other than having the same name). Line 23 is in the world constructor and therefore is only called one time (when the world is first created). To have it continuously called, place it in an act method (there is an act method for your World objects as well as your Actor objects). Your inequality will need attention on line 47.
CuzImMicah CuzImMicah

2020/10/9

#
I cannot use addObject in a Actor class for some reason. Is this regular?
danpost danpost

2020/10/9

#
CuzImMicah wrote...
I cannot use addObject in a Actor class for some reason. Is this regular?
Probably, the answer is yes, providing the way you are using it. The addObject method is a World instance method, meaning it must be executed by a World instance. Used in a World subclass, it might look as follows:
1
2
3
4
5
6
7
8
this.addObject(new MyActor(), 50, 100);
 
// or, expanded as
Actor myActor = new MyActor();
this.addObject(myActor, 50, 100) // "this" being the World instance the actor is placed into;
 
// or, condensed as
addObject(new MyActor(), 50, 100); // "this." being implicitly understood
From an Actor subclass, with an actor that is in a world, you can use:
1
2
3
4
5
World world = getWorld(); // or "= this.getWorld()"
world.addObject(new MyActor(), 50, 100);
 
// or, condensed as
getWorld().addObject(new MyActor(), 50, 100);
CuzImMicah CuzImMicah

2020/10/9

#
Ok, and one last question, I'm trying to add Strawberries randomly but it is not working. Either it does way too many when I change the inequality or it does absolutely nothing.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Igloo here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Igloo extends Actor
{
    /**
     * Act - do whatever the Igloo wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
         
      rndmDrops();
       
    
    
     
    public void rndmDrops() {
             
         if ( Greenfoot.getRandomNumber(100) > 75 ) {
             
           int i = 0 ;
           boolean whileOff = false;
            
           whileOff = false ;
         
          while ( whileOff = false ) {
             
            int rndmX = Greenfoot.getRandomNumber(996) ;
         
            getWorld().addObject(new Strawberry() , rndmX, 25 );
             
            if( i <= 2 ) {
                 
                whileOff = true ;
                 
            }
             
            i++ ;
             
           }
            
           Greenfoot.delay(1);
            
 
           }
            
             
         }
     
     
    
}
danpost danpost

2020/10/9

#
Remove line 48, as it will delay everything -- not just the Igloo actor. You only need one control value for spawning strawberries -- not two ( "i" and "whileOff" ) -- keep 'i':
1
2
3
4
5
6
7
int i = 0;
while ( i < 2 )
{
    int rndmX = Greenfoot.getRandomNumber(996);
    getWorld().addObject(new Strawberry(), rndmX, 25);
    i++;
}
(the above to replace from line 27 to 48). Raise "75" in line 25 to something in the mid to high 90s.
You need to login to post a reply.