We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello, i have a ask, how to get entity extends class name ?
ArrayList<Entity> entities;
void setup() {
size(500, 500);
entities = new ArrayList <Entity>();
entities.add(new Sign("Hello!", 200, 64));
entities.add(new Sign("test", 200, 128));
}
void draw() {
background(0);
println(entities.get(0).xPos);
((Sign) entities.get(0)).text = "testChangeText";
println( ((Sign) entities.get(0)).text);
for (Entity entity : entities) {
entity.show();
// how to get if a entity SIGN or BLOCK or some.. ? <----
entity.handle();
}
}
public class Layer extends Sign {
Layer(){
super("woah",200,200);
}
}
public class Sign extends Entity {
String text;
public Sign(String text, int mapX, int mapY) {
super(mapX, mapY, 14, 8);
this.text = text;
}
void show() {
noStroke();
fill(80);
rectMode(CORNER);
rect(xPos, yPos, w, h);
stroke(0);
line(xPos + 2, yPos + 2, xPos + w - 3, yPos + 2);
line(xPos + 2, yPos + 5, xPos + w - 3, yPos + 5);
}
void handle() {
textAlign(CENTER, CENTER);
fill(255);
text(text, xPos, yPos-32);
}
}
public abstract class Entity extends GameObject {
public Entity(int xPos, int yPos, int w, int h) {
this.xPos = xPos;
this.yPos = yPos;
this.w = w;
this.h = h;
}
abstract void show();
abstract void handle();
}
public abstract class GameObject {
int xPos, yPos;
int w, h;
abstract void show();
}
Answers
https://Forum.Processing.org/two/discussions/tagged/instanceof
from here: https://forum.processing.org/two/discussion/26510/class-extends
Koogs, its working, but how to split to a String, only name of class?
i don't know. (actually, i do)
i'm only tidying up after you after you started a new thread for the same code as before.
Please provide minimum amount of code demonstrating the part you are struggling with. How much of the code you posted is relevant to your issue? Please review what an MCVE is.
Kf
@GeorgeJava -- did you look at the links @GoToLoop provided about
instanceOf()
?Like:
And:
So: