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

2014/1/8

Need Help: How to increase speed

Maurice42 Maurice42

2014/1/8

#
(Sorry, for my bad English, I'm from Germany :P) Hi, I already have a counter. It counts up to 20 and you get +1 for every opponent you have shot.. When the counter reaches 20 it resets and a new level starts. (I have a second counter: a level counter) I want the opponents to move faster for each new level. How can I do that?
danpost danpost

2014/1/8

#
There are at least two way to do that. (1) increase the speed slider and put a delay between each movement which can be varied; (2) use double values for the location of the actor and use the 'setLocation' method to place the actor.
Maurice42 Maurice42

2014/1/8

#
The actor is already placed and it moves. It spawns randomly and then moves upside until it disappears. And I want him to increase his speed every level. Here is a picture of the setting:
danpost danpost

2014/1/8

#
Please show what code you have for this actor.
Maurice42 Maurice42

2014/1/8

#
"Fießling" means runt in German and "verschwinden" to disappear^^ Fießling is the name of the actor.
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
public class Fießling extends Actor
{
    public int direction;
   
    public Fießling (int direction)
    {
        this.setRotation (direction);
    }
 
    
    public void act()
    {
        move(3);
        verschwinden();
 
    }   
 
    public void verschwinden()
    {
        int y = (this).getY();
        if (y < 94) {
            getWorld().removeObject (this);
 
        }
    }
}
danpost danpost

2014/1/8

#
Simplifying what you have, we get this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import greenfoot.*;
 
public class Fießling extends Actor
{
    public Fießling(int direction)
    {
        setRotation(direction);
    }
 
    public void act()
    {
        move(3);
        verschwinden();
    }
 
    private void verschwinden()
    {
        if (getY() < 94) getWorld().removeObject(this);
    }
}
I removed the 'direction' field as it does not need to be saved. Once the rotation of the actor is set in the constructor, its value is no longer needed. Also, setting the y coordinate of the actor in a local field to be used just once creates more work than necessary for the system (setting up the local field). Calling it directly within the conditional 'if' statement does not cause the system to set up the local field. Now, to be able to change the speed, the '3' in line 12 needs to be made variable. Therefore, we will add an instance int field to the class called 'speed' and replace the '3' in line 12 with 'speed'.
1
2
// replace your 'public int direction;' with
private int speed;
Each time you create a Fießling object, it would need to know what the level is, so it can set its speed properly; so the constructor should now be:
1
2
3
4
5
public Fießling(int direction, int level)
{
    setRotation(direction);
    speed = 2+level; // '3+level' if the first level is '0'
}
Then, when you create an object of this class, use 'new Fießling(/* direction, level */)', putting the appropriate values (or expressions) in for the arguments.
Maurice42 Maurice42

2014/1/8

#
Okay, the Code looks now like this.
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
import greenfoot.*;
  
public class Fießling extends Actor
{
    private int speed;   
     
 
    public Fießling(int direction, int level) 
    
        setRotation(direction); 
        speed = 2+level;
    
 
     
      
    public void act()
    {
        move(speed);
        verschwinden();
 
    }   
 
    public void verschwinden()
    {
        int y = (this).getY();
        if (y < 94) {
            getWorld().removeObject (this);
 
        }
    }
}
But I get an error message from "Background": "constructor Fießling in class Fießling cannot be applied to given types; required: int, int; found: int; reason: actual and formal argument lists differ in length" How can I remove this error?
danpost danpost

2014/1/9

#
I will presume that 'Background' is the name of your world class. Please post the relavent code of that class that creates these runts.
Maurice42 Maurice42

2014/1/20

#
1
2
3
4
5
6
7
8
9
10
11
public void neuerFießling()
   {
       int direction = 270 ;
       int Bedingung = Greenfoot.getRandomNumber(101);
       int Koordinate = Greenfoot.getRandomNumber(654);
       int x = Koordinate + 478;
       if (Bedingung == 1) {
           addObject( new Fießling (direction) , x, 231) ;
       }
 
   }
You need to login to post a reply.