[英]Calling methods from other classes java
I have just started programming some things with Greenfoot, learning java along the way. 我刚刚开始用Greenfoot编程一些东西,并且一直学习Java。 I have become familiar on how to call certain methods between classes, and also differences between static and non-static.
我对如何在类之间调用某些方法以及静态和非静态之间的区别已经很熟悉。
I'm makeing a game where you play the crab and move around to collect worms. 我正在做一个游戏,您在其中玩螃蟹并四处走动以收集蠕虫。 There is a lobster that randomly roams around and if comes in contact with the crab, the crab dissapears.
有一只龙虾随机游荡,如果与螃蟹接触,螃蟹就会消失。 Every time you eat a worm, the score goes up by 10.
每次吃蠕虫,分数都会提高10。
It contains 5 classes named Crab, Lobster, Worm, Counter, and CrabWorld. 它包含5个名为Crab,Lobster,Worm,Counter和CrabWorld的类。 For your sake, I'll just post the well documented code for you to read.
为了您的方便,我将发布记录良好的代码供您阅读。 But the important part that I am having trouble with is calling a method from the lobster to the Crab instance created by the CrabWorld.
但是,我遇到的重要问题是调用从龙虾到CrabWorld创建的Crab实例的方法。 This method would change the lives of the crab.
这种方法会改变螃蟹的生活。
I have tried calling ((CrabWorld) getWorld), but I don't need to access the CrabWorld. 我已经尝试调用((CrabWorld)getWorld),但是不需要访问CrabWorld。 I need to access the Crab instance (created in the CrabWorld, if that matters) from from the Lobster Class.
我需要从Lobster类访问Crab实例(在CrabWorld中创建,如果有关系的话)。
Crabworld: 螃蟹世界:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Creates the crab Enviornment with a counter, crab and lobster. Also has methods to
* place random worms in the world over time. Also has a method to add the score
* To the Counter score
* @author Troy Bick
* @version 12/20/13
*/
public class CrabWorld extends World
{
private Actor playerCrab = new Crab();
private Counter score = new Counter("Score: ");
/**
* Constructor for objects of class CrabWorld.
*
*/
public CrabWorld()
{
super(560, 560, 1);
prepare();
}
/**
* Prepare the world for the start of the program. That is: create the initial
* objects and add them to the world.
*/
private void prepare()
{
addObject(score, 100, 540);
addObject(playerCrab, 280, 280);
addObject(new Lobster(), 100, 100);
}
/**
* Randomly places worms at random periods
*/
public void act()
{
if(Greenfoot.getRandomNumber(100)<0.5){
addObject(new Worm(), Greenfoot.getRandomNumber(540)+10, Greenfoot.getRandomNumber(540)+10);
}
}
public void eatenWorm()
{
score.add(10);
}
public void eatsCrab()
{
playerCrab.isEaten();
}
Crab: 螃蟹:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Crab extends Actor
{
private int wormsEaten = 0;
private int lives = 3;
/**
* Act - do whatever the Crab wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment. Run calls Act every
* frame.
*/
public void act()
{
moveAndTurn();
eat();
}
/**
* Determines the key pressed and moves/turns the crab.
*/
public void moveAndTurn()
{
move(3);
if(Greenfoot.isKeyDown("a")){
turn(3);
}
if(Greenfoot.isKeyDown("d")){
turn(-3);
}
}
/**
* Detects if the worm is close to the crab, and if so, eats it.
*/
public void eat()
{
Actor worm;
worm = getOneObjectAtOffset(0,0,Worm.class);
World world = getWorld();
if(worm != null)
{
world.removeObject(worm);
Greenfoot.playSound("eating.wav");
wormsEaten++;
((CrabWorld) getWorld()).eatenWorm();
}
}
/**
* Returns the number of worms eaten.
*/
public int getWormsEaten()
{
return wormsEaten;
}
/**
* Returns the number the lives the crab has left.
*/
public int getLivesCount()
{
return lives;
}
/**
* Subtracts a life from lives.
*/
public void eaten()
{
lives--;
}
}
Lobster: 龙虾:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Lobster here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Lobster extends Actor
{
/**
* Act - do whatever the Lobster wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
moveAround();
eat();
}
public void eat()
{
Actor crab;
crab = getOneObjectAtOffset(0,0,Crab.class);
if(crab != null)
{
World world = getWorld();
world.removeObject(crab);
((CrabWorld) getWorld()).eatsCrab();
}
}
public void moveAround()
{
move(3);
//Random Movements
if(Greenfoot.getRandomNumber(100) < 10)
{
turn(Greenfoot.getRandomNumber(40) - 20);
}
//World Edge Detection
if(getX() <= 10 || getX() >= getWorld().getWidth()-10)
{
turn(10);
}
if(getY() <= 10 || getY() >= getWorld().getHeight()-10)
{
turn(10);
}
}
}
So when the lobster that has be added to the world eats the crab, I want that crab to lose a life, but when I try to compile, I get an error on the CrabWorld class that it could not find the method mentioned. 因此,当添加到世界上的龙虾吃掉螃蟹时,我希望那只螃蟹丧命,但是当我尝试编译时,在CrabWorld类上遇到了一个错误,即找不到所提到的方法。 Why is that?
这是为什么?
Just very confused... If anyone could help me that would be great. 只是非常困惑...如果有人可以帮助我,那将是很棒的。 If I'm missing anything, I fix it.
如果我丢失任何东西,我会解决。
Thanks in advanced, Troy 谢谢,特洛伊
So it looks like you have a getWorld method in your Lobster class which I'm assuming gets the whole world. 因此,看来您在龙虾类中有一个getWorld方法,我假设它能获得整个世界。 If you add a getCrab method in your CrabWorld then your lobster could access the crab.
如果在CrabWorld中添加getCrab方法,则龙虾可以访问螃蟹。
Inside CrabWorld CrabWorld内部
public Crab getCrab(){
return playerCrab;
}
Then in Lobster 然后在龙虾
((CrabWorld) world).getCrab();
The problem you have is that in CrabWorld
you declare playerCrab
as 你的问题是,在
CrabWorld
声明playerCrab
为
private Actor playerCrab = new Crab();
So although playerCrab is actually an instance of Crab, to CrabWorld it is an Actor and you can therefore only call the methods that Actor defines. 因此,尽管playerCrab实际上是Crab的实例,但对于CrabWorld来说,它是一个Actor,因此您只能调用Actor定义的方法。
You could change your declaration to 您可以将声明更改为
private Crab playerCrab = new Crab();
That way CrabWorld knows that playerCrab
is an instance of Crab
and you can then call any of the public methods that Crab
defines. 这样CrabWorld知道
playerCrab
是Crab
的实例,然后您可以调用Crab
定义的任何公共方法。 Given that your member variable is called playerCrab
it seems that you will always have it as a Crab
, so there it's certainly appropriate for it to be declared as one. 假设您的成员变量称为
playerCrab
,似乎您将始终将其作为Crab
,因此将其声明为一个当然是适当的。
An alternative approach, if you control the Actor
type would be to add a loseLife()
method to it if you think that the concept of losing a life is common to classes that extend Actor
. 如果您认为失去生命的概念是扩展
Actor
类所loseLife()
,那么,如果您控制Actor
类型,则可以采用另一种方法,即为其添加一个loseLife()
方法。 Your Lobster
class doesn't have that concept at present, but if you were to decide to add that to it then a method on Actor
would be an appropriate way to do it. 您的
Lobster
类目前没有这个概念,但是如果您决定将其添加到该类中,那么Actor
上的方法将是实现此概念的合适方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.