Skip to main content
Participant
December 30, 2020
Answered

Creating multiple dynamic instances via new.lib HTML5 Canvas

  • December 30, 2020
  • 1 reply
  • 494 views

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!

 

 

This topic has been closed for replies.
Correct answer JoãoCésar17023019

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

1 reply

JoãoCésar17023019
Community Expert
JoãoCésar17023019Community ExpertCorrect answer
Community Expert
December 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.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

Participant
December 30, 2020

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

JoãoCésar17023019
Community Expert
Community Expert
December 30, 2020

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

 

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