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

2014/3/29

Help with sound

kasperk22 kasperk22

2014/3/29

#
Hello guys. In my game i have a car that is supposed to make different sounds. the sounds i want is the following: When the car is idle When the car is driving When the car is doing turbo When the car is reversing Here's my code for my car so far
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Skydemand here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class nyKøremand extends SmoothMover
{
    int change = 0;
    int change1 =0;
    int kør =2;
    int speed =5;
    GreenfootSound driving = new GreenfootSound("drive.wav");
    GreenfootSound turbo = new GreenfootSound("turbo.wav");
    GreenfootSound idle = new GreenfootSound("idle.wav");
    GreenfootSound reverse = new GreenfootSound("reverse.wav");
    String key = Greenfoot.getKey(); // saves the key returned
    /**
     * Act - do whatever the Skydemand wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        if (Greenfoot.isKeyDown("6")) change++ ;
        if (Greenfoot.isKeyDown("4")) change-- ;
        setRotation(change);
        change1=0;
        if (Greenfoot.isKeyDown("8")) change1=change1+kør ;
        if (Greenfoot.isKeyDown("5")) change1-- ;
        if(Greenfoot.isKeyDown("backspace")) change1=change1+speed ;
        move(change1);
        if (change1 ==0)  idle.playLoop();
        else idle.stop();
        if (change1 ==1)  driving.playLoop();
        else driving.stop();
        if (change1 ==-1)  reverse.playLoop();
        else reverse.stop();
        if (change1 ==5)  turbo.playLoop();
        else turbo.stop();
    }   
}
kasperk22 kasperk22

2014/3/29

#
oh, and ofc. the problem is that it's only the idle sound that works.
danpost danpost

2014/3/30

#
I think there are a couple of inherent problems here. First, when using 'isKeyDown' without any type of control, it is difficult to have a changing value stop at a particular number. The value of 'change' and 'change1' will probably be changing about 3 to 5 times for very quick keystrokes and a multitude of times if the key is pressed casually. You either need to change the method you are using from 'isKeyDown' to 'getKey' or put a timer control on the actions performs by the key conditions. Either way, you need more limitation of the value of the 'change1' field if only the four values are valid. Also, the 'change' field should be reset to zero at the beginning of the method (actually, it should be a variable that is local to the method instead of an instance field). Finally, line 19 will always (as far as I know) set 'key' to 'null'. As a single check for a key-up event, it would be virtually impossible to have a value other than 'null' set there. In order to catch a key-up event, you need to continually check for it by way of the act method or a method the act method calls. Keep in mind that once a key is returned using 'getKey', the internal keyboard buffer pointer is advanced to the next position and that last keystroke cannot be recalled by calling 'getKey' again. Therefore, if you need to check for more than one value for that key, you need to hold that returned key in a local String variable.
kasperk22 kasperk22

2014/3/30

#
alright then. I have no experience in using getKey. How would i use it in my code? I don't get what you mean by resetting change in the beginning of the code. wouldn't this just make the car unable to turn? The 19 line was actually just a test
danpost danpost

2014/3/30

#
I was thinking along these lines:
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
import greenfoot.*;
/**
 
public class nyKøremand extends SmoothMover
{
    int kør =2;
    int speed =5;
    GreenfootSound driving = new GreenfootSound("drive.wav");
    GreenfootSound turbo = new GreenfootSound("turbo.wav");
    GreenfootSound idle = new GreenfootSound("idle.wav");
    GreenfootSound reverse = new GreenfootSound("reverse.wav");
 
   public void act()
    {
        int change = 0;
        if (Greenfoot.isKeyDown("6")) change++ ;
        if (Greenfoot.isKeyDown("4")) change-- ;
        turn(change);
        int change1=0;
        if (Greenfoot.isKeyDown("8")) change1 = kør ;
        if (Greenfoot.isKeyDown("5")) change1 = -1;
        if (Greenfoot.isKeyDown("backspace")) change1 = speed ;
        move(change1);
        if (change1 ==0)  idle.playLoop();
        else idle.stop();
        if (change1 == kør)  driving.playLoop();
        else driving.stop();
        if (change1 ==-1)  reverse.playLoop();
        else reverse.stop();
        if (change1 ==speed)  turbo.playLoop();
        else turbo.stop();
    }   
}
Because we are not changing any values outside the method, the 'isKeyDown' method will work ok. Just needed to move the 'change' and 'change1' fields to inside the method as variables and use them to control the behavior. Also, using 'turn' instead of 'setRotation' seemed appropriate.
kasperk22 kasperk22

2014/4/1

#
ooh, i get it now :) thanks alot! But still, sometimes the sound does not get activated (and i get a nullpointer exception) after a turn or fast clicks. Is this because the GetKey is not in use?
danpost danpost

2014/4/1

#
You need to post the error message (in its entirety).
kasperk22 kasperk22

2014/4/4

#
1
2
3
4
5
6
7
8
9
10
11
at greenfoot.sound.SoundClip.processState(SoundClip.java:278)
    at greenfoot.sound.ClipProcessThread.run(ClipProcessThread.java:74)
    at java.lang.Thread.run(Thread.java:744)
Exception in thread "Thread-460" java.lang.NullPointerException
    at greenfoot.sound.SoundClip.processState(SoundClip.java:278)
    at greenfoot.sound.ClipProcessThread.run(ClipProcessThread.java:74)
    at java.lang.Thread.run(Thread.java:744)
Exception in thread "Thread-468" java.lang.NullPointerException
    at greenfoot.sound.SoundClip.processState(SoundClip.java:278)
    at greenfoot.sound.ClipProcessThread.run(ClipProcessThread.java:74)
    at java.lang.Thread.run(Thread.java:744)
kasperk22 kasperk22

2014/4/4

#
And here's the exact code for my car
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
import greenfoot.*; 
//* 
   
public class Køremand extends SmoothMover 
    int kør =2
    int speed =5
    GreenfootSound driving = new GreenfootSound("drive.wav");  
    GreenfootSound turbo = new GreenfootSound("turbo.wav");  
    GreenfootSound idle = new GreenfootSound("idle.wav");  
    GreenfootSound reverse = new GreenfootSound("reverse.wav");  
   
   public void act()  
    
        int change = 0
        if (Greenfoot.isKeyDown("6")) change++ ;  
        if (Greenfoot.isKeyDown("4")) change-- ; 
        turn(change); 
        int change1=0
        if (Greenfoot.isKeyDown("8")) change1 = kør ; 
        if (Greenfoot.isKeyDown("5")) change1 = -1
        if (Greenfoot.isKeyDown("backspace")) change1 = speed ; 
        move(change1); 
        if (change1 ==0)  idle.playLoop(); 
        else idle.stop(); 
        if (change1 == kør)  driving.playLoop(); 
        else driving.stop(); 
        if (change1 ==-1)  reverse.playLoop(); 
        else reverse.stop(); 
        if (change1 ==speed)  turbo.playLoop(); 
        else turbo.stop(); 
    }     
}
danpost danpost

2014/4/4

#
I cannot sure; but, it may be that you have too many sounds going at once. Or that you need to see if a sound is playing before trying to stop it. Maybe you need to see if a sound is not already playing before starting one as well. One thing I can say, is that you probably do not need to loop the sound as the act method will be executed continuously while the scenario is running and the actor is in the world. You might try changing 'speed' in lines 22 and 30 to '5' and set the value of 'speed' to 'change1' at the end of the method. Then, after line 23, if 'change1' is not equal to 'speed' (if the speed has change from the last known value) then stop the specific sound that may be playing (use the 'speed' value to determine which one it could be) and then start the new sound (using the 'change1' value). After that, you will set the 'speed' value to this new value of 'change1'.
kasperk22 kasperk22

2014/4/10

#
i'm sorry.. i don't think i understand what you mean by that... for me, it sounds like your method will do something else than i want. maybe it's because i use a mix of my language and english in my code... i changed the code now in all english.. Also what should be written instead of playLoop? here's the code:
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
import greenfoot.*; 
//* 
   
public class Køremand extends SmoothMover 
    int drive1 =2
    int turbo1 =5;
    int reverse1 =-1;
    int idle1 =0;
    GreenfootSound drive = new GreenfootSound("drive.wav");  
    GreenfootSound turbo = new GreenfootSound("turbo.wav");  
    GreenfootSound idle = new GreenfootSound("idle.wav");  
    GreenfootSound reverse = new GreenfootSound("reverse.wav");  
   
   public void act()  
    
        int change = 0
        if (Greenfoot.isKeyDown("6")) change++;  
        if (Greenfoot.isKeyDown("4")) change--; 
        turn(change);  //turn(amount) is basically setRotation(getRotation()+amount)
        int change1=0
        if (Greenfoot.isKeyDown("8")) change1 = drive1; 
        if (Greenfoot.isKeyDown("5")) change1 = reverse1; 
        if (Greenfoot.isKeyDown("backspace")) change1 = turbo1; 
        move(change1); 
        if (change1 ==idle1)  idle.playLoop(); 
        else idle.stop(); 
        if (change1 == drive1)  drive.playLoop(); 
        else drive.stop(); 
        if (change1 ==reverse1)  reverse.playLoop(); 
        else reverse.stop(); 
        if (change1 ==turbo1)  turbo.playLoop(); 
        else turbo.stop();
    }     
}
danpost danpost

2014/4/10

#
I was thinking something 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
32
33
34
35
36
37
38
39
import greenfoot.*; 
   
public class Køremand extends SmoothMover 
{
    int speed = 0;
    int drive1 =2
    int turbo1 =5;
    int reverse1 =-1;
    int idle1 =0;
    GreenfootSound drive = new GreenfootSound("drive.wav");  
    GreenfootSound turbo = new GreenfootSound("turbo.wav");  
    GreenfootSound idle = new GreenfootSound("idle.wav");  
    GreenfootSound reverse = new GreenfootSound("reverse.wav");  
   
   public void act()  
    
        int change = 0
        if (Greenfoot.isKeyDown("6")) change++;  
        if (Greenfoot.isKeyDown("4")) change--; 
        turn(change);  //turn(amount) is basically setRotation(getRotation()+amount)
        int change1=idle1; 
        if (Greenfoot.isKeyDown("8")) change1 = drive1; 
        if (Greenfoot.isKeyDown("5")) change1 = reverse1; 
        if (Greenfoot.isKeyDown("backspace")) change1 = turbo1; 
        move(change1);
        if (change1 != speed)
        {
            if (speed == idle1 && idle.isPlaying()) idle.stop();
            if (speed == drive1 && drive.isPlaying()) drive.stop();
            if (speed == turbo1 && turbo.isPlaying()) turbo.stop();
            if (speed == reverse1 && reverse.isPlaying()) reverse.stop();
            speed = change1;
            if (speed == idle1) idle.playLoop();
            if (speed == drive1) drive.playLoop();
            if (speed == turbo1) turbo.playLoop();
            if (speed == reverse1) reverse.playLoop();
        }
    }
}
With your original code the sound is being 'pinged' four times every act cycle, whether there was a change in the sound or not. With the method I supplied above, it is only 'pinged' twice and only when there is a change in the speed (or sound). I went ahead and used 'playLoop' because it is not continuously setting the sound to play (like what yours was doing -- which is why I suggested not to use it; but, the point is moot now). I cannot say for sure if it will behave like you want; but, it should be an improvement.
You need to login to post a reply.