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

2015/7/13

How to make a method execute 50 times then stop.

Minion1 Minion1

2015/7/13

#
I have no code to display, since I haven't been able to get this to work properly at all. I don't know the right "methodology" to set up the code in. Here is what I want to happen: There is a character on the screen. When you press the key, I would like this actor to move exactly 1 cell per cycle, with each cycle being redrawn, until 50 cells have been achieved then stop. That's it. To date, I haven't been able to set up a code that would do this, every loop I try to use cycles itself completely 50 times before being redrawn on the screen. What I want is for each movement of each cell to be redrawn so that I see the character move 50 cells exactly then cease until the button is pressed again. I intend to then animate the character in between those movements. Any help would be appreciated. Thanks in advance.
Minion1 Minion1

2015/7/13

#
Also, I forgot to include that I need this to happen when the key is up. In other words, you press the key one time, the character completes a movement of 50 cells then stops, and you do NOT have to hold the key down for this to happen.
danpost danpost

2015/7/13

#
Do not use your own loop for this. Use the "loop" created by greenfooot repeatedly calling the act method.
Minion1 Minion1

2015/7/13

#
It's been years danpost. Good to hear from you again. I thought about using the act method to continually repeat the movement and then stop after a "timer" had been depleted, but I can't seem to get that to work either. I've been away from programming for some time, and was never really good at it the first time. I'm finding it difficult to structure the code correctly.
danpost danpost

2015/7/13

#
Minion1 wrote...
I thought about using the act method to continually repeat the movement and then stop after a "timer" had been depleted, but I can't seem to get that to work either.
You should show what you tried so that you can learn from the fix.
Minion1 Minion1

2015/7/13

#
public void moveLeft() { setLocation(this.getX() - 1, this.getY()); timer++; } public void act() { String key = Greenfoot.getKey(); if ("a".equals(key) && timer < 50) { moveLeft(); } } Here is what my latest attempt was. I realize the problem is that the "if" statement in the act method is checking to see if the "a" key IS BEING pressed. What I want to do is find a way to make the "a" key the "Trigger" not the "condition" of the action.
Minion1 Minion1

2015/7/13

#
Ok, I think I figured out a way. It might be sloppy, but for now it will work. I inserted a boolean variable called "keyPressed" that begins as false, but will set itself to true once the key was pressed. The act method will then call the method "moveLeft" as long as the variable is true. Once the timer reaches 50, it is reset to 0 and the boolean is reset to false. Sloppy perhaps.. but working for now.
public void checkKeyPress()
    {
        String key = Greenfoot.getKey();
        if ("a".equals(key))
        {
            keyPressed = true;
        }
    }

    public void moveLeft()
    {      
        if (keyPressed == true && timer < 50)
        {
            setLocation(this.getX() - 1, this.getY());
            timer++;
        }
        else
        {
            timer = 0;
            keyPressed = false;
        }
    }

    public void act() 
    {
        checkKeyPress();
        moveLeft();
    }    
danpost danpost

2015/7/13

#
You need both conditions you have in your if clause; but both should not be used together. One is for the trigger; the other is for moving. The question you need to ask is this, "When the trigger is detected (when the key is pressed), what needs to be done to set the movement into action?" The answer to that may be obvious after you ask this, "What timer condition is required whereby the actor should move?" EDIT: The previous was started and finished before seeing your last post. You should not need the 'keyPressed' field -- the value of timer should be sufficient to determine what value it would have.
danpost danpost

2015/7/13

#
A simple coding might be:
public void act()
{
    String key = Greenfoot.getKey();
    if (timer == 0 && "a".equals(key)) timer = 50; // the trigger
    if (timer > 0) // the movement
    {
        timer--;
        setLocation(getX()-1, getY());
    }
}
Minion1 Minion1

2015/7/13

#
Well it seems that now I have a new problem. I'm now using booleans to tell the act method when to move, and for how long, in each direction (left, right, up, down). The code you see here is not working. For some reason moving to the right (the D key) works. But all the rest (S, A, W) only move one pixel then stop.
public void checkKeyPress()
    {
        String key = Greenfoot.getKey();
        if ("a".equals(key))
        {
            aKeyPressed = true;
        }
        if ("w".equals(key))
        {
            wKeyPressed = true;
        }
        if ("s".equals(key))
        {
            sKeyPressed = true;
        }
        if ("d".equals(key))
        {
            dKeyPressed = true;
        }
    }

    public void moveHero()
    {      
        if (aKeyPressed == true && timer < 50)
        {
            setLocation(this.getX() - 1, this.getY());
            timer++;
        }
         if (wKeyPressed == true && timer < 50)
        {
            setLocation(this.getX(), this.getY() - 1);
            timer++;
        }
         if (sKeyPressed == true && timer < 50)
        {
            setLocation(this.getX(), this.getY() + 1);
            timer++;
        }
         if (dKeyPressed == true && timer < 50)
        {
            setLocation(this.getX() + 1, this.getY());
            timer++;
        }
        else
        {
            timer = 0;
            aKeyPressed = false;
            sKeyPressed = false;
            wKeyPressed = false;
            dKeyPressed = false;
        }
    }

    public void act() 
    {
        checkKeyPress();
        moveHero();
    }    
Minion1 Minion1

2015/7/13

#
Ha. Never mind. Solved it. Some of the old tricks are coming back to me. I changed all the additional "if" statements to "else if" statements, and now it works like a charm. Thank you for the help danpost. I'm sure my code is still sloppy, but at this point I'm just happy that it's working.
You need to login to post a reply.