交互事件

Demo

https://eva.js.org/playground/#/event

Member

hitArea object 可选

点击热区,带有透明度的图片的响应区域是整个 GameObject 的矩形区域,所以,可以通过 hitArea 属性设置响应区域。 hitArea 区域的属性值相对于 GameObject 的矩形区域。 正常来说无需设置 hitArea。

| | 圆形 | HIT_AREA_TYPE.Circle | {style: {x,y,radius}} | | 椭圆 | HIT_AREA_TYPE.Ellipse | {style:{x,y,width,height}} | | 矩形 | HIT_AREA_TYPE.Rect | {style:{x,y,width,height}} | | 圆角矩形 | HIT_AREA_TYPE.RoundedRect | {style:{x,y,width,height,radius}} | | 多边形 | HIT_AREA_TYPE.Polygon | {style: {paths: [x,y,x,y,x,y]}} 或 {style: {paths: [{x,y},{x,y},{x,y}]}} |

  1. import { Event, HIT_AREA_TYPE } from '@eva/plugin-renderer-event'
  2. const evt = image.addComponent(
  3. new Event({
  4. hitArea: {
  5. type: HIT_AREA_TYPE.Polygon,
  6. style: {
  7. paths: [107, 49, 148, 24, 172, 28, 198, 77, 189, 106, 123, 198, 71, 180, 10, 80, 34, 32, 90, 37]
  8. }
  9. }
  10. })
  11. )

Method

Event Component 用来支持 Entity 的事件绑定,事件有

| | tap | 点击,如果 touchstart 和 touchend 都在当前 gameObject 上会被触发,如果不需要可按照一定条件在 touchend 上面添加 e.stopPropagation() | | touchstart | | | touchmove | 不在当前物体上 move 也会触发,很奇怪 | | touchend | | | touchendoutside | 当 touchend 的时候,touch 已经不在物体上时,会触发 |

on (eventName, listener)

监听事件

off (eventName, listener)

移除监听

emit (eventName, …args)

触发事件

once (eventName, listener)

监听一次性事件

Instance Event’s Arguments

listener 函数的参数

  1. {
  2. data:{
  3. pointerId: number,
  4. position: {x, y}
  5. },
  6. stopPropagation: ()=>{},
  7. gameObject: gameObject
  8. }

阻止事件冒泡

  1. event.stopPropagation()