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

2012/12/6

Help with switching images

1
2
programmer274 programmer274

2012/12/6

#
Hello I am relatively new to programming and I am just taking a course this year in high-school. I have been playing around with a project. Here is what I need help with: What I want is a way that I can keep track of whether the left or right key was pressed last. Not the very last key pressed but which one was more recent. This will be so I can tell the program what set of images to rotate through when no keys are being pressed and what to rotate through when the a or s keys are being pressed based on whether the left or right key was more recent. I want to get rid of having to press the d and f keys for a seperate direction kick and punch. (f) notation stands for flipped image in my code btw. Here is my code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Fighter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Fighter extends Animal
{
    private GreenfootImage image1;
    private GreenfootImage image2;
    private GreenfootImage image3;
    private GreenfootImage image4;
    private GreenfootImage image5;
    private GreenfootImage image6;
    private GreenfootImage image7;
    private GreenfootImage image8;
    private GreenfootImage image9;
    private GreenfootImage image10;
    private GreenfootImage image11;
    private GreenfootImage image12;
    private GreenfootImage image13;
    private GreenfootImage image14;
    private GreenfootImage image15;
    private GreenfootImage image16;
    private GreenfootImage image17;
    private GreenfootImage image18;
    /**
     * Act - do whatever the Fighter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Fighter() 
    {
       image1 = new GreenfootImage("Fighter Neutral 1.png");
       image2 = new GreenfootImage("Fighter Neutral 2.png");
       image3 = new GreenfootImage("Fighter Neutral 3.png");
       image4 = new GreenfootImage("Fighter Kick 1.png");
       image5 = new GreenfootImage("Fighter Kick 2.png");
       image6 = new GreenfootImage("Fighter Kick 3.png");
       image7 = new GreenfootImage("Fighter Punch 1.png");
       image8 = new GreenfootImage("Fighter Punch 2.png");
       image9 = new GreenfootImage("Fighter Punch 3.png");
       image10 = new GreenfootImage("Fighter Neutral 1(f).png");
       image11 = new GreenfootImage("Fighter Neutral 2(f).png");
       image12 = new GreenfootImage("Fighter Neutral 3(f).png");
       image13 = new GreenfootImage("Fighter Kick 1(f).png");
       image14 = new GreenfootImage("Fighter Kick 2(f).png");
       image15 = new GreenfootImage("Fighter Kick 3(f).png");
       image16 = new GreenfootImage("Fighter Punch 1(f).png");
       image17 = new GreenfootImage("Fighter Punch 2(f).png");
       image18 = new GreenfootImage("Fighter Punch 3(f).png");
    }   
    public void act()
    {
        move();
        attack();
    }
    public void move()
    {  
    if (Greenfoot.isKeyDown("right"))
    {
        move(3);
        setImage(image1);
        wait(2);
        setImage(image2);
        move(3);
        wait(5);
        setImage(image3);
        wait(2);
        move(3);
    }
    if (Greenfoot.isKeyDown("left"))
    {
        move(-3);
        setImage(image10);
        wait(2);
        setImage(image11);
        move(-3);
        wait(5);
        setImage(image12);
        wait(2);
        move(-3);
    }
}
public void attack()
{
 if (Greenfoot.isKeyDown("d"))
    {
        setImage(image4);
        wait(5);
        setImage(image5);
        wait(5);
        setImage(image6);
        wait(10);
        
    }
 if (Greenfoot.isKeyDown("f"))
     {
      setImage(image7);
      wait(5);
      setImage(image8);
      wait(5);
      setImage(image9);
      wait(10);
     
     }
     if (Greenfoot.isKeyDown("a"))
    {
        setImage(image16);
        wait(5);
        setImage(image17);
        wait(5);
        setImage(image18);
        wait(10);
      
    }
 if (Greenfoot.isKeyDown("s"))
     {
      setImage(image13);
      wait(5);
      setImage(image14);
      wait(5);
      setImage(image15);
      wait(10);   
     
     }
    }
public void wait(int time)
{
  Greenfoot.delay(time);
}
}
Kyle273 Kyle273

2012/12/6

#
You can make a boolean value like isFacingLeft to keep track of which way your character is facing. If you press the left key, set isFacingLeft to true. If you press the right key, set isFacingLeft to false. Edit: I do the same kind of thing in my project to keep my player facing the same way. Check it out if you want! http://www.greenfoot.org/scenarios/6857
programmer274 programmer274

2012/12/6

#
ok let me try that thanks
programmer274 programmer274

2012/12/7

#
hey its not working what am I doing wrong?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Fighter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Fighter extends Animal
{
    private GreenfootImage image1;
    private GreenfootImage image2;
    private GreenfootImage image3;
    private GreenfootImage image4;
    private GreenfootImage image5;
    private GreenfootImage image6;
    private GreenfootImage image7;
    private GreenfootImage image8;
    private GreenfootImage image9;
    private GreenfootImage image10;
    private GreenfootImage image11;
    private GreenfootImage image12;
    private GreenfootImage image13;
    private GreenfootImage image14;
    private GreenfootImage image15;
    private GreenfootImage image16;
    private GreenfootImage image17;
    private GreenfootImage image18;
    private boolean isFacingLeft;
    /**
     * Act - do whatever the Fighter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Fighter() 
    {
       image1 = new GreenfootImage("Fighter Neutral 1.png");
       image2 = new GreenfootImage("Fighter Neutral 2.png");
       image3 = new GreenfootImage("Fighter Neutral 3.png");
       image4 = new GreenfootImage("Fighter Kick 1.png");
       image5 = new GreenfootImage("Fighter Kick 2.png");
       image6 = new GreenfootImage("Fighter Kick 3.png");
       image7 = new GreenfootImage("Fighter Punch 1.png");
       image8 = new GreenfootImage("Fighter Punch 2.png");
       image9 = new GreenfootImage("Fighter Punch 3.png");
       image10 = new GreenfootImage("Fighter Neutral 1(f).png");
       image11 = new GreenfootImage("Fighter Neutral 2(f).png");
       image12 = new GreenfootImage("Fighter Neutral 3(f).png");
       image13 = new GreenfootImage("Fighter Kick 1(f).png");
       image14 = new GreenfootImage("Fighter Kick 2(f).png");
       image15 = new GreenfootImage("Fighter Kick 3(f).png");
       image16 = new GreenfootImage("Fighter Punch 1(f).png");
       image17 = new GreenfootImage("Fighter Punch 2(f).png");
       image18 = new GreenfootImage("Fighter Punch 3(f).png");
    }   
    public void act()
    {
        move();
        attack();
    }
    public void move()
    {  
        if (!Greenfoot.isKeyDown("right") && (!Greenfoot.isKeyDown("left")))
        {
            isFacingLeft = false; 
              
            if(isFacingLeft = false)
            {
              setImage(image1);
              wait(10);
              setImage(image2);
              wait(15);
              setImage(image3);
              wait(10);  
            }
            if(isFacingLeft = true)
            {
              setImage(image10);
              wait(10);
              setImage(image11);
              wait(15);
              setImage(image12);
              wait(10);  
            }
        }
danpost danpost

2012/12/7

#
There is no refreshing of the scenario while a specific 'act' sequence is in progress. That is, as long as the 'act' for the Fighter object is processing (through all the waits and image changes), no visible action will occur in your scenario (not even for the Fighter object who is being 'act'ed on). Your 'wait'ing needs to be turned into counted 'act' cycles. You will need some instance object variables; a counter to count the cycles, something to track which image you are on and/or going to use next, and (which you seem to already have) something to determine the direction of movement.
programmer274 programmer274

2012/12/7

#
Could you please give some examples or better explain what you are talking about? I kind of have an idea but I'm not sure about the concept of why making the wait periods act cycles or evn how to start. I have many questions like some wait periods are 15 others 10 and still other 3. Do these each need different variables? Also, what does keeping track of how many cycles there have been help? Lastly, what would track what image I'm on and how does this help? I know just basic if getIMage stuff...Is that what you mean? Thanks in advance.
danpost danpost

2012/12/7

#
Refer to my latest post in this discussion. You can change the 0, 6, 12 sequence to whatever. That is, the different periods do not need different variables, just a different statement to catch each transition point. Any further question (either about the code there or other), just post back here.
programmer274 programmer274

2012/12/8

#
if (!Greenfoot.isKeyDown("right") && (!Greenfoot.isKeyDown("left"))) { waitCycle = 0; isFacingLeft = false; if(isFacingLeft = false) { waitCycle = waitCycle + 1; if(waitCycle == 1) { setImageI(image1); waitCycle = waitCycle + 1; } is this what you mean? Can I just do this for images 1, 2, and 3? When I get to three i can just set it to some value right? It would be helpful if you could give me an example using my code if I'm wrong.
vonmeth vonmeth

2012/12/8

#
Using the code danpost provided.
    private int waitCycle;

    public void move()  
    {    
        if (!Greenfoot.isKeyDown("right") && (!Greenfoot.isKeyDown("left")))  
        {  
            isFacingLeft = false;   

            waitCycle++
            if (waitCycle==18) waitCycle=0;

            if(isFacingLeft == false)  
            {  
              if (waitCycle==0)    setImage(image1);
              if (waitCycle==6)    setImage(image2);
              if (waitCycle==12)  setImage(image3);
            }  
            if(isFacingLeft == true)
            {  
              if (waitCycle==0)    setImage(image10);
              if (waitCycle==6)    setImage(image11);
              if (waitCycle==12)  setImage(image12);
            }  
        } 
You don't want to set waitCycle to 0 like you did, or else every time it runs through, it will be 0. Thus the image will always be set to image1. Change those 6, 12, 18 values based on how often you want it to switch between images. If you look at the code danpost provided, you will see that can both cut down on the number of images you have and the number of lines of code but simply mirroring the image.
    private int waitCycle;

    public void move()  
    {    
        if (!Greenfoot.isKeyDown("right") && (!Greenfoot.isKeyDown("left")))  
        {  
            isFacingLeft = false;   

            waitCycle++
            if (waitCycle==18) waitCycle=0;
 
            if (waitCycle==0)    setImage(image1);
            if (waitCycle==6)    setImage(image2);
            if (waitCycle==12)  setImage(image3);
            
            if(isFacingLeft == true) getImage().mirrorHorizontally(); 
        } 
programmer274 programmer274

2012/12/8

#
Ok thanks this should help me get farther.
vonmeth vonmeth

2012/12/8

#
Oh might want to change "private int waitCycle;" to "private int waitCycle = 0;" so it is initialized to some value.
programmer274 programmer274

2012/12/8

#
if (!Greenfoot.isKeyDown("right") && (!Greenfoot.isKeyDown("left"))) { isFacingLeft = false; waitCycle++; if (waitCycle==18) waitCycle=0; if(isFacingLeft == false) { if (waitCycle==0) setImage(image1); if (waitCycle==6) setImage(image2); if (waitCycle==12) setImage(image3); } if(isFacingLeft == true) { if (waitCycle==0) setImage(image10); if (waitCycle==6) setImage(image11); if (waitCycle==12) setImage(image12); } } if (Greenfoot.isKeyDown("right")) { move(3); setImage(image1); wait(2); setImage(image2); move(3); wait(5); setImage(image3); wait(2); move(3); isFacingLeft = false; } if (Greenfoot.isKeyDown("left")) { move(-3); setImage(image10); wait(2); setImage(image11); move(-3); wait(5); setImage(image12); wait(2); move(-3); isFacingLeft = true; } Can you explain why my character keeps facing right even when i have pressed the left key he doesn't stay bouncing in that direction
programmer274 programmer274

2012/12/8

#
wait nvm i got it
vonmeth vonmeth

2012/12/8

#
Remove "isFacingLeft = false;" at the very top. Thought you had that for testing purposes. ;)
danpost danpost

2012/12/8

#
I hate to say it, but everything is wrong with what you have there. First, you are saying to execute this code if neither the 'left' or 'right' key is pressed; this may or may not be what you wanted to do. However, moving on -- you set 'waitCycle' to 0 and 'isFacingLeft' to false; then ??? ask if 'waitCycle' is false (when you just then set it to false). Next, now that you are inside that 'if' block, you add one to 'waitCycle' and ??? ask if it is equal to one (remember, you just set it to zero and added one to it). So, now you enter the inner 'if' block, set the image to 'image1' and increment 'waitCycle' yet again. In total, that code is equivalent to:
if (!Greenfoot.isKeyDown("left") && !Greenfoot.isKeyDown("right"))
{
    setImageI(image1);
    waitCycle=2;
If you are logged on and run my 'Private Messaging' scenario, you can forward me your email address. At which point I will contact you so you can attach your scenario, as is, so I can help you more efficiently.
There are more replies on the next page.
1
2