1

I am using Adobe Animate CC and coding in JavaScript. I am trying to detect a collision between two symbols using the following code:

createjs.Ticker.on("tick", gameLoop, state);

function gameLoop(){

var pt = player.globalToLocal(collect.x, collect.y);
    if(player.hitTest(pt.x, pt.y)){
        alert("hit");           
    }
}

But this isn't doing anything, the statement is always false.

How do I solve this?

ALR
  • 441
  • 2
  • 7
  • 19
  • You are likely misunderstanding how hitTest works. Check out this recent post: http://stackoverflow.com/questions/36257762/flash-cc-createjs-hittest-works-without-hit/36360801#36360801 – Lanny Apr 08 '16 at 15:13

4 Answers4

0

You can use "setBounds(_x , _y , _wifth , _height)"

player.setBounds(-30 , -30 , 60 , 60);
enemy.setBounds(-30 , -30 , 60 , 60);

then to check :

bounds = player.getTransformedBounds();

if (bounds.intersects(enemy.getTransformedBounds())) 
{
    // something code
}

references in spanish : https://www.youtube.com/watch?v=AlqiplcnM7s

Chelendez
  • 163
  • 1
  • 9
  • Animate symbols already have a `nominalBounds` (although getBounds doesn't use it) -- so you could use those coordinate. – Lanny Jul 15 '16 at 15:25
0

Here is an example code for Adobe Animate CC that incorporates 6 drag able items, and 2 drop areas. This also works when the canvas is set to responsive. It should be easy enough to cannibalize this into whatever is needed between drag and drop and collision detection / hit test.

this.block1.on("pressmove", function (evt) {

var p = stage.globalToLocal(evt.stageX, evt.stageY);

evt.currentTarget.x = p.x;

evt.currentTarget.y = p.y;

});

this.block2.on("pressmove", function (evt) {

var p = stage.globalToLocal(evt.stageX, evt.stageY);

evt.currentTarget.x = p.x;

evt.currentTarget.y = p.y;

});

this.block3.on("pressmove", function (evt) {

var p = stage.globalToLocal(evt.stageX, evt.stageY);

evt.currentTarget.x = p.x;

evt.currentTarget.y = p.y;

});

this.block4.on("pressmove", function (evt) {

var p = stage.globalToLocal(evt.stageX, evt.stageY);

evt.currentTarget.x = p.x;

evt.currentTarget.y = p.y;

});

this.block5.on("pressmove", function (evt) {

var p = stage.globalToLocal(evt.stageX, evt.stageY);

evt.currentTarget.x = p.x;

evt.currentTarget.y = p.y;

});

this.block6.on("pressmove", function (evt) {

var p = stage.globalToLocal(evt.stageX, evt.stageY);

evt.currentTarget.x = p.x;

evt.currentTarget.y = p.y;

});





this.on("tick", update.bind(this));

function update() {

var b1 = this.block1.localToLocal(100, 0, this.frontAnswerDrop);

var b2 = this.block2.localToLocal(100, 0, this.frontAnswerDrop);

var b3 = this.block3.localToLocal(100, 0, this.frontAnswerDrop);

var b4 = this.block4.localToLocal(100, 0, this.backAnswerDrop);

var b5 = this.block5.localToLocal(100, 0, this.backAnswerDrop);

var b6 = this.block6.localToLocal(100, 0, this.backAnswerDrop);





if (this.frontAnswerDrop.hitTest(b1.x, b1.y)) {

console.log("b1 collided");

}

if (this.frontAnswerDrop.hitTest(b2.x, b2.y)) {

console.log("b2 collided");

}

if (this.frontAnswerDrop.hitTest(b3.x, b3.y)) {

console.log("b3 collided");

}

if (this.backAnswerDrop.hitTest(b4.x, b4.y)) {

console.log("b4 collided");

}

if (this.backAnswerDrop.hitTest(b5.x, b5.y)) {

console.log("b5 collided");

}

if (this.backAnswerDrop.hitTest(b6.x, b6.y)) {

console.log("b6 collided");

}

}
0

Try to make the object smaller. The code should work. It worked for me. I have a player which is a bike. The program shows collision when bike front wheels hit the cat.

When my bike back wheel strikes with the cat, it doesn't show collision alert.

Potato23
  • 65
  • 1
  • 6
0

Instead of createJs.ticker you can use SetInterval () function and keep remaining same code as it is.I think you made mistake in createJs

Potato23
  • 65
  • 1
  • 6