Skip to main content
Romain29189298g7i4
Participating Frequently
May 8, 2023 12:06 PM UTC
Answered

How I can center tow movieClip after an hitTest, because I have a problème with my code ?

Hello,

I have an other problem.

I made fort the test a simple animation. I have tow movieClips inside (Dot1 and Dot2) a MovieClip from the library called (LineMovieClip). I trace a line between the tow "Dot" and I can move the "Dot2" with startDrag. I have a red square on stage, and  I made an hitTest. When that hitTest is "true" I'm trying to center the "Dot2" with the "redSquare", but I have a problème because the .x and .y aren't in the same "stage" (on on the principal the "square", and on the "LineMovieClip")

My code is :

"

let root = this;

var MakeTheLine = new createjs.Shape();
this.addChild(MakeTheLine);

let Line_mc = new lib.LineMovieClip();

this.addChild(Line_mc);
MakeTheLine.x = Line_mc.x = 100;
MakeTheLine.y = Line_mc.y = 100;

function MakeTheLineF() {
MakeTheLine.graphics.clear();
console.log(MakeTheLine.graphics)
MakeTheLine.graphics.setStrokeStyle(3);
MakeTheLine.graphics.beginStroke("#0000ff");
MakeTheLine.graphics.moveTo(Line_mc.Dot1.x, Line_mc.Dot1.y);
MakeTheLine.graphics.lineTo(Line_mc.Dot2.x, Line_mc.Dot2.y);
}

Line_mc.Dot2.on("mousedown", onMouseDownDot2.bind(this));
Line_mc.Dot2.on("pressmove", onMouseMoveDot2.bind(this));
Line_mc.Dot2.on("pressup", onMouseUpDot2.bind(this));

function onMouseDownDot2 (evt){

var item = Line_mc.Dot2;
item.offset = {x:0, y:0};

var pt = item.parent.globalToLocal(evt.stageX, evt.stageY);
item.offset.x = pt.x - item.x;
item.offset.y = pt.y - item.y;
item.drag = true
};


function onMouseMoveDot2(evt){

var item = Line_mc.Dot2;
if (item.drag){
var pt = item.parent.globalToLocal(evt.stageX , evt.stageY);
item.x = pt.x - item.offset.x;
item.y = pt.y - item.offset.y;

}
MakeTheLineF();
};

function onMouseUpDot2(evt){
var pt = Line_mc.localToLocal(Line_mc.Dot2.x,
Line_mc.Dot2.y, root.RedSquare_mc);

if(root.RedSquare_mc.hitTest(pt.x, pt.y)){
console.log("Hit");
Line_mc.Dot2.x = root.RedSquare_mc;
Line_mc.Dot2.y = root.RedSquare_mc;
}
}
//
MakeTheLineF();

"

==> the problème is here :

"

Line_mc.Dot2.x = root.RedSquare_mc;
Line_mc.Dot2.y = root.RedSquare_mc;

"

Can you help me.

Thanks very much.

Romain

    This topic is closed to new replies. Start a new post to keep the conversation going.
    kglad
    kgladCorrect answer
    Community Expert
    May 16, 2023 12:12 PM UTC

    Hello, nothing that work very well.

    Thanks for everything


    you're welcome.

    9 replies

    kglad
    Community Expert
    Community Expert
    May 8, 2023 1:31 PM UTC

    you should spend a few minutes learning how to debug your own code.  then come here for more difficult problems.

     

    Lesson 1: https://youtu.be/bG05_am-fQI

    Lesson 2: https://youtu.be/H9e9toftvdM

    Translate
    Romain29189298g7i4
    Participating Frequently
    May 8, 2023 1:37 PM UTC

    Thanks, your right...

    I will see the video.

    But I am a simple biology teacher who try to make application for my student, I'm a beginner in HTML5, I was OK with AS2 and AS3, but that hard for me to find all the differences of psychology...

    Translate
    kglad
    Community Expert
    Community Expert
    May 8, 2023 1:42 PM UTC

    i understand.  we were all beginners at one time.

     

    the most important thing to learn with any coding system is how to debug your errors because you're going to make errors.  if you don't know how to debug your errors you waste a lot of time.

    Translate