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

2015/1/5

Delay?

1
2
xFabi xFabi

2015/1/5

#
So basically, since there is no delay function i made this code, in order to not shoot infinite bullets a sec
private void schießen()
    {    

        if(Greenfoot.isKeyDown("right"))
        {
            if(pause>0)
            {
                pause--;
            }
            if(pause == 0)
            {
                sm();
                pause=20;
            }
        }
        else if(Greenfoot.isKeyDown("left"))
        {
            if(pause>0)
            {
                pause--;
            }
            if(pause == 0)
            {
                sml();
                pause=20;
            }
        }
        else if(Greenfoot.isKeyDown("up"))
        {
            if(pause>0)
            {
                pause--;
            }
            if(pause == 0)
            {
                smu();
                pause=20;
            }
        }
        else if(Greenfoot.isKeyDown("down"))
        {
            if(pause>0)
            {
                pause--;
            }
            if(pause == 0)
            {
                smd();
                pause=20;
            }
        }
But since my pause value only gets decreased while holding down the key, in this code, i added
        while(pause<0)
        {
            pause--;
        }
into my method, so i couldnt instantly shoot one bullet, when i didnt fire one for a certain time. It still wont shoot instantaneously when i dont shoot for some time. Wheres my fault :c?
xFabi xFabi

2015/1/5

#
~Forgot to add, looks like this in final~
private void schießen()
    {    
        while(pause<0)
        {
            pause--;
        }

        if(Greenfoot.isKeyDown("right"))
        {
            if(pause>0)
            {
                pause--;
            }
            if(pause == 0)
            {
                sm();
                pause=20;
            }
        }
        else if(Greenfoot.isKeyDown("left"))
        {
            if(pause>0)
            {
                pause--;
            }
            if(pause == 0)
            {
                sml();
                pause=20;
            }
        }
        else if(Greenfoot.isKeyDown("up"))
        {
            if(pause>0)
            {
                pause--;
            }
            if(pause == 0)
            {
                smu();
                pause=20;
            }
        }
        else if(Greenfoot.isKeyDown("down"))
        {
            if(pause>0)
            {
                pause--;
            }
            if(pause == 0)
            {
                smd();
                pause=20;
            }
        }
    }
danpost danpost

2015/1/5

#
For one thing, it does not appear that the value of 'pause' will ever be less than zero (all changes either set it to a positive value or decrease it from a positive value). That being said, the 'while' loop will never execute (good thing!, as it would loop infinitely and freeze your program because decreasing its value will never make the condition -- that it be less than zero -- false). The running of the delay, which you have duplicated in each keycheck code only need be move to before the keycheck code:
public void schießen()
{
    if (pause > 0)
    {
        pause--;
    }
    else if (Greenfoot.isKeyDown("right"))
    {
        sm();
        pause = 20;
    }
    else if (Greenfoot.isKeyDown("left"))
    {
        sml();
        pause = 20;
    }
    else if // etc.
}
xFabi xFabi

2015/1/5

#
well i wrote pause<0 instead of pause>0 ._. omg i dont understand what you mean, by moving pause
danpost danpost

2015/1/5

#
xFabi wrote...
i dont understand what you mean, by moving pause
I started it for you in my last post.
xFabi xFabi

2015/1/5

#
still dont get it :c so i dont need this parts? But it wouldnt consider the pause value for the else-if methods then, would it? Since i require every method to wait for pause to be 0
else if(Greenfoot.isKeyDown("down"))
        {
            if(pause>0)  <-
            {
                pause--;  <-   this?
            }
            if(pause == 0) <-
            {
                smd();
                pause=20;
            }
public void schießen()
{
    if (pause > 0)
    {
        pause--;
    }
    else if (Greenfoot.isKeyDown("right"))
    {
        sm();
        pause = 20;
    }
    else if (Greenfoot.isKeyDown("left"))
    {
        sml();
        pause = 20;
    }
    else if // etc.
}
danpost danpost

2015/1/5

#
As I have written it, the value of pause will decrease if greater than zero; otherwise (else; or, if it is zero) the keys will be checked.
xFabi xFabi

2015/1/5

#
Okay, got that by now, but this is just beauty, and not related to my actual problem -the value not continuously decreasing- is it? (Sorry to bother you, if it is, and I'm just too dumb to understand ._.)
danpost danpost

2015/1/5

#
It only decreases until it becomes zero; then it will stay zero until one of the keys is detected and its value is set back to 20, where it will then start decreasing again.
xFabi xFabi

2015/1/5

#
Well that's exactly my problem, it doesn't decrease T_T And btw my sm smd/r/l/u methods are shooting methods so it should be shooting anyway :( Kinda confused now..
danpost danpost

2015/1/5

#
Is the 'schießen()' method being called from the act method?
xFabi xFabi

2015/1/5

#
Yes it is.
danpost danpost

2015/1/5

#
Please post the current code for the class.
xFabi xFabi

2015/1/5

#
Everything? The Shootings Method is the 'final' i Posted at the top I just put schießen(); in the act() then, Thats it. I Cant copy anything Else because i Cant access my pc atm I can Post it tomorrow, if youd like. Still thanks for your help by Now
danpost danpost

2015/1/5

#
If you cannot access you pc, then how do you know the value of 'pause' does not decrease?
There are more replies on the next page.
1
2