array[ii].contact(d => {
console.log(d.type+' '+d.name);
});
I can get information about object d, but I can't get information about the second object array[ii], how do I find out the names of both collided?
array[ii].contact(d => {
console.log(d.type+' '+d.name);
});
I can get information about object d, but I can't get information about the second object array[ii], how do I find out the names of both collided?
Hi @romaniom - welcome to the forum.
If you have put the contact on the array[ii] then that is one object and the other is d. So array[ii].name and d.name. I don't usually use names... but those would be the two objects.
Hi, legend!
It's not that simple!
I create 20 balls through a cycle, there is a contact(d) and so on.
and when a collision occurs, it always says that the most recent one is colliding - array[19] and D.
isn't there a way to get information about two real objects inside the contact function?
I'm making a game where balls collide with each other. And when any two identical balls collide, they merge.
The first and 8th balls collided, he says that the 19th ball and the 8th collided.
If the fifth ball and 7 collided, he will say that the 19th ball and 7 collided.
that is, it is always assumed that the last element of the array collides.
for(var ii=0;ii<20;ii++){
cjsShape = new lib.ss();
ret=zimify(cjsShape, 80, 80).centerReg().pos(This.xx.x,This.xx.y).addPhysics({dynamic:true, shape:"circle", bounciness:0});
ret.type=getRandomInt(0,5);
ret.name=ii;
array.push(ret);
array[ii].contact(d => {
});
}
Ah... I see - that old problem. That used to be solved with closures but with the arrow function you can handle it easier.
loop(20, ii=>{
// do all your stuff
ret.contact(d => {
// use ret in here and it will be the right object
});
});
Or we could add an extra parameter on the contact callback that holds the currentTarget - the contact is a callback and, as such, does not have an event object to get the target or the currentTarget. I suppose knowing that would be handy, but since the object that the contact is put on is always one of the objects, we had not needed it.
We took a look and it was pretty simple to add a third parameter to the callback so that is the original object. Do a hard refresh and it should work for you - we have patched ZIM 017.
It really works! Thank you so much for your help, I've been trying for two days to find a way to solve this problem, I saw a LOOP in your code, but I didn't think it would help and didn't pay attention.
Thanks for ZIM, it helps me a lot as a flash programmer to use modern solutions for publishing in html.
Oh, thank you! Good!