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

2014/5/6

What is wrong with this code (probably a lot)

iSuckAtGreenfoot iSuckAtGreenfoot

2014/5/6

#
Basically what I'm trying to do is a cat move left and right while switching images to make it appear like it is actually moving. When it is not moving it should be sitting down. Also, once the cat touches a piece of pizza it should appear to eat it by switching images. So here it is: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class cat here. * * @author (your name) * @version (a version number or a date) */ public class cat extends Animal { /** * Act - do whatever the cat wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void setImage() { GreenfootImage cat_sit = new GreenfootImage ("cat-sit.png"); GreenfootImage walk_left = new GreenfootImage ("cat-walk-left.png"); GreenfootImage walk_left2 = new GreenfootImage ("cat-walk-left2.png"); GreenfootImage walk_right = new GreenfootImage ("cat-walk-right.png"); GreenfootImage walk_right2 = new GreenfootImage ("cat-walk-right.png"); GreenfootImage eat_pizza = new GreenfootImage ("cat-eat.png"); GreenfootImage eat_pizza2 = new GreenfootImage ("cat-eat2.png"); } public void catMove() { if (Greenfoot.isKeyDown("left")==true); move(5); if (Greenfoot.isKeyDown("right")==true); move(5); if (Greenfoot.isKeyDown("left")==false); setImage("cat-sit.png"); if (Greenfoot.isKeyDown("right")==false); setImage("cat-sit.png"); } public void switchImage() { if (getImage()== walk_left) { setImage(walk_left2); } else { setImage(walk_left); } if (getImage()==walk_right) { setImage(walk_right2); } else { setImage(walk_right); } } protected boolean intersects(Actor other) { if (intersects()==true); setImage("cat-eat.png"); delay(1); setImage("cat-eat2.png"); } }
davmac davmac

2014/5/6

#
Please use code tags when you post code. I'm personally not going to look at this because it's too hard to copy and paste code or refer to line numbers. Post again with code tags.
lordhershey lordhershey

2014/5/6

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class cat here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class cat extends Animal
{
   
  static GreenfootImage cat_sit = new GreenfootImage ("cat-sit.png");
  static GreenfootImage walk_left = new GreenfootImage ("cat-walk-left.png");
  static GreenfootImage walk_left2 = new GreenfootImage ("cat-walk-left2.png");
  static GreenfootImage walk_right = new GreenfootImage ("cat-walk-right.png");
  static GreenfootImage walk_right2 = new GreenfootImage ("cat-walk-right.png");
  static GreenfootImage eat_pizza = new GreenfootImage ("cat-eat.png");
  static GreenfootImage eat_pizza2 = new GreenfootImage ("cat-eat2.png");
  	 
  int counter = 0; 
  
  boolean EatingAPizza = false;
  boolean walkingLeft = false;
  boolean walkingRight = false;
  
  public void catMove()
  {
  
    walkingLeft = false;
    walkingRight = false;
     
    if(EatingAPizza)
    {
      //You cannot move and eat a pizza cause you will get a cramp
  	  return;
    }
     
    //I am assuming that the angle rotation is default 0
    if (Greenfoot.isKeyDown("left")==true) 
    {
  	  move(-5);
  	  walkingLeft = true;
    }
    
    if (Greenfoot.isKeyDown("right")==true)
    {
  	move(5);
  	//you cannot walk both left and right
  	walkingLeft = false;
        walkingRight = true;
    }
    
    //at this point we know that we are either walking left or right or not
  }
  
  public void switchImage()
  {
    
    if(EatingAPizza)
    {
      if(counter > 0)
  	  {
          setImage(eat_pizza2);
  	    EatingAPizza = false;
  	  }
  	  else
  	  {
  	    setImage(eat_pizza);
  	  }
    }
    
    if(!walkleft && !walklright)
    {
  	  setImage(cat_sit);
    }
    
    if  (walkleft)
    {
  	  if(counter > 0)
  	  {
  	    setImage(walk_left2);
  	  }
  	  else
  	  {
  	    setImage(walk_left);
  	  }
    }
    
    if  (walkright)
    {
  	  if(counter > 0)
  	  {
  	    setImage(walk_right2);
  	  }
  	  else
  	  {
  	    setImage(walk_right);
  	  }
    }
  
    counter = (counter + 1) % 2;
  }
  
  protected boolean intersects(Actor other)
  {
     if (intersects())
     {
	   /*Start that Pizza eating*/
       EatingAPizza = true;
  	   counter = 0;
     }
  }  

}
having a ; after each if statement will cause the if statement to do a nop if true, therefore every line of code in your example executes in the catMove routine. I am not sure what the rest of the code looks like but this is what I coughed up.
danpost danpost

2014/5/6

#
Two things: (1) there is no act method within the class, so no action will be taken; (2) the method called by 'intersects()' on line 106 in nowhere to be found and the method 'intersects(Actor)' called at line 104 will overshadow the 'intersects(Actor' method of the Actor class; you may want to use 'isTouching(Class)' instead to determine if it is eating time.
You need to login to post a reply.