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

2012/5/9

Changing Images for Visual Appeal

JayWood860 JayWood860

2012/5/9

#
i am currently trying to make a game where when either the left or right arrow key is pressed, the image of the actor changes to make it appear to be walking. however, not only do the images not change but the actor is going the wrong way! Here's the code i've posted:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
 * Write a description of class Stewie here.
 * 
 * JUWAN
 * 
 */
public class Stewie extends Actor
{
    public Stewie()
    {
        setImage(img);
    }
    GreenfootImage img = new GreenfootImage("stewieMain.png");
    GreenfootImage wr1 = new GreenfootImage("stewie1.png");
    GreenfootImage wr2 = new GreenfootImage("stewie2.png");
    GreenfootImage wl1 = getLeft1();
    GreenfootImage wl2 = getLeft2();
    /**
     * Act - do whatever the Stewie wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if ( Greenfoot.isKeyDown("left") )
        {
            setLocation(getX() - 2, getY());
            walkLeft();
        }
        else if ( Greenfoot.isKeyDown("right") )
        {
            setLocation(getX() + 2, getY());
            walkRight();
        }
        else setImage(img);
    }    
    public GreenfootImage getLeft1()
    {
        GreenfootImage a;
        flip();
        a = wr1;
        return a;
    }
    public GreenfootImage getLeft2()
    {
        GreenfootImage a;
        flip2();
        a = wr1;
        return a;
    }
    public void flip()
    {
        wr1.mirrorVertically();
    }
    public void flip2()
    {
        wr2.mirrorVertically();
    }
    public void walkLeft()
    {
        int a = 1;
        int b = a;
        if ( b != 0 )
        {
            b--;
            setImage(wl1);
        }
        else if ( b == 0 )
        {
            setImage(wl2);
            b = a;
        }
    }
    public void walkRight()
    {
        int a = 1;
        int b = a;
        if ( b != 0 )
        {
            b--;
            setImage(wr1);
        }
        else if ( b == 0 )
        {
            setImage(wr2);
            b = a;
        }
    }
}
any reasons why this isn't working?
JayWood860 JayWood860

2012/5/9

#
i fixed most of this up by changing the world size but now the image only changes once, not continuosly, and it doesn't flip when i go right.
danpost danpost

2012/5/10

#
For one thing lines 61 & 62, as well as lines 76 & 77, have both 'a' and 'b' set to 1 before checking 'b's value. It will always be 1 at the time it checks its value, so the 'if' blocks are meaningless as the first parts will ALWAYS execute.
JayWood860 JayWood860

2012/5/10

#
what should i define b as then before the "if" statements?
danpost danpost

2012/5/10

#
What are you trying to use 'b' for? What was it, it was supposed to do? For that matter, what is 'a' for, as well?
danpost danpost

2012/5/10

#
I have a feeling they are to control the images for walking, but neither is declared outside the method, and the names need to be something more appropriate for what they do. Try the following:
int walkImage = 0;
public void walkLeft()
{
    walkImage = (walkImage + 1) % 2;
    if (walkImage == 0) setImage(wl1); else setImage(wl2);
}
public void walkRight()
{
    walkImage = (walkImage + 1) % 2;
    if (walkImage == 0) setImage(wr1); else setImage(wr2);
}
danpost danpost

2012/5/10

#
BTW, you may still have other problems, but the walking part should, using this, be fixed. You may want to change lines 17 & 18 and append to the constructor to fix the images. Replace lines 8 through 18 with the following:
GreenfootImage img = new GreenfootImage("stewieMain.png");
GreenfootImage wr1 = new GreenfootImage("stewie1.png");
GreenfootImage wr2 = new GreenfootImage("stewie2.png");
GreenfootImage wl1 = new GreenfootImage("stewie1.png");
GreenfootImage wl2 = new GreenfootImage("stewie2.png");

public Stewie()
{
    setImage(img);
    wl1.mirrorHorizontally();
    wl2.mirrorHorizontally();
}
and remove lines 37 through 58.
JayWood860 JayWood860

2012/5/10

#
i've changed the walk method and rearranged the images as you suggested and the images flip perfectly. however, as the left key is down (or the right key), i wish to have the images go back and forth between wl1 and wl2 (or wr1 and wr2) to make the object appear to be walking. this would involve changing a few things in the walkRight()/walkLeft() methods, correct?
JayWood860 JayWood860

2012/5/10

#
i've fixed this issue as well. Thank you for all your help!
JayWood860 JayWood860

2012/5/14

#
I have a slightly different issue but the same principle being applied. when the space bar is pressed, i want the actor to go through these series of images as you can see, however he does nothing as the space bar is pressed. i've rewritten this code multiple times and have had slightly different out comes, but none of them being successful. this is what i currently have now, which i am well aware is not going to work. any suggestions?
 public void act() 
    {
        if ( Greenfoot.isKeyDown("left") ){ setLocation(getX() - range, getY()); walkLeft(); }
        else if ( Greenfoot.isKeyDown("right") ) { setLocation(getX() + range, getY()); walkRight(); }
        else setImage(img);
        while ( Greenfoot.isKeyDown("space")){ shoot(); }
        relocate();
    }    
    public void shoot()
    {
        while (Greenfoot.isKeyDown("left")) 
        {
            int time = 0;
            if ( time == 0){setImage(sl1); time++;}
            if ( time == 1){setImage(sl2); time++;}
            if ( time == 2){setImage(sl3); time++;}
            if ( time == 3){setImage(sl);}
        }
        while (Greenfoot.isKeyDown("right")) 
        {
            int time = 0;
            if ( time == 0){setImage(sr1); time++;}
            if ( time == 1){setImage(sr2); time++;}
            if ( time == 2){setImage(sr3); time++;}
            if ( time == 3){setImage(sr);}
        }
    }
danpost danpost

2012/5/14

#
The problem with the shooting, is that all the images are exhausted in one act cycle (which means your image does not appear to change, and the rest of the scenario freezes, until the 'space' is released). What you need is to do, is to spread the action out amongst several act cycles, by doing this: First, add a boolean instance variable to the actor class called 'shooting' and initialize it to 'false'; and add an int instance variable to the actor class called 'shootingFrame' and initialize it to '-1'. Then, encapsulate your 'if's for the keystrokes in an 'if (!shooting)' block. Next, change the 'while' for the 'space' key to an 'if' (inside the same block); but only have it set 'shooting' to 'true'. Finally, after the main 'if (!shooting)' block, add the following block:
if (shooting)
{
    shootingFrame++;
    if (shootingFrame == 0) setImage(sr1);
    if (shootingFrame == 1) setImage(sr2);
    if (shootingFrame == 2) setImage(sr3);
    if (shootingFrame == 3)
    {
        setImage(sr);
        shootingFrame = -1;
        shooting = false;
    }
}
JayWood860 JayWood860

2012/5/16

#
this is perfect! thanks
You need to login to post a reply.