www.webdeveloper.com
Results 1 to 9 of 9

Thread: Resetting position of object.

  1. #1
    Join Date
    Mar 2010
    Posts
    19

    Resetting position of object.

    I am currently making a frogger game and I have code that allows him to move all over the game at the moment using keystrokes.

    I am trying to write code that causes the frogs position to be reset to his starting place when he reaches a certain area on the screen.

    Here is my current code:

    // Resetting Frog Position (Upon Reaching a Lilypad): (CURRENTLY NOT WORKING)
    //var frogy=650;
    //var frogx=325;

    function resetFrog(){
    document.getElementById("frogger").style.left=frogx + "px";
    document.getElementById("frogger").style.top=frogy + "px";
    if(frogx==325 && frogy==675){frogx=325, frogy=650} //Lilypad 1
    }

    resetFrog();

    In, if(frogx==*** && frogy==***), the asterix define where when the frog moves into that position he is moved back to frogx325 and frogy650.

    Any help please!?



    W1S3B0Y,

  2. #2
    Join Date
    Dec 2009
    Location
    Colorado
    Posts
    104
    It's not really good coding standards to put if statement all on one line. Your problem seems to be here. Use semicolons. Even though they are not required it can save you hours of frustration without sometimes.

    if(frogx==325 && frogy==675){frogx=325, frogy=650} //Lilypad

    try this:

    if(frogx==325 && frogy==675){
    frogx=325;
    frogy=650;
    } //Lilypad

  3. #3
    Join Date
    Mar 2010
    Posts
    19
    I have done this, but it does not cause it to work, thanks for the advice however.

    Here is my frog movement code which works:

    // Frog Movement:
    var frogy=650;
    var frogx=325;

    function frogmoveup(){
    frogy-=25;
    if (frogy <= 100){frogy =100}
    document.getElementById("frogger").style.top=frogy + "px";
    }

    function frogmovedown(){
    frogy+=25;
    if (frogy >= 650){frogy =650}
    document.getElementById("frogger").style.top=frogy + "px";
    }

    function frogmoveleft(){
    frogx-=25;
    if (frogx <= 50){frogx =50}
    document.getElementById("frogger").style.left=frogx + "px";
    }

    function frogmoveright(){
    frogx+=25;
    if (frogx >= 625){frogx =625}
    document.getElementById("frogger").style.left=frogx + "px";
    }

  4. #4
    Join Date
    Dec 2009
    Location
    Colorado
    Posts
    104
    Try == in your if statements. Equal to (remember, the "=" sign assigns a value to a variable, the "==" compares two values for equality.)

  5. #5
    Join Date
    Mar 2010
    Posts
    19
    I know the difference between == and = and I have used == in my if statement for resetting position.

  6. #6
    Join Date
    Dec 2009
    Location
    Colorado
    Posts
    104
    Ah so you have. Trying to help. I don't see anything else. If you're still having issues tomorrow I'll copy a paste your code and try it out. That's part of I don't like if's on one line. They can be hard to read.

  7. #7
    Join Date
    Mar 2010
    Posts
    19
    No problem, and if you could that would be really great thanks - I don't think there is any more I can do on my behalf due to limited knowledge of JS.

  8. #8
    Join Date
    Dec 2009
    Location
    Colorado
    Posts
    104
    I cut and pasted your code into Notepad++ and tinkered with it a little. There is nothing wrong with your Javascript. I don't know what you are passing into it though.

    A couple things came to mind.

    First if you are passing a variable from an html input tag to Javascript, it is not going to come in as an integer. It will come in as a string and you have to use parseInt or parseFloat and convert it in order to do the numeric comparisons you are trying to do.

    Secondly, if it were me, I would put some alerts in your functions. Make sure they are getting executed and make sure you are getting a value back that you are expecting.

    Without seeing all the code (html, javascript and images), it's hard to recreate your problem. But, your javascript is syntactically correct.

  9. #9
    Join Date
    Mar 2010
    Posts
    19
    When I insert alerts into the resetFrog function, they are all executed up until the if statement is made.

    If you like, I can send you a .rar file containing all my images and scripts for the game thus far?

    It is roughly 650kb in size I believe.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles