Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

262
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!