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

2013/12/3

character walking help

chrissy098 chrissy098

2013/12/3

#
making a christmas game for college and im having trouble with some code.....the walking is fine, its the animation, its only a flipped image but it does help with the game play this is the walking code private void checkMove() { if(Greenfoot.isKeyDown("left")) if(leftIsPressed == false) { turn (-45); leftIsPressed = true; } if((!Greenfoot.isKeyDown("left")) && (leftIsPressed == true)) leftIsPressed = false; if(Greenfoot.isKeyDown("right")) if(rightIsPressed == false) { turn (45); rightIsPressed = true; } if((!Greenfoot.isKeyDown("right")) && (rightIsPressed == true)) rightIsPressed = false; if(Greenfoot.isKeyDown("up")) move(1); if(Greenfoot.isKeyDown("down")) move(-1 ); }//end of method checkMove and heres the animation private void createImages() { image1 = new GreenfootImage("santa1.png"); image2 = new GreenfootImage("santa2.png"); setImage(image1); } private void updateChangeDelay() { changeDelay+=1; } public void switchImage()//switches the images and sets the delay between the change { if(changeDelay>=1) { if (getImage() == image1) { setImage(image2); } else { setImage(image1); }//endif changeDelay=0; }//endif }//end of method switchImagE } im thinking something is wrong cause the animation doesnt work.....any ideas
danpost danpost

2013/12/3

#
Please show the rest of the class (at least the 'act' method and any other methods that are used during an act cycle).
chrissy098 chrissy098

2013/12/3

#
here is it all
private GreenfootImage image1;
   private GreenfootImage image2;
   int changeDelay =0;
   private int prevX;
   private int prevY;
   private int level;
   private int storeCurrentPosition;
   
   private boolean leftIsPressed = false;
   private boolean rightIsPressed = false;
   /**
    * Act - do whatever the Santa wants to do. This method is called whenever
    * the 'Act' or 'Run' button gets pressed in the environment.
    */
   public void act() 
   {
        // Add your action code here.
        checkMove();
        checkCollision();
        storeCurrentPosition();
        switchImage();
        createImages();
        updateChangeDelay();
        
   }    
    
   /**
   * Checks to see if a key has been pressed and moves the player accordingly
   */
   private void checkMove()
   {
       if(Greenfoot.isKeyDown("left"))
         if(leftIsPressed == false)
         {
            turn (-45);
            leftIsPressed = true;
        }
        if((!Greenfoot.isKeyDown("left")) && (leftIsPressed == true))
        leftIsPressed = false;
              
        
       if(Greenfoot.isKeyDown("right"))
         if(rightIsPressed == false)
         {
            turn (45);
            rightIsPressed = true;
        }
        if((!Greenfoot.isKeyDown("right")) && (rightIsPressed == true))
        rightIsPressed = false;
        
       if(Greenfoot.isKeyDown("up"))
          move(1);
       
       if(Greenfoot.isKeyDown("down"))
          move(-1 );
   }//end of method checkMove
    
   private void createImages()
   {
        image1 = new GreenfootImage("santa1.png");
        image2 = new GreenfootImage("santa2.png");
        setImage(image1);
   }
    
   private void updateChangeDelay()
   {
       changeDelay+=1;
   }
   
   public void switchImage()//switches the images and sets the delay between the change
   {
        if(changeDelay>=1)
        {
            if (getImage() == image1)
            {
                setImage(image2);
            }
            else
            {
                setImage(image1);
            }//endif
            changeDelay=0;
        }//endif
   }//end of method switchImagE
}
danpost danpost

2013/12/3

#
I believe the problem is that you are creating the images each act cycle (you call 'createImages' within the act method). This is something you only want to do once, when the Santa object is created. Add the following constructor to the class (constructors are executed once, when the object is created .
public Santa()
{
    createImages();
}
Also remove line 22 above (remove the calling of 'createImages' from the act method). If you have further related problems, please do not hesitate to post here.
chrissy098 chrissy098

2013/12/4

#
i did what you said, but the images are constantly there...it looks like he is having a minor fit...
danpost danpost

2013/12/4

#
Change the number '1' in line 72 above to something more like '8' or so.
chrissy098 chrissy098

2013/12/5

#
changed it to 40 and it looks pretty good..... what code would i use to make it work when he walks and stop when he does....
You need to login to post a reply.