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

2 days ago

timer issue

Haelena Haelena

2 days ago

#
I want to use an int timer for jumping animation (see the example of wished result in jabka.wip on my page, precisely on the jump to the left (I used delay() there, which now I want to avoid). However, the timer doesn't work at all. Any ideas of how to fix it? public class Jabka extends Actor { private int a; private int ticker=1000; //instead of delay private int timer=0; //for jumping animation private boolean facingLeft=false; private int ground=350; private boolean idle=true; public void act(){ String key=Greenfoot.getKey(); if(key != null && key.equals("d") && idle)jumpRight(); if(key != null && key.equals("a") && idle)jumpLeft(); if(Greenfoot.isKeyDown("s"))dive(); if(Greenfoot.isKeyDown("w") && idle) strike(); if(!Greenfoot.isKeyDown("s") && !Greenfoot.isKeyDown("w") && !idle) rest(); } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public void jumpRight(){ turn(-90); a = getX(); setImage("jabka.skin.2.png"); while (timer<36){ if (getX()<=595){ if(ticker > 0) ticker--; else{ turn(5); move(4); timer++; ticker=200; } }else{ setLocation(0,getY()-50); a = getX()-100; setRotation(45); timer+=28; } } turn(-90); setRotation(0); setLocation(a+100,ground); setImage("jabka.skin.1.png"); timer=0; facingLeft=false; } public void jumpLeft(){ turn(90); a = getX(); setImage("jabka.skin.2.png"); getImage().mirrorHorizontally(); while (timer<36) { if (getX()>=1){ turn(-5); move(-4); Greenfoot.delay(1); timer++; }else{ setLocation(600,getY()-50); a = getX()+100; setRotation(-45); timer+=28; } } setRotation(0); setLocation(a-100,ground); setImage("jabka.skin.1.png"); getImage().mirrorHorizontally(); timer=0; facingLeft=true; }
danpost danpost

yesterday

#
Haelena wrote...
I want to use an int timer for jumping animation (see the example of wished result in jabka.wip on my page, precisely on the jump to the left (I used delay() there, which now I want to avoid). However, the timer doesn't work at all. Any ideas of how to fix it? << Codes Omitted >>
It seems you have coded an entire jump into one act step. You have coded the act method too generally. It needs to provide what needs to be done at any one moment. A jump command (by keyboard activation) should start a jump. The jump must be controlled through multiple act steps. You may want to check out the codes in my Jump and Run Demo w/Moving Platform Just run the scenario and click bottom screen buttons to view codes.
You need to login to post a reply.