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

2016/3/28

Trouble with isTouching

thomas1998 thomas1998

2016/3/28

#
I'm getting the cannot find symbol - method isTouching (java.lang.class <Food>) error. Bellow is the current method and the area highlighted is the isTouching in the if statement any help would be appreciated.
public void act()

{
    moveAround();
    if(isTouching(Food.class)) {
        removeTouching(Food.class);
        
        SnakeWorld world = (SnakeWorld)getWorld();
        world.addFood();
    }
}
Super_Hippo Super_Hippo

2016/3/28

#
Which version of Greenfoot do you use?
danpost danpost

2016/3/28

#
thomas1998 wrote...
I'm getting the cannot find symbol - method isTouching (java.lang.class <Food>) error. Bellow is the current method and the area highlighted is the isTouching in the if statement any help would be appreciated. < Code Omitted >
Please copy/paste the entire class code here. It could be something elsewhere is wrong.
thomas1998 thomas1998

2016/3/28

#
This is the rest of the code in the snake head actor
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class SnakeHead here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SnakeHead extends Actor
{

private final int EAST = 0;
private final int SOUTH = 90;
private final int WEST = 180;
private final int NORTH = 270;

private final int SPEED = 10;
private int counter = 0;

public SnakeHead()
{
    GreenfootImage img = new GreenfootImage (20,20);
    img.fill();
    setImage(img);
    
    setRotation (Greenfoot.getRandomNumber(4)*90);
}
public void act()
{
moveAround();
    if(isTouching(Food.class)) {    
        removeTouching(Food.class);
         
        SnakeWorld world = (SnakeWorld)getWorld();
        world.addFood();
    }
}

private void moveAround()
{
counter ++;
if(counter==SPEED) {
move(1);
counter = 0;
}
if (Greenfoot.isKeyDown("right")) {
    setRotation(EAST);
}
if (Greenfoot.isKeyDown("down")) {
    setRotation(SOUTH);
}
if (Greenfoot.isKeyDown("left")) {
    setRotation(WAST);
}
if (Greenfoot.isKeyDown("up")) {
    setRotation(NORTH);
}
}
}
This is the code under SnakeWorld
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class SnakeWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SnakeWorld extends World
{

    /**
     * Constructor for objects of class SnakeWorld.
     * 
     */
    public SnakeWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(30, 20, 20); 
        
        GreenfootImage img = new GreenfootImage(20,20);
        img.drawRect(0,0,19,19);
        setBackground(img);
        int x = Greenfoot.getRandomNumber(getWidth());
        int y = Greenfoot.getRandomNumber(getHeight());
        addObject (new SnakeHead(), x, y);
        
        addFood();
    }
    
    public void addFood()
    
   {
      int x = Greenfoot.getRandomNumber(getWidth());
      int y = Greenfoot.getRandomNumber(getHeight());
      addObject (new Food(), x, y); 
}
}
This is the code under FoodActor
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Food here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Food extends Actor
{
    /**
     * Act - do whatever the Food wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Food () 
    {
       GreenfootImage img = new GreenfootImage(20,20);
       img.setColor(Color.GREEN);
       img.fill();
       setImage(img);
    }  
    public void act()
    {
        
        
    }
}
danpost danpost

2016/3/28

#
Everything seems to be in order here. Maybe it is your version of greenfoot; however, I am not aware of any issues that this might be considered being a part of (though, there has been some recent issues with collision in conjunction with setting actor images).
thomas1998 thomas1998

2016/3/28

#
OK, thanks I'll re-download greenfoot and see if it works from there.
thomas1998 thomas1998

2016/3/28

#
After downloading the latest version of Greenfoot and opening my Snake scenario I cannot compile as the Next error button is faded out and the main screen says the the world class may have an error. Does the mean the code is unusable with this version?
danpost danpost

2016/3/28

#
thomas1998 wrote...
After downloading the latest version of Greenfoot and opening my Snake scenario I cannot compile as the Next error button is faded out and the main screen says the the world class may have an error. Does the mean the code is unusable with this version?
This is not something I can test out myself. I am still using Greenfoot v2.3.0 (USB) which I am completely pleased with.
Super_Hippo Super_Hippo

2016/3/28

#
SnakeHead class line 53. It should be WEST and not WAST. Code works for me then.
You need to login to post a reply.