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

2018/2/22

Need help with spamming game

iminblue iminblue

2018/2/22

#
Hey guys I'm trying to create a game about the Olympics and I encountered a problem. My idea was to make the runner move by alternating the right and left keys . Sadly , if you hold down both keys the runner will shoot his way towards the finish line , making the whole game useless. So here's my code :
  public int myplace=0;
    public int ok=0;
    public int frame=0;
    public int MYPLACE=0;
    public void act() 
    {
        // Add your action code here.
        
        PLACE place = (PLACE) (getWorld().getObjects(PLACE.class).get(0));
        if (Greenfoot.isKeyDown("left") && ok==0 && nr==10) {ok=1; move (5); frame++; framing (); }
        else if (Greenfoot.isKeyDown("right") && ok==1 &&  nr==10) {ok=0;move(5); frame++; framing ();} 
        if (getY()<=38 && MYPLACE==0) {MYPLACE=place.place;
         OlimpFinishMessege olimpfinishmessege = new OlimpFinishMessege();
        getWorld().addObject(olimpfinishmessege,715,399);}
            

         
    }
   

 public void framing ()
 {if (frame%4==1) setImage ("doesthisrun2.png");
  else if (frame%4==3) setImage ("doesthisrun3.png");
  else setImage ("doesthisrun.png");
    }
Please help . Thanks in advance !
danpost danpost

2018/2/22

#
Allow 4 values for the 'ok' field -- namely '0', '1', '2' and '3'. Then you can increment it when a key is detected and increment it again when the key is released. Only move when the value is even. When it is '0' accept one of the keys and when it is '2' accept the other. Increment its value when moving and when the last movement key is released. Set it back to '0' instead of the increment to '4'. The following is the basic idea without your framing and whatever 'nr' does:
switch (ok)
{
    case 0: if (Greenfoot.isKeyDown("left")) { move(); ok++; } break;
    case 1: if (!Greenfoot.isKeyDown("left")) ok++; break;
    case 2: if (Greenfoot.isKeyDown("right")) { move(); ok++; } break;
    case 3: if (!Greenfoot.isKeyDown("right")) ok = 0; break;
}
iminblue iminblue

2018/2/23

#
danpost wrote...
Allow 4 values for the 'ok' field -- namely '0', '1', '2' and '3'. Then you can increment it when a key is detected and increment it again when the key is released. Only move when the value is even. When it is '0' accept one of the keys and when it is '2' accept the other. Increment its value when moving and when the last movement key is released. Set it back to '0' instead of the increment to '4'. The following is the basic idea without your framing and whatever 'nr' does:
switch (ok)
{
    case 0: if (Greenfoot.isKeyDown("left")) { move(); ok++; } break;
    case 1: if (!Greenfoot.isKeyDown("left")) ok++; break;
    case 2: if (Greenfoot.isKeyDown("right")) { move(); ok++; } break;
    case 3: if (!Greenfoot.isKeyDown("right")) ok = 0; break;
}
Thank you so much man it worked ! Appreciate the help ! Have a good day !
You need to login to post a reply.