0

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);
}
}
bergik37
  • 1
  • 2
  • what have you tried so far? This is a pretty basic concept that shouldn't be at all problematic even for the beginner to comprehend. – jwenting May 04 '20 at 13:39
  • I was thinking about creating an array of objects, reading about creating a clone. But I couldn't implement anything else – bergik37 May 04 '20 at 13:53

2 Answers2

0

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.

Tenfour04
  • 83,111
  • 11
  • 94
  • 154
-3

You should use the clone() mehtod.

Ex: object.clone()

https://en.wikipedia.org/wiki/Clone_(Java_method)

https://www.geeksforgeeks.org/clone-method-in-java-2/

  • No, Object.clone() should be avoided. https://stackoverflow.com/questions/3521458/how-to-clone-an-object-you-dont-know-the-type-of – Omaha May 04 '20 at 14:40