Skip to main content
chenjil43641795
Legend
April 7, 2018
Answered

ANCC2018,hitTest,How to use

  • April 7, 2018
  • 1 reply
  • 610 views

createjs.Ticker.addEventListener("tick", pengzhuang);

function pengzhuang() {
var pz=bb.hitTest(aa.x,aa.y);
if(pz==true){alert("yes");}else{alert("no");}
stage.update();
}

Learn how to use HitTest, but fail。

Want to know why

    This topic has been closed for replies.
    Correct answer kglad

    use:

    createjs.Ticker.addEventListener("tick", pengzhuang.bind(this));

    function pengzhuang() {

    var pt = this.bb.globalToLocal(this.aa.x,this.aa.y);  // to check for upper left overlap.  for centers, adjust.
    if(this.bb.hitTest(pt.x,pt.y)){
    console.log('true');

    } else {

    console.log('false');

    }
    stage.update();
    }

    1 reply

    kglad
    Community Expert
    Community Expert
    April 8, 2018

    bb and aa (and therefore aa.x and aa.y) are probably undefined.  try:

    createjs.Ticker.addEventListener("tick", pengzhuang.bind(this));

    function pengzhuang() {
    if(this.bb.hitTest(this.aa.x,this.aa.y)){
    console.log('true');

    } else {

    console.log('false');

    }
    stage.update();
    }

    chenjil43641795
    Legend
    April 8, 2018

    kglad
    Community Expert
    Community Expert
    April 9, 2018

    Now it's on the left.

    What to do if it's a rectangle。

    localToLocal,It doesn't work.

    createjs. Ticker.addEventListener("tick", pengzhuang.bind(this));


    function pengzhuang() {

    var ax=this.aa.x
    var ay=this.aa.y

    var p = this.bb.localToLocal(ax,ay,this.aa);

    if (this.bb.hitTest(p.x,p.y)) {
        this.aa.alpha = 0.1;
    console.log('yes');
    } else {
        this.aa.alpha = 0.9;
    console.log('no');
    }
    stage.update();
    }


    again, // to check for upper left overlap.  for centers, adjust.