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

2011/12/17

Question: Is there any API that can create a timer in Greenfoot?

1
2
b321234 b321234

2011/12/17

#
Actually I'm working on a shooting game, and I want to make the gun reloads. I created a method like
public void reload(){
        Greenfoot.playSound("clipout.wav");
        Greenfoot.playSound("clipin.wav");
        Greenfoot.playSound("boltpull.wav");
        bulletNum=0;
        canFire=1;
}    
It works but.... the sounds don't play one by one. They play almost simultaneously because the CPU is too fast lol So... What should I do?? Thankssss!!
dordor dordor

2011/12/17

#
Do an int variable (in my example called reloading). Replace in the reload method lines 2, 3 and 4, with this lines:
GreenfootSound sound1=new GreenfootSound("clipout.wav");  

GreenfootSound sound2=new GreenfootSound("clipin.wav");  

GreenfootSound sound3=new GreenfootSound("boltpull.wav");  

sound1.Play();

reloadLevel++;
and in the act method put this code:
if(reloadLevel==1)
{
     if(!sound1.isPlaying())
     {
          sound2.play();
          reloadLevel++;
     }
}
else if(reloadLevel==2)
{
     if(!sound2.isPlaying())
     {
          sound3.play();
          reloadLevel++;
     }
}
else if(reloadLevel==3)
{
     if(!sound3.isPlaying())
     {
          reloadLevel=0;
     }
}
Builderboy2005 Builderboy2005

2011/12/17

#
What you want is not a timer class or API, but a simple integer counter that is reset to 0 whenever the reload method is called. Every act cycle, you want to increase this counter by 1. Then you assign certain sounds to certain times in the counter. The first sound would likely be triggered when the counter is at 0 or 1, and then the second counter triggered at 60 or whatever sounds right. Once you have all your sounds set to specific times, everything should come together! This has the added effect of restarting the sound whenever the method is called. If this is not desired, you may want to simply use an audio program to combine all 3 sounds into 1, simplifying the problem immensely.
b321234 b321234

2011/12/17

#
@dordor, it says Play method not found
Builderboy2005 Builderboy2005

2011/12/17

#
are you capitalizing play()?
b321234 b321234

2011/12/17

#
@Builderboy2005 Thanks for the idea but I don't have any software like that..
b321234 b321234

2011/12/17

#
@Builderboy2005 yea that's what dordor wrote lol, changed P into lower case and it works. Thanks a lot!
Builderboy2005 Builderboy2005

2011/12/17

#
Looks like his is lowercase to me, but glad you got it working!
b321234 b321234

2011/12/17

#
It's working but now I have a logical problem... Can you help me take a look at the codes?
     int a=0;
        if(a==0){
            if (Greenfoot.isKeyDown("space")){
                if(bulletNum<=10){
                    if(canFire==1)
                    {
                        getWorld().addObject(new Bullet(),getX()-9,getY()-52);
                        Greenfoot.playSound("fire.wav");
                        canFire = 0;
                        bulletNum++;
                    }
                }
                else if(bulletNum>10)
                    a=1;
            }
            else if (!Greenfoot.isKeyDown("space")){
                canFire=1;
            }
        }
        if(a==1){
            if(bulletNum>10){
                canFire=0;
                reload();
            }
        }
My gun just doesn't stop firing even while reloading. And I can hear the reload sound. I stuck on this for more than half an hour. Just too stupid for this lol
danpost danpost

2011/12/17

#
You are trying to make things far more difficult than need be. You do not need the variable 'a' to qualify checking if reloading is neccessary; this should be done immediately after incrementing bulletNum. You have 'if' inside of 'if' inside of 'if' inside of 'if', with a couple of 'else if's. With a little re-arranging and some trimming, I came up with the following:
boolean spaceDown = Greenfoot.isKeyDown("space");
if (!spaceDown) canFire = 1;
if (spaceDown && canFire == 1 && reloadLevel == 0)
{
    getWorld().addObject(new Bullet(), getX() - 9,getY() - 52);
    Greenfoot.playSound("fire.wav");
    canFire = 0; 
    bulletNum++;
    if (bulletNum > 10)
    {
        reload();
        bulletNum = 0;
    }   
}
b321234 b321234

2011/12/18

#
@danpost, thank you very much, I ddn want to make things more complicated either. But I was just too stupid to think of a good idea. Yea boolean, it just never got into my head. Thanks again ;)
Builderboy2005 Builderboy2005

2011/12/18

#
Don't be too hard on yourself and call yourself stupid, we all go through a learning process and learn new things every day! Don't look at this as an exercise in stupidity, but look at it like a learning experience :D Trust me, keep at it and you will get better ^^
b321234 b321234

2011/12/18

#
@Builderboy2005, thanks for the encouragement! Actually I've learned Java for only 3 months, and I want to learn greenfoot myself. Still have many things that I don't understand, but I'll try my best lol Thanks again!
danpost danpost

2011/12/18

#
I have learned much just looking over some of the discussion here (not that I will use most of it)!
dordor dordor

2011/12/18

#
b321234 wrote...
@Builderboy2005 yea that's what dordor wrote lol, changed P into lower case and it works. Thanks a lot!
Sorry for the confusion. You can see that in the other times I wrote play() and not Play(). In any case, sorry.
There are more replies on the next page.
1
2