OnDrag event listener

Locally I can use dragControls.addDragEventListener(“selectend”, func) by adding // @ts-ignore

When I upload to glitch I am getting a type script error
Uncaught TypeError: Cannot read properties of undefined (reading ‘addDragEventListener’)

What is the proper way to add event listener to drag controls, when I use the addeventlistener function it doesn’t do anything

if(dragControls) {
//dragControls.addDragEventListener(“selectend”, func) ?
dragControls.addEventListener(“selectend”, func) ?
}

Original Post on Discord

by user 931365436187111505

How are those dragControls referenced / how do you assign them?

Your code looks fine, looks rather like “wherever dragControls comes from” might be incorrect

const dragControls = GameObject.getComponent(this.gameObject, DragControls)
const func = () => {
console.log(“Drag Ended!”)
this.gameObject.position.set(this.currentCubeBelow.position.x,
this.gameObject.position.y,
this.currentCubeBelow.position.z)
this.selectEndEventListener = 0;
}

    if(this.selectEndEventListener==0) {
        if(dragControls) {
            //dragControls.addDragEventListener("selectend", func)
            dragControls.addEventListener("selectend", func)
            console.log(dragControls)
        }
        this.selectEndEventListener = 1;
    }

by user 931365436187111505

https://stump-fourth-produce.glitch.me/

by user 931365436187111505

trying to get the cactus to snap to the center of the tile when i stop dragging

by user 931365436187111505

This

const dragControls = GameObject.getComponent(this.gameObject, DragControls)

should be fine – how are you importing DragControls?

Also, could you clarify what addDragEventListener is? I don’t think that exists, you should probably use addEventListener(dragstart) and so on

wait that is probably it, i’m not importing dragcontrols from the right location, it was importing from - import {DragControls} from “three/examples/jsm/controls/DragControls”;

by user 931365436187111505

there is a class DragControls with method - addDragEventListener in the engine/engine-components folder

by user 931365436187111505

should I be importing that class?

by user 931365436187111505

works now, thank!

by user 931365436187111505

OK great! Sorry for the confusion :slightly_smiling_face: