我正在编写一个关于处理的 2d 游戏库,我目前正在处理事物的物理方面。我有一个名为 Object 的类 - 用于操作图像。我希望能够将我的物理类“附加”到对象类 - 这样我就可以通过对象访问所有物理函数,即:
//Scroll to left to see more of comments
class Object extends Game{ //It's worth pointing out that all of my classes extend a Game class
Object(String name){ //A way to add an image to my Object and initialise the class fully
PImage image = loadImage(name);
}
void attachPhysics(){ //I want to be able to call this so that I can directly access functions in the Physics class
}
}
class Physics extends Game { //My Physics class also extends the Game class
Physics(){
//Main initialisation here
}
void projectile(int angle, int speed, int drag){
//Projectile code goes here
}
}
所以如果我有这两个类,我就可以像这样调用它们:
//Scroll to left to see more of comments
void setup(){
Object ball = new Object("ball.gif");
}
void draw(){ //In processing draw is similar to main in java
ball.attachPhysics(); //I attach Physics
ball.projectile(40, 5, -1); //I should then be able to access Physics classes via the ball Object which can manipulate the ball Object (call on its functions as well)
}
如果有人可以帮助我,我将不胜感激,如果您愿意,我可以发布完整的代码。值得注意的是,处理只是java加上一些附加的功能,而这段代码目前还没有设置为库,它只是直接从处理中编译出来的。