Doubt about creating an object

1

I'm new to the world of Javascript programming and I still have a lot of difficulties with things that are relatively simple, but I still can not solve them. I want to create a function that shows multiple objects on an HTML5 canvas inside the CC animate. I have several .png files in my library with names like: a1, a2, a3 ..., a13, b1, b2 ... etc. I randomly choose 3 or 4 of these objects to show on the canvas. However I do not know how to make the connection between the variable name that I pass to my function and the object to create on the canvas.

The drawing variable passed as a parameter is a string with the names of objects a1, a2, and so on. But when creating the new variable cards I can not put the name of the variable where I have in italic. There it accepts the name of the objects directly but I do not know how to bind to a variable that I pass as a parameter.

function Desenhar(**desenho**) {
   var cards = new lib.*desenho*();
   this.addChild(cards);
}   

Can anyone help me?

How? :) There are several functions, some have not finished, especially the drawing. The code is an implementation of the BlackJack game. I have buttons for calling the main functions. Apparently the game itself works. Distribute the cards, check the values, make the sums and say who won, lost or was eliminated. I have a simple interface made in the animate with the buttons. Now I wanted to draw the game as it happens. I can have errors in the code and I sure have them but now I hit the drawing problem. If it were an object only I could drag it to the canvas and show when I wanted to, but the cards are chosen randomly and are many cards. The cardVal passed as parameter I always assume the names of the cards, a1, a2, etc ...

    
asked by anonymous 25.11.2017 / 23:57

1 answer

2

You can access properties and methods by name (as a string) by using square brackets instead of the dot. You can do this:

function Desenhar(tipoDesenho) {
   var cards = new lib[tipoDesenho]();
   this.addChild(cards);
} 
    
26.11.2017 / 00:08