在不同的 Greenfoot 世界中显示相同的内容
Displaying the same contents onto a different Greenfoot world
对于 Greenfoot 决赛 class 项目,我决定制作一款名为 "Doggie the Super Catcher" 的游戏。这个游戏基本上做的是,演员 Doggie 将尽最大努力在屏幕上来回 运行(通过用户的左右当然是箭头键盘输入)。然而,问题是我想创建多个级别,并且我有点希望我拥有的第一个世界(名为 GameWorld)出现在级别 1 中,这是一个不同的世界。我该怎么做?换句话说,当满足特定条件(即达到特定点数)时,我似乎无法将相同的东西从 GameWorld 转移到 Level1,其中包括四叶草、糖果和计分板?仅供参考:我为计分板使用了一个单独的演员(希望这有帮助......)。你们有人知道办法吗?谢谢
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Doggie extends Actor
{
// Declare arrays of Greenfoot images for animation
private GreenfootImage[] runningLeft;
private GreenfootImage[] runningRight;
// Declare Greenfoot images for standing still facing left and right
private GreenfootImage standingStill;
private GreenfootImage standingLeft;
private GreenfootImage standingRight;
//
boolean facingLeft;
// Integers that help control the speed of movement and animation
private int animationCounter;
private int animationDelay;
private int animationDelayCounter;
private int animationSpeed;
Clover c;
SpecialTreat1 c1;
SpecialTreat2 c2;
public Doggie()
{
String fileName;
String fileNamePrefix = "left0";
String fileNameSuffix = ".png";
runningLeft = new GreenfootImage[3];
runningRight = new GreenfootImage[3];
for (int imageCounter = 0; imageCounter < runningLeft.length; imageCounter++)
{
fileName = fileNamePrefix + (imageCounter + 1) + fileNameSuffix;
runningLeft[imageCounter] = new GreenfootImage(fileName);
runningRight[imageCounter] = new GreenfootImage (runningLeft[imageCounter]);
runningRight[imageCounter].mirrorHorizontally();
}
standingStill = new GreenfootImage ("still01.png");
standingLeft = new GreenfootImage ("left01.png");
standingRight = new GreenfootImage (standingLeft);
standingRight.mirrorHorizontally();
facingLeft = true;
animationSpeed = 5;
animationDelay = 6;
animationDelayCounter = 0;
animationCounter = 0;
}
/**
* Act - do whatever the Doggie wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
animationDelayCounter++;
if (animationDelayCounter == animationDelay)
{
animationCounter++;
animationDelayCounter = 0;
}
if (animationCounter > runningRight.length - 1)
animationCounter = 0;
c = (Clover)getOneIntersectingObject(Clover.class);
c1 = (SpecialTreat1)getOneIntersectingObject(SpecialTreat1.class);
c2 = (SpecialTreat2)getOneIntersectingObject(SpecialTreat2.class);
if (c != null)
{
c.collect();
GameWorld gW = (GameWorld)getWorld();
gW.showPoints();
}
if (c1 != null)
{
c1.collect();
GameWorld gW = (GameWorld)getWorld();
gW.showPoints();
}
if (c2 != null)
{
c2.collect();
GameWorld gW = (GameWorld)getWorld();
gW.showPoints();
}
}
public void runLeft()
{
if (!(facingLeft))
animationCounter = 0;
facingLeft = true;
setImage (runningLeft[animationCounter]);
setLocation (getX() - animationSpeed, getY());
}
public void runRight()
{
if (facingLeft)
animationCounter = 0;
facingLeft = false;
setImage (runningRight[animationCounter]);
setLocation (getX() + animationSpeed, getY());
}
public void standStill()
{
if (facingLeft)
{
setImage(standingLeft);
}
else
setImage(standingRight);
animationCounter = 0;
animationDelayCounter = 0;
}
}
在 Greenfoot 中,关卡通常是通过不同的世界来实现的。所以你可能有:
class Level1 extends World
{
...
}
和
class Level2 extends World
{
...
}
它们都将直接出现在 class 图中世界的下方。如果你想在关卡之间共享代码(例如,它们都生成三叶草),那么你可能需要一个共享的父级 class。所以你可能有:
class DoggieWorld extends World
{
public void spawnClovers(int speed)
{
if (Greenfoot.getRandomNumber(1000) < speed)
addObject(new Clover(), Greenfoot.getRandomNumber(getWidth()), 0);
}
}
那么你有两个级别 classes,每个级别都扩展了 DoggieWorld(不仅仅是普通世界),允许你从每个级别调用 spawnClovers 方法:
class Level1 extends DoggieWorld
{
public void act()
{
spawnClovers(20);
}
}
class Level2 extends DoggieWorld
{
public void act()
{
spawnClovers(50);
}
}
我不想让这个答案太长,但希望这给出了总体思路:有一个共享的父世界 class,两个级别 classes 都继承自(使用扩展) ,然后两个级别 classes 都可以从共享父级调用方法。
编辑:抱歉,我想我只是更清楚地理解了你在切换关卡时也想在世界之间转移演员。为此,您可以遍历所有您感兴趣的类型的演员,将他们从现有世界中移除并将他们添加到新世界中。例如。在 Level1 中包含这种方法:
public void changeLevel()
{
World nextLevel = new Level2();
for (Clover c : getObjects(Clover.class))
{
// Must ask for position before removing from world:
int x = c.getX();
int y = c.getY();
removeObject(c);
nextLevel.addObject(c, x, y);
}
Greenfoot.setWorld(nextLevel);
}
希望对您有所帮助
对于 Greenfoot 决赛 class 项目,我决定制作一款名为 "Doggie the Super Catcher" 的游戏。这个游戏基本上做的是,演员 Doggie 将尽最大努力在屏幕上来回 运行(通过用户的左右当然是箭头键盘输入)。然而,问题是我想创建多个级别,并且我有点希望我拥有的第一个世界(名为 GameWorld)出现在级别 1 中,这是一个不同的世界。我该怎么做?换句话说,当满足特定条件(即达到特定点数)时,我似乎无法将相同的东西从 GameWorld 转移到 Level1,其中包括四叶草、糖果和计分板?仅供参考:我为计分板使用了一个单独的演员(希望这有帮助......)。你们有人知道办法吗?谢谢
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Doggie extends Actor
{
// Declare arrays of Greenfoot images for animation
private GreenfootImage[] runningLeft;
private GreenfootImage[] runningRight;
// Declare Greenfoot images for standing still facing left and right
private GreenfootImage standingStill;
private GreenfootImage standingLeft;
private GreenfootImage standingRight;
//
boolean facingLeft;
// Integers that help control the speed of movement and animation
private int animationCounter;
private int animationDelay;
private int animationDelayCounter;
private int animationSpeed;
Clover c;
SpecialTreat1 c1;
SpecialTreat2 c2;
public Doggie()
{
String fileName;
String fileNamePrefix = "left0";
String fileNameSuffix = ".png";
runningLeft = new GreenfootImage[3];
runningRight = new GreenfootImage[3];
for (int imageCounter = 0; imageCounter < runningLeft.length; imageCounter++)
{
fileName = fileNamePrefix + (imageCounter + 1) + fileNameSuffix;
runningLeft[imageCounter] = new GreenfootImage(fileName);
runningRight[imageCounter] = new GreenfootImage (runningLeft[imageCounter]);
runningRight[imageCounter].mirrorHorizontally();
}
standingStill = new GreenfootImage ("still01.png");
standingLeft = new GreenfootImage ("left01.png");
standingRight = new GreenfootImage (standingLeft);
standingRight.mirrorHorizontally();
facingLeft = true;
animationSpeed = 5;
animationDelay = 6;
animationDelayCounter = 0;
animationCounter = 0;
}
/**
* Act - do whatever the Doggie wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
animationDelayCounter++;
if (animationDelayCounter == animationDelay)
{
animationCounter++;
animationDelayCounter = 0;
}
if (animationCounter > runningRight.length - 1)
animationCounter = 0;
c = (Clover)getOneIntersectingObject(Clover.class);
c1 = (SpecialTreat1)getOneIntersectingObject(SpecialTreat1.class);
c2 = (SpecialTreat2)getOneIntersectingObject(SpecialTreat2.class);
if (c != null)
{
c.collect();
GameWorld gW = (GameWorld)getWorld();
gW.showPoints();
}
if (c1 != null)
{
c1.collect();
GameWorld gW = (GameWorld)getWorld();
gW.showPoints();
}
if (c2 != null)
{
c2.collect();
GameWorld gW = (GameWorld)getWorld();
gW.showPoints();
}
}
public void runLeft()
{
if (!(facingLeft))
animationCounter = 0;
facingLeft = true;
setImage (runningLeft[animationCounter]);
setLocation (getX() - animationSpeed, getY());
}
public void runRight()
{
if (facingLeft)
animationCounter = 0;
facingLeft = false;
setImage (runningRight[animationCounter]);
setLocation (getX() + animationSpeed, getY());
}
public void standStill()
{
if (facingLeft)
{
setImage(standingLeft);
}
else
setImage(standingRight);
animationCounter = 0;
animationDelayCounter = 0;
}
}
在 Greenfoot 中,关卡通常是通过不同的世界来实现的。所以你可能有:
class Level1 extends World
{
...
}
和
class Level2 extends World
{
...
}
它们都将直接出现在 class 图中世界的下方。如果你想在关卡之间共享代码(例如,它们都生成三叶草),那么你可能需要一个共享的父级 class。所以你可能有:
class DoggieWorld extends World
{
public void spawnClovers(int speed)
{
if (Greenfoot.getRandomNumber(1000) < speed)
addObject(new Clover(), Greenfoot.getRandomNumber(getWidth()), 0);
}
}
那么你有两个级别 classes,每个级别都扩展了 DoggieWorld(不仅仅是普通世界),允许你从每个级别调用 spawnClovers 方法:
class Level1 extends DoggieWorld
{
public void act()
{
spawnClovers(20);
}
}
class Level2 extends DoggieWorld
{
public void act()
{
spawnClovers(50);
}
}
我不想让这个答案太长,但希望这给出了总体思路:有一个共享的父世界 class,两个级别 classes 都继承自(使用扩展) ,然后两个级别 classes 都可以从共享父级调用方法。
编辑:抱歉,我想我只是更清楚地理解了你在切换关卡时也想在世界之间转移演员。为此,您可以遍历所有您感兴趣的类型的演员,将他们从现有世界中移除并将他们添加到新世界中。例如。在 Level1 中包含这种方法:
public void changeLevel()
{
World nextLevel = new Level2();
for (Clover c : getObjects(Clover.class))
{
// Must ask for position before removing from world:
int x = c.getX();
int y = c.getY();
removeObject(c);
nextLevel.addObject(c, x, y);
}
Greenfoot.setWorld(nextLevel);
}
希望对您有所帮助