【发布时间】:2017-02-23 01:59:21
【问题描述】:
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
/**
* This class defines a crab. Crabs live on the beach. They like sand worms
* (very yummy, especially the green ones).
*
* Version: 4
*
* The crab is keyboard controlled and eats worms. In this version, we have added
* a sound when the crab eats a worm.
*/
public class Crab extends Actor
{
private GreenfootImage image1;
private GreenfootImage image2;
private int age;
/**
* Create a crab and initialize its two images.
*/
public Crab()
{
image1 = new GreenfootImage("crab.png");
image2 = new GreenfootImage("crab2.png");
setImage(image1);
}
/**
* Act - do whatever the crab wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
boolean isAlive;
int n;
checkKeypress();
move(5);
lookForWorm();
if ( getImage() == image1)
{setImage(image2);
}
else
{
setImage(image1);
}
}
/**
* Check whether a control key on the keyboard has been pressed.
* If it has, react accordingly.
*/
public void checkKeypress()
{
if (Greenfoot.isKeyDown("up"))
{
setLocation(getX(), getY()-4);
}
if (Greenfoot.isKeyDown("down"))
{
setLocation(getX(), getY()+4);
}
}
/**
* Check whether we have stumbled upon a worm.
* If we have, eat it. If not, do nothing.
*/
public void lookForWorm()
{
if ( isTouching(Worm.class) )
{
removeTouching(Worm.class);
Greenfoot.playSound("slurp.wav");
wormsEaten = wormsEaten + 1;
if (wormsEaten == 8)
{
Greenfoot.playSound("fanfare.wav");
Greenfoot.stop();
}
}
}
【问题讨论】:
-
解析什么?也许“slurp.wav: 和”fanfare.wav“要么找不到要么无效。
-
因为您在
class末尾缺少}?
标签: java