Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

263
Visualizações
How to use a function parameter, on a function inside this function

I know is hard to understand but, here's the code

function aleatorizar() {
    const random = Math.random() * canvas.width;
    return random;
}

function desenharbombas(x) {
    ctx.beginPath();
    ctx.arc(x, posição, 50, 0, 2 * Math.PI);
    ctx.stroke();
    ctx.fillStyle = "#000000";
    ctx.fill();

    function escrever() {
        ctx.font = "17px Arial Black";
        ctx.fillStyle = "#ffffff";
        ctx.fillText("texto", x, posição);
    }
    escrever()
}

function animar() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    desenharbombas()
    requestAnimationFrame(animar);
    //como passar parametro da função animar para o parametro da função desenhar bombas

}
animar()

So, as you can see, in the "desenharbombas" function, i need the parameter to determine where my "bombs" will be located in the x axis, and that number is randomized by the "aleatorizar" function, so i need a solution where i can put a parameter on the function "animar", (which would be the randomized number function), and pass it to the "desenhar bombas function". representing it, it would be something like this (this doesn't work btw)

function desenharbombas(x) {
    //x parameter defines the x axis of the bombs, and i need this number to be randomized
    ctx.beginPath();
    ctx.arc(x, posição, 50, 0, 2 * Math.PI);
    ctx.stroke();
    ctx.fillStyle = "#000000";
    ctx.fill();

    function escrever() {
        ctx.font = "17px Arial Black";
        ctx.fillStyle = "#ffffff";
        ctx.fillText("texto", x, posição);
    }
    escrever()
}

function animar(y) {
    //that's where the problems at, this solution doesn't work, i need to use a parameter to 
    another parameter in the "desenharbombas"
    function
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    desenharbombas(x = y)
    requestAnimationFrame(animar);
    //como passar parametro da função animar para o parametro da função desenhar bombas

}
animar(y = aleatorizar())
//this is the parameter i need to recieve to use in the "desenharbombas" function

For the love of god, i've been stuck on this for hours, please be my savior

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I believe something like this would work:

function aleatorizar() {
    const random = Math.random() * canvas.width;
    return random;
}

function desenharbombas(x) {
    ctx.beginPath();
    ctx.arc(x, posição, 50, 0, 2 * Math.PI);
    ctx.stroke();
    ctx.fillStyle = "#000000";
    ctx.fill();

    function escrever() {
        ctx.font = "17px Arial Black";
        ctx.fillStyle = "#ffffff";
        ctx.fillText("texto", x, posição);
    }
    escrever()
}

function animar(randomNumber) {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    desenharbombas(randomNumber)
    requestAnimationFrame(animar);
    //como passar parametro da função animar para o parametro da função desenhar bombas

}

const randomNumber = aleatorizar();
animar(randomNumber)

about 4 years ago · Juan Pablo Isaza Relatório

0

function aleatorizar() {
    const random = Math.random() * canvas.width;
    return random;
}

function desenharbombas(aleatorizar) {
    //call the function using function_name() and then assign it to a variable
    x = aleatorizar()
    ctx.beginPath();
    ctx.arc(x, posição, 50, 0, 2 * Math.PI);
    ctx.stroke();
    ctx.fillStyle = "#000000";
    ctx.fill();

    function escrever() {
        ctx.font = "17px Arial Black";
        ctx.fillStyle = "#ffffff";
        ctx.fillText("texto", x, posição);
    }
    escrever()
}

function animar() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    //pass the function as a parameter
    desenharbombas(aleatorizar)
    requestAnimationFrame(animar);

}
animar()

Let me know if this is what you were trying to do

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda