Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Mar 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Putting function in variable calls it.

    I'm messing around with doing stuff with Canvas on the Wii U web browser, because it can take advantage of the Wii U controller. Someday I hope to make a game in Canvas specifically for play on the Wii U.

    Anyway, for reasons I won't get into, I have to make my own function that emulates a for loop. It's imperitive that this function calls other functions from variables. No problem there, except that for some reason, declaring a variable as a function seems to call that function once.

    Here, go to the W3C TryIt Editor and insert this code...
    Code:
    <!DOCTYPE html><html><body>
    
    
    
    <script type="text/javascript">
    
    var canvas = document.createElement("canvas");
    var ctx = canvas.getContext("2d");
    canvas.width = 200;
    canvas.height = 200;
    document.body.appendChild(canvas);
    // Creates the canvas and sticks it on the page.
    
    
    var circleX = canvas.width / 2;
    var circleY = canvas.height / 2;
    var circleRadius = 24;
    var circleColor = "#FF0000";
    // Starts the circle in the center of the canvas and colors it red.
    
    
    function clearScreen(){
    ctx.clearRect(0, 0, canvas.width, canvas.height);}
    // Erases the canvas. Kind of...kind of an obvious one, there >_>
    
    
    function drawCircle(){
    ctx.beginPath();
    ctx.arc(circleX, circleY, circleRadius, 0, Math.PI*2, true);
    ctx.fillStyle=circleColor;
    ctx.fill();}
    // Draws a filled circle with circleRadius radius at (circleX, circleY).
    
    
    // Begin messy test stuff...
    
    var loopEndValue = 11;
    var whatevs = 7;
    
    var loopThisFunction = blueCircle();
    
    function blueCircle(){
    ctx.beginPath();
    ctx.arc(36, 36, 24, 0, Math.PI*2, true);
    ctx.fillStyle="#ABCFED";
    ctx.fill();}
    
    function fiveLoopChecker(){
    switch (true){
    case (whatevs < 0):
    document.write("Less than 1");
    break;
    case (whatevs > -1 && whatevs < 6):
    document.write("inbetween -1 and 5");
    break;
    case (whatevs == loopEndValue):
    document.write("Equal to 11");
    break;}}
    
    
    function jamesLoopton(){
    fiveLoopChecker();
    clearScreen();
    drawCircle();}
    // The game loop.
    
    
    setInterval(jamesLoopton, 16);
    // Runs the game loop every sixteen milliseconds. Almost synchs up with 60 FPS.
    
    
    </script></body></html>
    When you submit the code, you'll notice that a blue circle flashes on the screen for a second. Well, if you comment out "var loopThisFunction = blueCircle();" guess what? The blue circle doesn't flash...

    Code:
    <!DOCTYPE html><html><body>
    
    
    
    <script type="text/javascript">
    
    var canvas = document.createElement("canvas");
    var ctx = canvas.getContext("2d");
    canvas.width = 200;
    canvas.height = 200;
    document.body.appendChild(canvas);
    // Creates the canvas and sticks it on the page.
    
    
    var circleX = canvas.width / 2;
    var circleY = canvas.height / 2;
    var circleRadius = 24;
    var circleColor = "#FF0000";
    // Starts the circle in the center of the canvas and colors it red.
    
    
    function clearScreen(){
    ctx.clearRect(0, 0, canvas.width, canvas.height);}
    // Erases the canvas. Kind of...kind of an obvious one, there >_>
    
    
    function drawCircle(){
    ctx.beginPath();
    ctx.arc(circleX, circleY, circleRadius, 0, Math.PI*2, true);
    ctx.fillStyle=circleColor;
    ctx.fill();}
    // Draws a filled circle with circleRadius radius at (circleX, circleY).
    
    
    // Begin messy test stuff...
    
    var loopEndValue = 11;
    var whatevs = 7;
    
    var loopThisFunction = blueCircle();
    
    function blueCircle(){
    ctx.beginPath();
    ctx.arc(36, 36, 24, 0, Math.PI*2, true);
    ctx.fillStyle="#ABCFED";
    ctx.fill();}
    
    function fiveLoopChecker(){
    switch (true){
    case (whatevs < 0):
    document.write("Less than 1");
    break;
    case (whatevs > -1 && whatevs < 6):
    document.write("inbetween -1 and 5");
    break;
    case (whatevs == loopEndValue):
    document.write("Equal to 11");
    break;}}
    
    
    function jamesLoopton(){
    fiveLoopChecker();
    clearScreen();
    drawCircle();}
    // The game loop.
    
    
    setInterval(jamesLoopton, 16);
    // Runs the game loop every sixteen milliseconds. Almost synchs up with 60 FPS.
    
    
    </script></body></html>
    That means that simply declaring the variable "loopThisFunction" to be equal to the function "blueCircle" calls that function. How am I supposed to put a function into a variable without calling it?

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,983
    Thanks
    56
    Thanked 557 Times in 554 Posts
    quite possibly
    Code:
    var loopThisFunction = blueCircle;

  • Users who have thanked xelawho for this post:

    SomethingFunny (04-20-2013)

  • #3
    New Coder
    Join Date
    Mar 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Holy crap that actually works O_O
    I didn't think it could be that simple.

    And if I plug "loopThisFunction();" into the game loop, it draws the blue circle. Woo-hoo!

    Thanks so much for the ultra-fast reply ^_^

    (P.S. Do you know how to mark a topic as Resolved? I don't see a button anywhere, and I can't even edit the topic post of any of my topics I've created...)

  • #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,983
    Thanks
    56
    Thanked 557 Times in 554 Posts
    I think you have to have a certain amount of posts or something before you're allowed to edit (includes mark as resolved) your posts - an anti-spam move. Some info here

  • #5
    New Coder
    Join Date
    Mar 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Well it makes sense that you have to have X posts before you can edit, to prevent spambots from jacking up stuff, but how could a spambot possibly abuse MarkAsResolved?

    I've got all these topics, and I can't mark them as resolved (which would be very nice etiquette), because I don't have enough posts.

    The situation will resolve itself eventually, as I post more, but still. I feel like a jerk for not marking my topics as resolved.

    Anyway, thanks for the link.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •