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

2015/7/12

Loops...

1
2
ILOVELABRADORS ILOVELABRADORS

2015/7/12

#
Greetings people, Is it possible if one of you post a sample of a for loop for me please? Any help would be appreciated! Thank you for your time! :)
danpost danpost

2015/7/12

#
You can find examples here.
ILOVELABRADORS ILOVELABRADORS

2015/7/12

#
Thanks! That helped! But I have one more question. Uh, can someone post the coding for making an actor alternate pictures in a loop? I tried, but I think I left a few things out. Oops! Thnx!
danpost danpost

2015/7/12

#
ILOVELABRADORS wrote...
can someone post the coding for making an actor alternate pictures in a loop? I tried, but I think I left a few things out.
You need to post the code that you tried.
ILOVELABRADORS ILOVELABRADORS

2015/7/13

#
Here's the code...
class ForDemo {
            public void act(String[ ] args) {
                for(int i=1; i<100; i++){
                    if (getImage() == image1)
                    {
                        setImage(image2);
                    }    
                    else
                    {
                        setImage(image1);
                    }    
                }    
           }
        }
I thought of replacing the "String" and the "args" with something else, but when I compile it, it says it's an illegal start of expression. When I don't replace it, it compiles just fine but nothing happens.
ILOVELABRADORS ILOVELABRADORS

2015/7/13

#
I don't know what's wrong and why it's not working.
danpost danpost

2015/7/13

#
You should probably show the entire class code (preferably, the code that compiles). Lines 1 and 2 seem out of sorts for code belonging to an Actor subclass. The for loop looks to be properly created (the loop will execute 99 times, however); but, the act method of neither the World or Actor class use a parameter. The loop will run in its entirety within one act cycle; so visually, if any change occurs, it will be seen as one single change. It really depends on what this animation is doing; that is, when and how long it is to execute. The speed is something else that needs considered.
ILOVELABRADORS ILOVELABRADORS

2015/7/13

#
The entire class code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * IT RESEMBLES TO... CHEESE.
 * 
 * @author (YOU EAT CHEESE) 
 * @version (YOU EAT MORE CHEESE)
 */
public class Happyface extends Actor
{
    private GreenfootImage image1;
    private GreenfootImage image2;
    /**
     * The happy face changes from a happy face to a sad face when
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        for(int i=1; i<100; i++){
                    if (getImage() == image1)
                    {
                        setImage(image2);
                    }    
                    else
                    {
                        setImage(image1);
                    }    
        }    
    }    
}
I removed the
class ForDemo {
            public void act(String[ ] args) {
part. It compiled, but when I pressed the run button, my object disappeared.
danpost danpost

2015/7/13

#
Still, you have not yet assigned any GreenfootImage objects to the 'image1' and 'image2' fields. Also, the 'for' loop needs replaced with an 'if' condition that is controlled by the value of an 'int' field (the counter).
ILOVELABRADORS ILOVELABRADORS

2015/7/14

#
Um, I'm a terrible programmer. Is it possible if you can explain or maybe give a few examples? (Right now, I'm panicking because my project is due at the end of tomorrow and I'm not even halfway through!)
davmac davmac

2015/7/14

#
First: You have declared two image variables:
    private GreenfootImage image1;
    private GreenfootImage image2;
But, you have never assigned anything to them (image1 = ?; image2 = ?), so while the variables do exist, they don't hold a reference to any actual image. Second: You alternate between the two images without any delay. The images will change so fast that you will not see it. You need to insert a delay.
(Right now, I'm panicking because my project is due at the end of tomorrow and I'm not even halfway through!)
It sounds like you have left your task too late. You need to understand that this is not anyone's fault but your own. Please do not transfer this time pressure on people who are helping you from goodwill.
davmac davmac

2015/7/14

#
danpost:
Also, the 'for' loop needs replaced with an 'if' condition that is controlled by the value of an 'int' field (the counter).
This isn't strictly correct. The technique of checking which image is currently active and then switching to the other one should be fine (it might not work correctly on the first iteration, but that could be remedied by setting the image to image1 before the loop starts).
danpost danpost

2015/7/14

#
davmac wrote...
The technique of checking which image is currently active and then switching to the other one should be fine (it might not work correctly on the first iteration, but that could be remedied by setting the image to image1 before the loop starts).
It is not the first iteration that was my concern -- but the following iterations. The end result of the 'act' method will be one of two states. Any odd number of iterations will change the image and any even number will keep the image the same. There are only two results. One can be reached by not doing anything and the other can be reached by one iteration (which can be done without a loop).
davmac davmac

2015/7/14

#
Oh, I think the task requires actually using a loop:
ILOVELABRADORS wrote...
Uh, can someone post the coding for making an actor alternate pictures in a loop? I tried, but I think I left a few things out. Oops!
I'm assuming this is a set task which states that a loop should be used. As such the loop above is ok, but a delay needs to be inserted so that the change in the image is visible. But yes, if you wanted the image to alternate as the scenario was running (i.e. while the other actors were also acting) then you would not use a loop.
ILOVELABRADORS ILOVELABRADORS

2015/7/14

#
I changed my coding to this
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * IT RESEMBLES TO... CHEESE.
 * 
 * @author (YOU EAT CHEESE) 
 * @version (YOU EAT MORE CHEESE)
 */
public class Happyface extends Actor
{
    private GreenfootImage smiley1 = new GreenfootImage("smiley1.png");
    private GreenfootImage smiley3 = new GreenfootImage("smiley3.png");
    
    private boolean image1 = false;
    
    /**
     * The happy face changes from a happy face to a sad face when
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        change();
    }
   
    public void change()
    {
        for(int i=1; i<101; i++){
                    if (image1) {
                        setImage( "smiley1.png" );
                    } else {
                        setImage( "smiley3.png" );
                    }    
        }    
    }
}    
it compiled and when I ran it, it did change into a frowning face, except it didn't repeat.
There are more replies on the next page.
1
2