Highlighted

Get global variables inside listener function .

New Here ,
Apr 16, 2019

Copy link to clipboard

Copied

Hi.

I have a drag and drop game for an educational purpose, and I want to rewrite my code from AS3 to javascript (createJS).

I have two movieclips on the stage  : dgABC ,dgDEF .

this is my code  :

var dg =  this.dgABC;

dg.name = "dgABC";

var dgt = this.dgDEF;

dgt.name = "dgDEF";

var draggedArray = [];

draggedArray.push(dg);

draggedArray.push(dgt);

container= new createjs.Container();

container.addChild(dg);

container.addChild(dgt);

function setlistener() {

for (var u = 0; u < draggedArray.length; u++) {

draggedArray.addEventListener("pressup", onpressup);

draggedArray.addEventListener("pressmove", onpressmove);

}

}

setlistener()  ;

//DRAG FUNCTIONALITY =====================

function onpressmove(evt) {

alert( container.getNumChildren());

container.setChildIndex(evt.currentTarget,container.getNumChildren() - 1);

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

evt.currentTarget.x = pt.x;

evt.currentTarget.y = pt.y;

stage.update();

}

//Mouse UP and SNAP====================

function onpressup(evt) {

// code..

}

but I get two problems :

1- alert( container.getNumChildren()); // return 0;  scope problem? so the function  : container.setChildIndex() dont work

2- I  use the other form of listener : dg.on("pressmove", onpressmove,this);

but without succes.

Hi Touy

touy62750769  wrote

but I get two problems :

1- alert( container.getNumChildren()); // return 0;  scope problem? so the function  : container.setChildIndex() dont work

2- I  use the other form of listener : dg.on("pressmove", onpressmove,this);

but without succes.

to your first problem:

getNumChildren() - I don't think this is a legible method for the Container class in EaselJS.

Then I'm not entirely sure, but having already "two movieclips on the stage  : dgABC ,dgDEF", I don't think you can add them to your container with the addChild() method. In my test setup I removed them from the stage and in the library I gave them linkage names: Dgabc and Dgdef. And coded

var dg = new lib.Dgabc();

dg.name = "dgABC";

var dgt = new lib.Dgdef();

dgt.name = "dgDEF";

Then further below

container = new createjs.Container();

container.addChild(dg, dgt);

console.log(container.numChildren);

Result in the console: 2 (as expected)

But I can't make them visible or make the container visible, I don't know. My suspicion is that the Container class is only feasible when one works with CreateJS independently from Animate. When I i.e. code:

stage.addChild(dg, dgt);

they become visible. I never worked with the Container class and wouldn't need it in an Animate scenario. I would use a movieclip-container instead.

Klaus

Views

232

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Get global variables inside listener function .

New Here ,
Apr 16, 2019

Copy link to clipboard

Copied

Hi.

I have a drag and drop game for an educational purpose, and I want to rewrite my code from AS3 to javascript (createJS).

I have two movieclips on the stage  : dgABC ,dgDEF .

this is my code  :

var dg =  this.dgABC;

dg.name = "dgABC";

var dgt = this.dgDEF;

dgt.name = "dgDEF";

var draggedArray = [];

draggedArray.push(dg);

draggedArray.push(dgt);

container= new createjs.Container();

container.addChild(dg);

container.addChild(dgt);

function setlistener() {

for (var u = 0; u < draggedArray.length; u++) {

draggedArray.addEventListener("pressup", onpressup);

draggedArray.addEventListener("pressmove", onpressmove);

}

}

setlistener()  ;

//DRAG FUNCTIONALITY =====================

function onpressmove(evt) {

alert( container.getNumChildren());

container.setChildIndex(evt.currentTarget,container.getNumChildren() - 1);

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

evt.currentTarget.x = pt.x;

evt.currentTarget.y = pt.y;

stage.update();

}

//Mouse UP and SNAP====================

function onpressup(evt) {

// code..

}

