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

2014/3/7

I would like some help using For Loops

DarkMageCkmario DarkMageCkmario

2014/3/7

#
I am making a tower defense game for my intro to programming class, and i have code that uses the move and turn methods to get to the end of the track, but it skips directly to the end of the track. Here is the code for the for loop,
public class TEST_CODE extends basic_medspeed
{
    /**
     * this is the pathing app, write a code for "hits"
     */
    public void act() 
    {
        //pathing work make it move slowly
    setLocation(38, 2);
     
    for (int i = 0;i < 90; i++); 
    {move(1);
    Greenfoot.delay(1);}
    
    }    
}
and here is the code for the pathing that i need
public class basic_medspeed extends Actor
{
    /**
     * this is the pathing app, write a code for "hits"
     */
    public void act() 
    {
        //pathing work make it move slowly
    setLocation(38, 2);
    turn(90);
    move(15);
    turn(90);
    move(20);
    turn(-90);
    move(30);
    turn(-90);
    move(47);
    turn(-90);
    move(38);
    turn(90);
    move(20);
    turn(90);
    move(18);
    turn(-90);
    move(10);
    }    
}
I am very new to programming, so any help would be awesome
Pointifix Pointifix

2014/3/7

#
sure, its doing all this things withing one tick, so you will only get to see it on the end of the track. Always move your objects only once per tick, so void act is finished and then it will redraw your object, next tick it will move again and redraw and so on.
danpost danpost

2014/3/7

#
First, it seems there are some things you may not be aware of: (1) an 'act' method of either the 'World' or 'Actor' class is executed, basically, between display frames; - this means that only the end result is displayed (at the completion of the act method) (2) the 'delay' method of the 'Greenfoot' class stops execution of any code for number of unit time given - this means that you are delaying the execution of all act methods for an extended amount of time - you may see animation through your code using the 'for' and 'delay' because the screen has time to refresh during the delay What you need to understand is that the initial looping is inherent in the cycling of the act methods while the scenario is running. Only the details of the animation need to be dealt with (how far to move each act and when to turn, in your case). Try this;
public void act()
{
    move(1);
}
and run the scenario to see what your actor does. Then, try this
public void act()
{
    move(1);
    if (getRotation() == 0 && getX() == 128) turn(90);
}
Try to understand what is happen with both of these. The second line says: 'if moving to the right and reached the 128 mark, turn right (to go down)'.
Super_Hippo Super_Hippo

2014/3/7

#
The method 'act' is executed every act cycle. In your case, if you have a 'TEST_CODE' object, than it will always set to 38|2 and move 90 times. Do not use a loop or a delay there. The act method in the TEST_CODE object also overrides its superclass' act method, so it will not move/turn/move/turn and so on. If you create a basic_medspeed object, this object will also be placed to 38|2 and do all this moving and turning every act cycle and you only see the result of all that.
DarkMageCkmario DarkMageCkmario

2014/3/11

#
Thank you guys so much, the explanations really helped. I can continue working on this now! haha, it seems like a dumb question now. Thanks much
You need to login to post a reply.