[英]Call a function from another class
I'm writing a little program for a school project.我正在为学校项目编写一个小程序。 The theme is "objective Coding" so there's the "main Tab" where the draw method and setup method is.
主题是“客观编码”,因此绘制方法和设置方法所在的“主选项卡”。 In other tabs, I wrote other classes and functions.
在其他选项卡中,我编写了其他类和函数。
So in the my "main Tab" there's the function void draw
and it looks like this:所以在我的“主选项卡”中有函数
void draw
,它看起来像这样:
void draw() {
background();
h1.displayH();
steuerungH();
t1.displayT();
}
The three methods background();
三个方法
background();
, h1.displayH();
,
h1.displayH();
and t1.displayT();
和
t1.displayT();
worked without a problem.工作没有问题。 I tried writing the code like:
我尝试编写如下代码:
...
...
h1.streuerungH();
...
h1
is an object, a rectangle I try to move with the function void steuerungH();
h1
是一个对象,我尝试使用函数void steuerungH();
移动的矩形void steuerungH();
, but there's always an error saying: ,但总是有一个错误说:
The function
steuerungH()
does not exist.函数
steuerungH()
不存在。
Im not sure what the problem exactly is, because the class in which I wrote the function void steuerungH();
我不确定问题到底是什么,因为我在其中编写函数
void steuerungH();
looks exacltly like the others长得一模一样
float gravity = 0.1;
float speed = 0;
class Steuerung {
void steuerungH(){
if (key == UP|| key == 'w') {
playerYPosition = playerYPosition +speed;
speed = speed + gravity;
if (playerYPosition >= 30); {
speed = speed *-1;
}
} else if (key == DOWN ||key == 's') {
/*Duck methode muss noch geschrieben werden*/
} else if (key == RIGHT || key == 'd') {
playerXPosition = playerXPosition +10;
} else if (key == LEFT ||key == 'a') {
playerXPosition = playerXPosition -10;
}
}
}
First of all you should think about playerXPosition
and playerYPosition
.首先,您应该考虑
playerXPosition
和playerYPosition
。 Wouldn't it be better to create a class Player
with the attributes playerXPosition
and playerYPosition
and a method streuerungH
?创建一个具有
playerXPosition
和playerYPosition
属性以及方法streuerungH
Player
类不是更好吗?
class Player {
float gravity = 0.1;
float speed = 0;
float playerXPosition;
float playerYPosition;
// [...]
void steuerungH(){
if (key == UP|| key == 'w') {
playerYPosition = playerYPosition +speed;
speed = speed + gravity;
if (playerYPosition >= 30); {
speed = speed *-1;
}
} else if (key == DOWN ||key == 's') {
/*Duck methode muss noch geschrieben werden*/
} else if (key == RIGHT || key == 'd') {
playerXPosition = playerXPosition +10;
} else if (key == LEFT ||key == 'a') {
playerXPosition = playerXPosition -10;
}
}
}
Anyway, if you want to keep your current designe, then you have to options to solve the issue:无论如何,如果您想保留当前的设计,那么您必须选择解决问题:
Create an instance of Steuerung
.创建
Steuerung
的实例。 eg例如
Steuerung s = new Steuerung();
void draw() {
// [...]
s.streuerungH();
// [...]
}
The other option is:另一种选择是:
Since Processing provides functions in global namespace, you can turn steuerungH
to a function:由于Processing在全局命名空间中提供函数,因此您可以将
steuerungH
转换为函数:
float gravity = 0.1;
float speed = 0;
void steuerungH(){
// [...]
}
void draw() {
// [...]
streuerungH();
// [...]
}
You have to create a new Steuerung object like this你必须像这样创建一个新的 Steuerung 对象
Steuerung steuerung = new Steuerung();
Then you can call the method with然后你可以调用该方法
steuerung.steuerungH();
When you simply do当你简单地做
steuerungH();
Java will be looking for a steuerungH()
method in your Main class which doesn't exist and therefor throws an error. Java 将在您的 Main 类中查找不存在的
steuerungH()
方法,因此会引发错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.