英文:
How do I create copies of objects?
问题
如何在规律间隔内创建激光中点(Lasermid)对象的副本?激光中点(Lasermid)是激光束的轨迹。我希望激光束的轨迹不仅是单个激光中点(Lasermid)对象,而是一个集合。类似经典游戏"贪吃蛇"的效果。
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.scenes.scene2d.Stage;
public class Laser extends BaseActor {
Lasermid lasermid;
Color color = new Color(Color.GREEN);
public Laser(float x, float y, Stage s) {
super(x, y, s);
loadTexture("assets/Line11.png");
setSize(30, 10);
setMaxSpeed(800);
setBoundaryPolygon(8);
setSpeed(10);
lasermid = new Lasermid(-30, 0, s);
addActor(lasermid);
lasermid.setColor(color);
// lasermid.setPosition(getX(),getY());
}
public void act(float dt) {
super.act(dt);
applyPhysics(dt);
}
}
class Lasermid extends BaseActor {
public Vector2 position = new Vector2();
public Lasermid(float x, float y, Stage s) {
super(x, y, s);
loadTexture("assets/Line111.png");
setSize(30, 10);
setBoundaryPolygon(8);
}
public void act(float dt) {
super.act(dt);
applyPhysics(dt);
}
}
英文:
How do I create copies of a lasermid object at regular intervals?Lasermid is a trace from the Laser beam.I want the trace from the laser beam to remain not a single lasermid object but a set.Like the classic game snake?
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.scenes.scene2d.Stage;
public class Laser extends BaseActor {
Lasermid lasermid;
Color color=new Color(Color.GREEN);
public Laser(float x, float y, Stage s) {
super(x, y, s);
loadTexture("assets/Line11.png");
setSize(30,10);
setMaxSpeed(800);
setBoundaryPolygon(8);
setSpeed(10);
lasermid = new Lasermid(-30, 0, s);
addActor(lasermid);
lasermid.setColor(color);
// lasermid.setPosition(getX(),getY());
}
public void act(float dt) {
super.act(dt);
applyPhysics(dt);
}
}
Class Lasermid
public class Lasermid extends BaseActor{
public Vector2 position = new Vector2();
public Lasermid(float x, float y, Stage s) {
super(x, y, s);
loadTexture("assets/Line111.png");
setSize(30, 10);
setBoundaryPolygon(8);
}
public void act(float dt) {
super.act(dt);
applyPhysics(dt);
}
}
答案1
得分: 0
你的类不应在构造函数中使用 Stage。如果你实际上用它做任何事情,那将会创建双向耦合和脆弱的代码。
而且你绝对不应该在 Actor 内部加载 Texture,特别是那些被其他 Actor 使用的 Texture。Texture 是占用大量内存和 CPU 资源的资源,当你从一个 Texture 切换到另一个时,会消耗大量内存、加载时间和 CPU 周期。当每个 Actor 加载自己的 Texture 副本时,你会浪费内存、加载时间和绘制过程中的 CPU 周期。你还因为没有释放它们而导致大量内存泄漏。你应该为整个游戏加载一个 Texture 的单一副本,并将对 Texture 的引用传递给 Actor 的构造函数。
复制对象没有捷径。你必须创建一个静态方法或一个构造函数,手动将每个属性的值复制到新实例中。如果适用,你还必须复制内部引用的类。
因此,Laser 的复制方法可能如下所示,但你需要复制它使用的每个相关参数:
public Laser copy() {
final Laser laser = new Laser(getX(), getY());
laser.setRotation(getRotation());
laser.setSpeed(getSpeed());
laser.setColor(getColor());
laser.laserMid.setRotation(laserMid.getRotation);
// 等等。
return laser;
}
如果你使用构造函数来完成这个任务,你可以利用继承关系来简化复制子类的工作。例如:
// BaseActor 构造函数:
public BaseActor(BaseActor other) {
super();
setX(other.getX());
setY(other.getY());
setColor(other.getColor());
// 等等。
}
// Laser 构造函数:
public Laser(float x, float y) {
super();
init();
}
public Laser(Laser other) {
super(other);
init();
laser.laserMid.setRotation(laserMid.getRotation);
// 等等。
}
private void init() {
setSize(30,10);
setMaxSpeed(800);
setBoundaryPolygon(8);
setSpeed(10);
// 等等。
}
顺便说一下,你的 color
字段遮盖了 Actor 已经拥有的 color 字段。
英文:
Your classes should not use a Stage in the constructor. If you were actually using it for anything, it would create a bidirectional coupling and fragile code.
And you absolutely should not be loading a Texture from inside an Actor, especially one that is used by other Actors. Textures are assets that take up significant memory and CPU when you switch from one to another. When each actor loads its own copy of the Texture, you are wasting memory, load time, and CPU cycles during the drawing process. You are also leaking a lot of memory by not disposing of them. You should load a single copy of a Texture for the whole game and pass the reference to the Texture to the constructor of your Actor.
There is no shortcut to copying an object. You must create either a static method or a constructor that manually copies the values of each property into the new instance. And you must copy the inner referenced classes as well, if that's relevant.
So the copy method for Laser would look something like this, but you'd need to copy every relevant parameter that it uses:
public Laser copy() {
final Laser laser = new Laser(getX(), getY());
laser.setRotation(getRotation());
laser.setSpeed(getSpeed());
laser.setColor(getColor());
laser.laserMid.setRotation(laserMid.getRotation);
// etc.
return laser;
}
If you do this with constructors, you can take advantage of the hierarchy to simplify the effort of copying subclasses. For example:
// BaseActor constructor:
public BaseActor(BaseActor other) {
super();
setX(other.getX());
setY(other.getY());
setColor(other.getColor());
// etc.
}
// Laser constructors:
public Laser(float x, float y) {
super();
init();
}
public Laser(Laser other) {
super(other);
init()
laser.laserMid.setRotation(laserMid.getRotation);
// etc.
}
private void init() {
setSize(30,10);
setMaxSpeed(800);
setBoundaryPolygon(8);
setSpeed(10);
// etc.
}
By the way, your color
field is shadowing the color field that Actor already has.
答案2
得分: -3
你应该使用 clone() 方法。
例如:object.clone()
https://en.wikipedia.org/wiki/Clone_(Java_method)
https://www.geeksforgeeks.org/clone-method-in-java-2/
英文:
You should use the clone() mehtod.
Ex: object.clone()
专注分享java语言的经验与见解,让所有开发者获益!
评论