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

2014/7/12

addObject (new Face) populates the wrong face.

dhollan8 dhollan8

2014/7/12

#
Everything else is correct, But the smiley face that keeps loading when I reset is the smile, I need it to be the frown. My code is: public void populateWorld() { for (int i =1; i<=7; i++) addObject (new Face(), i*64,44;); for (int i =1; i<=7; i++) addObject (new Me(), i*70,536); } And when I play it. The Smile turn to a frown when the me finds it. But it should be turning from a frown to a smile. So again, it works the way it is supposed to, but because it starts out in reverse. that is incorrect. That code is: public void lookForMe() { if (canSee(Me.class)) { GreenfootImage tempImage; tempImage = getImage(); if (tempImage == image1) { setImage(image2); } else { set Image (image1); } } I have tried removing the images and removing the sub class and re-writing it, But it still populates with the smile. I have tried to switch the public void looForMe() the 1s for the 2s and vise versa. but it still populates with the smile. I NEVER thought I would be so disappointed to see that many smiles in my life. Any ideas as to how to switch this? Thank You in advance Anyone have any ideas?
danpost danpost

2014/7/12

#
You need to show the code for the entire Face class (not just the 'lookForMe' method. Use the 'code' tag below the 'Post a reply' box to insert your code into the post.
dhollan8 dhollan8

2014/7/12

#
public class Face extends Animal
{
    //instance variables...fields
    private GreenfootImage image1;
    private GreenfootImage image2;
    
    public Face()
    {    //constructor is only done once in the life of Face
        image1 = new GreenfootImage("smiley3.png");
        image2 = new GreenfootImage("smiley1.png");
        setImage (image1);
        setImage (image2);
    }
    
    public void act() 
    {
        move();
        turnAtEdge();
        randomTurn();
    }

    public void randomTurn()
    {
        //code to randomly turn the Face
        if (Greenfoot.getRandomNumber(100)<10)
        {
            //turn a random number of degrees bet. 0 and 45
            turn (Greenfoot.getRandomNumber(90)-45);
        }
        move();
    }
    
    public void lookForMe()
    {
        if (canSee(Me.class))
        {
        GreenfootImage tempImage;
        tempImage = getImage(); 
        if (tempImage == image1)
        {
            setImage(image2);
        }
        else
        {
            setImage(image1);
        }
    }
    }

    public void turnAtEdge()
    {
        if (atWorldEdge())
        {
            turn (17);
        }
    }
    }    
dhollan8 dhollan8

2014/7/12

#
And here is the world that I am trying to populate that is returning the smile and not the frown. I even closed it down, Started all over again. and guess what, STILL with the smiles. So that tells me that I am doing something backwards. When I inspect it, it even tells me that the wrong png is in there.
    public CrabWorld() 
    {
        super(560, 560, 1);
        //addObject(new Face()
        //addObject (new Me()
        populateWorld();
    }

    public void populateWorld()
    {
        for (int i = 1; i<=7; i++)
        {
            addObject(new Face(), i*64, 44); 
            addObject(new Me(), i*70, 536);
        }
       
    }
danpost danpost

2014/7/13

#
Remove line 12 from snippet of the Face class above. Then change the 'lookForMe' method to this:
public void lookForMe()
{
    if (getImage() == image1 && canSee(Me.class)) setImage(image2);
    // to have it change back when the Me object no longer intersects,
    // add the following line
    if (getImage() == image2 && !canSee(Me.class)) setImage(image1);
}
Still, from what you gave above, I cannot tell you why you were getting those results. However, I do not see where you might be calling 'lookForMe' from.
dhollan8 dhollan8

2014/7/13

#
Thank You, I will try this and see what happens. I went ahead and sent this to the instructor so it was on time, with the same question that I sent you. I'll let you know if removing the bracket helps.
danpost danpost

2014/7/14

#
dhollan8 wrote...
Thank You, I will try this and see what happens. I went ahead and sent this to the instructor so it was on time, with the same question that I sent you. I'll let you know if removing the bracket helps.
It is the removal of line 12 that will help. The change in the 'lookForMe' method was to limit when the image of the actor is set to only when the state of intersection of a Me object changes (only at the time the Me object starts intersecting and at the time the Me object ends intersecting). Having the image set every act cycle is a CPU-hogger.
You need to login to post a reply.