Hi, It's been a while since I last programmed and now ... I'm stuck.
I'm trying to get an object to move based on timings. But such code should only be executed in random order (as I tried to do in the "crush" method. However once the whole process completes in the "timerUp method, I want it to loop back into the "pauseTimer" method and I am unable to do so. I'm not sure if it's because the values of "pauseTimer" don't reset and therefore it gets messed up or if it is something else. I'm pretty confused to say the least, sorry for the messy code.
Here's the code:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Crusher extends Crush
{
private int timer = 10;
private int timer2 = 151;
private int timer3 = 300;
private int pause = 100;
/**
* Act - do whatever the Crusher wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
mover2();
pauseTimer();
//timerDown();
//turn2();
//timerUp();
}
/**
* Check whether the control keys are being pressed, and turn if they are.
*/
public void mover2()
{
if (Greenfoot.isKeyDown("G"))
{
setLocation(getX(), getY()+20);
}
if (Greenfoot.isKeyDown("T"))
{
setLocation(getX(), getY()-5);
}
}
public void crush()
{
if (Greenfoot.getRandomNumber(100)<10)
{
timerDown();
timerUp();
}
else
{
pauseTimer();
}
}
public void pauseTimer()
{
if(pause>0)
pause--;
if(pause == 0)
{
crush();
}
if ( pause < 0) pause = 100;
}
public void timerDown()
{
if (timer < 150)
{
setLocation(this.getX(), this.getY() + 4);
timer++;
}
}
public void timerUp()
{
timer3--;
if (timer3 < 150 && timer3 > - 150)
{
setLocation(this.getX(), this.getY() - 4);
timer3--;
}
if (timer3 ==-150) pauseTimer();
}
}