but I get two problems :

1- alert( container.getNumChildren()); // return 0;  scope problem? so the function  : container.setChildIndex() dont work

2- I  use the other form of listener : dg.on("pressmove", onpressmove,this);

but without succes.

Hi Touy

touy62750769  wrote

but I get two problems :

1- alert( container.getNumChildren()); // return 0;  scope problem? so the function  : container.setChildIndex() dont work

2- I  use the other form of listener : dg.on("pressmove", onpressmove,this);

but without succes.

to your first problem:

getNumChildren() - I don't think this is a legible method for the Container class in EaselJS.

Then I'm not entirely sure, but having already "two movieclips on the stage  : dgABC ,dgDEF", I don't think you can add them to your container with the addChild() method. In my test setup I removed them from the stage and in the library I gave them linkage names: Dgabc and Dgdef. And coded

var dg = new lib.Dgabc();

dg.name = "dgABC";

var dgt = new lib.Dgdef();

dgt.name = "dgDEF";

Then further below

container = new createjs.Container();

container.addChild(dg, dgt);

console.log(container.numChildren);

Result in the console: 2 (as expected)

But I can't make them visible or make the container visible, I don't know. My suspicion is that the Container class is only feasible when one works with CreateJS independently from Animate. When I i.e. code:

stage.addChild(dg, dgt);

they become visible. I never worked with the Container class and wouldn't need it in an Animate scenario. I would use a movieclip-container instead.

Klaus

Views

233

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Apr 16, 2019 0
Advocate ,
Apr 17, 2019

Copy link to clipboard

Copied

Hi Touy

touy62750769  wrote

but I get two problems :

1- alert( container.getNumChildren()); // return 0;  scope problem? so the function  : container.setChildIndex() dont work

2- I  use the other form of listener : dg.on("pressmove", onpressmove,this);

but without succes.

to your first problem:

getNumChildren() - I don't think this is a legible method for the Container class in EaselJS.

Then I'm not entirely sure, but having already "two movieclips on the stage  : dgABC ,dgDEF", I don't think you can add them to your container with the addChild() method. In my test setup I removed them from the stage and in the library I gave them linkage names: Dgabc and Dgdef. And coded

var dg = new lib.Dgabc();

dg.name = "dgABC";

var dgt = new lib.Dgdef();

dgt.name = "dgDEF";

Then further below

container = new createjs.Container();

container.addChild(dg, dgt);

console.log(container.numChildren);

Result in the console: 2 (as expected)

But I can't make them visible or make the container visible, I don't know. My suspicion is that the Container class is only feasible when one works with CreateJS independently from Animate. When I i.e. code:

stage.addChild(dg, dgt);

they become visible. I never worked with the Container class and wouldn't need it in an Animate scenario. I would use a movieclip-container instead.

Klaus

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 17, 2019 2
New Here ,
Apr 17, 2019

Copy link to clipboard

Copied

Thank you for your reply ,

the problem with:

var dg = new lib.Dgabc ();

stage.addChild (dg);

is that the MovieClip Dgabc becomes unstoppable !!

All attempts to stop

have failed. You can see this link: https://forums.adobe.com/thread/2366997.

about your problem "But I can't make them visible or make the container visible, I don't know. My suspicion is that the Container class is only feasible when one works with CreateJS independently from Animate." I am using : stage.addClid(container ). and children inside the container are visible.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 17, 2019 0
New Here ,
May 28, 2019

Copy link to clipboard

Copied

Now I can control the movieClip by the script " gotoAndStop ". the problem was that I have to uncheck "Use advanced layers " in the "Advanced Settings" ."Use advanced layers " disrupt the code.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 28, 2019 0
ClayUUID LATEST
Adobe Community Professional ,
May 28, 2019

Copy link to clipboard

Copied

Yet more evidence that advanced layers should be disabled by default. Animate team, are you listening?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 28, 2019 0