sendMessage函数是Yuka js库中的一个触发器(Trigger),用于向其他对象发送消息。
trigger.sendMessage( receiver, message, data )
该函数没有返回值。
const receiver = new Yuka.Entity();
const message = 'PLAYER_DEATH';
const data = {
killedBy: 'Monster',
damage: 60
};
trigger.sendMessage( receiver, message, data );
receiver.onMessage = function( message, data ) {
switch( message ) {
case 'PLAYER_DEATH':
console.log( 'Player has been killed by ' + data.killedBy + ' with ' + data.damage + ' damage.' );
break;
default:
console.error( 'Unknown message received: ' + message );
break;
}
};