S
S
Sergey2021-01-09 15:43:12
JavaScript
Sergey, 2021-01-09 15:43:12

How to properly create handlers in playcanvas?

Good afternoon. The task in playcanvas is to generate primitives and hang handlers on each or one common one, so that when you click, you get exactly the selected object. In the code below, the handler refers to all created objects at once, which is logical in principle. The evt object also has no reference to the clicked object. Tell me what method to get the clicked primitive

var CreateCube = pc.createScript('createCube');

// initialize code called once per entity
CreateCube.prototype.initialize = function() {
    
    var el = this.app.root.findByName('BoxTemplate')
    for(var i = 0; i < 10; i ++){
        var newEl = el.clone()
        newEl.name = 'BoxTemplate'+ i
        newEl.rigidbody.teleport(0,0,5)
        this.app.root.addChild(newEl)
this.app.mouse.on(pc.EVENT_MOUSEDOWN, this.onMouseDown, newEl)
        
    }
};

// update code called every frame
CreateCube.prototype.update = function(dt) {
    
};
CreateCube.prototype.onMouseDown = function(evt){
  console.log(this)
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question