Есть свойство caller, но в typescript почему то не получается применить https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Function/caller
Делаю как то так
export class Shoot extends State {
shoot(point: PIXI.Point, state: string) {
console.log(this.shoot.caller);
this.playerComponents.network.socket.emit('touche', point);
this.playerComponents.changeState(this.playerComponents.states[state]);
}
}
Изначальная задача понять из какого класса был вызван метод и перейти в предыдущее состояние, для этого просто передаю строкой имя класса.
Пример того как вызывается функция
export class Move extends State {
timer: any;
move(input: number) {
clearTimeout(this.timer);
this.playerComponents.network.socket.emit('Input', input);
this.timer = setTimeout(() => {
this.playerComponents.changeState(this.playerComponents.states.Idle);
}, 500);
}
shoot(point: PIXI.Point) {
this.playerComponents.network.socket.emit('Input', 0);
this.playerComponents.changeState(this.playerComponents.states.Shoot);
this.playerComponents.state.shoot(point,'Move');
}
}
.....
states: any = {
'Idle' : new Idle(this),
'Move' : new Move(this),
'Shoot': new Shoot(this),
'Build': new Build(this),
};
.....
Минимальный пример
this.playerComponents.state.shoot(point,'Move');
console.log((this.shoot as any).caller);
– Stepan Kasyanenko 4 окт '17 в 9:21