-
I'm very new to JS/TS and I'm sure this will be a very simple fix, I just can't seem to work it out. I am trying to draw a shape out of lines and I need to be able to capture when any of the lines in the shape are clicked. I intend to create multiple instances of this class to draw each line of the shape. When I click But import { Actor, Engine, Color, vec, Line, Vector } from 'excalibur';
class LineOfShape extends Actor {
name: string;
start: Vector;
end: Vector;
constructor(start: Vector, end: Vector, name: string) {
super({
pos: vec(100, 100),
});
this.name = name;
this.start = start;
this.end = end;
}
onInitialize(engine: Engine): void {
this.graphics.anchor = Vector.Zero;
this.graphics.use(
new Line({
start: this.start,
end: this.end,
color: Color.Cyan,
thickness: 10
})
)
this.events.on('pointerdown', (evt) => {
console.log(this.name + ' clicked');
})
}
}
const game = new Engine({
width: 600,
height: 400
});
const plots = [
[[0, 0], [200, 200]],
[[200, 200], [400, 200]],
]
for (let i = 0; i < plots.length; i++) {
const plot = plots[i];
const linee = new LineOfShape(vec(plot[0][0], plot[0][1]), vec(plot[1][0], plot[1][1]), 'line' + i);
game.add(linee);
}
game.start(); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
@Zoobdude This is a good question! I think what's happening is the built in pointer system is using the bounds of the line, not the pixels of the line to determine whether to send the I think there are 2 options in the current state:
|
Beta Was this translation helpful? Give feedback.
@Zoobdude This is a good question!
I think what's happening is the built in pointer system is using the bounds of the line, not the pixels of the line to determine whether to send the
pointerdown
event to the actor. By default in the latest excalibur both the collision geometry and the graphics AABB will but used for clicks.I think there are 2 options in the current state:
Add some collider that would represent the clickable area of the line, and toggle off
actor.pointer.useGraphicsBounds = false
. This might be the easiest?Use the current setup but do some additional processing. Because you will a bounding box around the graphic, youhave false positives but no false negatives on lin…