• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Change layer for symbol in code - HTML5 Canvas

Engaged ,
Feb 21, 2022 Feb 21, 2022

Copy link to clipboard

Copied

How can a symbol be changed to a layer through JavaScript?

Views

878

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
community guidelines

correct answers 3 Correct answers

Community Expert , Feb 21, 2022 Feb 21, 2022

understood, and that does make sense.

 

so, assuming your code is on the main timeline:

 

to make the lens appear to be above everything else on the main timeline, use

 

this.addChild(this.ConcaveSphereLens);

 

the lens will remain the topmost object on your main timeline unless and until you either reparent it (eg, using addChild) or you use similar code applied to some other object.

 

and the easiest, but not only, way to make the lens above some things and below others is to add a movieclip (

...

Votes

Translate

Translate
Community Expert , Feb 23, 2022 Feb 23, 2022

do the same for scaleX and scaleY.

Votes

Translate

Translate
Community Expert , Feb 24, 2022 Feb 24, 2022

this:

 

item.x-=item.lensParentLeft.x-9.5;
		item.y-=item.lensParentLeft.y-9.5;

 

suggests item.lensParentLeft is not aligned at 0,0 on its own timeline, and is 19px wide and high and is centered.  align at 0,0 and you won't need that fudge factor. or if that's where it needs to be aligned for other reasons use:

 

item.x-=item.lensParentLeft.x-item.lensParentLeft.width/2;
item.y-=item.lensParentLeft.y-item.lensParentLeft.height/2;

Votes

Translate

Translate
Engaged ,
Feb 23, 2022 Feb 23, 2022

Copy link to clipboard

Copied

I made the stage really big in the document settings.

 

The parented lens is attached to the parent OK, but it was way down in the bottom left corner, normally off stage in the original stage size.  It has also lost it's scale as set as an instance.  

 

2022-02-24_10-45-58.jpg

Votes

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
community guidelines
Community Expert ,
Feb 23, 2022 Feb 23, 2022

Copy link to clipboard

Copied

you mean bottom right (the sum of those two x,y's).

Votes

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
community guidelines
Engaged ,
Feb 23, 2022 Feb 23, 2022

Copy link to clipboard

Copied

Yeah bottom right X Y

 

Votes

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
community guidelines
Engaged ,
Feb 23, 2022 Feb 23, 2022

Copy link to clipboard

Copied

I know it's attached to the parent MC as when I click to rotate that lensHolder, the lens rotates. Though the rotation/transform point is  the the centre of the lenHolder/hitbox, so it appears the lens has laos lost it's original transform point. A solution might be to reset it's transfom point once parented back to the lensHolder (or parent MC)...

Votes

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
community guidelines
Engaged ,
Feb 23, 2022 Feb 23, 2022

Copy link to clipboard

Copied

Or else subtract the X and Y of the parent from the lens X and Y once parented??  If so, how would that be done?

Votes

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
community guidelines
Community Expert ,
Feb 23, 2022 Feb 23, 2022

Copy link to clipboard

Copied

just assign the x and y of the lens when reparenting.

Votes

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
community guidelines
Engaged ,
Feb 23, 2022 Feb 23, 2022

Copy link to clipboard

Copied

You mean when setting the parent?  Setting the X Y of the lens after parenting does not seem to do anything...

 

function onMouseUp(evt){
	var item = evt.currentTarget;
	item.drag = false;
	var pt = item.localToLocal(item.dot.x, item.dot.y, item.LFLensHolder.hitBox);
	if(item.LFLensHolder.hitBox.hitTest(pt.x, pt.y) ){
		item.x = item.LFLensHolder.x;
		item.y = item.LFLensHolder.y;
		item.lensParentLeft.addChild(item);
		//console.log(item.parent);
		item.x = 662.65;
		item.y = 539.2;
	}
}

Votes

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
community guidelines
Community Expert ,
Feb 23, 2022 Feb 23, 2022

Copy link to clipboard

Copied

try

 

item.x-=item.lensParentLeft.x;

item.y-=item.lensParentLeft.y;

Votes

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
community guidelines
Engaged ,
Feb 23, 2022 Feb 23, 2022

Copy link to clipboard

Copied

Yes, that works with a bit of offset to the x and y.

 

What about the size?  The lens has increased in size, it seems that the size has been cumulative as well as the position...

Votes

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
community guidelines
Community Expert ,
Feb 23, 2022 Feb 23, 2022

Copy link to clipboard

Copied

do the same for scaleX and scaleY.

Votes

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
community guidelines
Engaged ,
Feb 23, 2022 Feb 23, 2022

Copy link to clipboard

Copied

@kglad  Thanks for all of  your help with this.  Greatly appreciated!

 

One last question on this thread.  How do I access the instance name of item in the following code, that is after the addChild??

 

var Lenses = [this.ConcaveSphereLens, this.ConvexSphereLens, this.ConcaveCylinderLens, this.ConvexCylinderLens, this.PrismLens, this.AccessoryLens];

	// Apply the same code for each lens MovieClip symbol in the array Lenses with a For Loop
for(var i = 0; i<Lenses.length; i++){	
	Lenses[i].on("mousedown", onMouseDown.bind(this));
	Lenses[i].on("pressmove", onMouseMove.bind(this));
	Lenses[i].on("pressup", onMouseUp.bind(this));
	Lenses[i].LFLensHolder = this.LFLensHolder;
	Lenses[i].lensParentLeft = this.lensParentLeft;
	Lenses[i].originX = Lenses[i].x;
	Lenses[i].originY = Lenses[i].y;
}

function onMouseDown(evt){
	var item = evt.currentTarget;
	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 onMouseUp(evt){
	var item = evt.currentTarget;
	item.drag = false;
	var pt = item.localToLocal(item.dot.x, item.dot.y, item.LFLensHolder.hitBox);
	if(item.LFLensHolder.hitBox.hitTest(pt.x, pt.y) ){
		item.x = item.LFLensHolder.x;
		item.y = item.LFLensHolder.y;
		item.lensParentLeft.addChild(item);
		//console.log(item.parent);
		item.x-=item.lensParentLeft.x-9.5;
		item.y-=item.lensParentLeft.y-9.5;
		item.scale = 0.54;
 	}
}

function onMouseMove(evt){
	var item = evt.currentTarget;
	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;
	}
}

 

 

 

Votes

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
community guidelines
Engaged ,
Feb 23, 2022 Feb 23, 2022

Copy link to clipboard

Copied

Ah, item.name

Votes

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
community guidelines
Community Expert ,
Feb 24, 2022 Feb 24, 2022

Copy link to clipboard

Copied

LATEST

this:

 

item.x-=item.lensParentLeft.x-9.5;
		item.y-=item.lensParentLeft.y-9.5;

 

suggests item.lensParentLeft is not aligned at 0,0 on its own timeline, and is 19px wide and high and is centered.  align at 0,0 and you won't need that fudge factor. or if that's where it needs to be aligned for other reasons use:

 

item.x-=item.lensParentLeft.x-item.lensParentLeft.width/2;
item.y-=item.lensParentLeft.y-item.lensParentLeft.height/2;

Votes

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
community guidelines