Creating multiple dynamic instances via new.lib HTML5 Canvas

New Here ,
Dec 30, 2020

Copy link to clipboard

Copied

I am rebuilding my old game built with AS2 for Adobe Animate canvas. Got stuck while duplicating the objects. Found this thread from JoãoCésar using new lib, and everything is fine but I would like to create multiple instances form my library item 'bug' (has linkage 'bug' as well). Pardon my code, but I would like to be something like this:

var that = this;
this.buttonAdd.addEventListener("click", addFromLibrary);

function addFromLibrary(e) {
	for (var i = 0; i < 5; i++) {
		var ["bug" + i] = new lib.bug();
		["bug" + i].x = 10 * i;
		["bug" + i].y = 10 * i;
		that.addChild["bug" + i];
	}
}

... where I expect all 5 flying bugs on stage. Obviously it doesn't work with dynamic variables. As I understand bracket notation should be used over eval in Animate javascript, but not sure how exactly should I use it. As furhter more I would like to access these created bugs for collision event as well. Many thanks!

 

 

TOPICS
Code

Views

64

Likes

Translate

Translate

Report

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

1 Correct Answer

Adobe Community Professional , Dec 30, 2020
JoãoCésar Adobe Community Professional , Dec 30, 2020
Hi.   There's no need to use bracket notation in this situation. But if you really need to use it, you should write something like: that["bug" + i].x = 10 * i;   Because bracket notation is used to access properties of an object.   And for your code to work, you could do something like this: var that = this; function addFromLibrary(e) { var i, bug; for (i = 0; i < 5; i++) { bug = new lib.bug(); bug.x = 10 * i; bug.y = 10 * i; that.addChild(bug); that.bugs.push(bug); } } that.bu...

Likes

Translate

Translate
Jump to answer Jump to answer
Adobe Community Professional ,
Dec 30, 2020

Copy link to clipboard

Copied

Hi.

 

There's no need to use bracket notation in this situation. But if you really need to use it, you should write something like:

that["bug" + i].x = 10 * i;

 

Because bracket notation is used to access properties of an object.

 

And for your code to work, you could do something like this:

var that = this;

function addFromLibrary(e)
{
	var i, bug;
	
	for (i = 0; i < 5; i++)
	{
		bug = new lib.bug();
		bug.x = 10 * i;
		bug.y = 10 * i;
		that.addChild(bug);
		that.bugs.push(bug);
	}
}

that.bugs = [];
that.buttonAdd.addEventListener("click", addFromLibrary);

 

Please notice that I'm storing the instances in a array for later reference. But it's not mandatory if all that you need is to spawn some instances at runtime.

 

I hope this helps.

 

Regards,

JC

Likes

Translate

Translate

Report

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

Copy link to clipboard

Copied

Thank you João,

Your suggestion worked as intended. Thanks a lot 🙂 I will try to develop the rest and hope to continue to move on. I believe I will make use of this array with instances as well. Have a great New Year!

Andrius

Likes

Translate

Translate

Report

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

Copy link to clipboard

Copied

That's great, Andrius! You're welcome!

 

I wish you a happy New Year and a fun time developing your game!

Likes

Translate

Translate

Report

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