Jumping and falling?
  • Hi Selim,

    I am working on a Mario type game. I have manage to simply implement left and right movement, walking etc, but I have no clue how to write a code for jumping and falling? I think I need to use Sinus or something , but I have no clue how. also I don't know how to check what is location of the block that I am currently standing on? because when I jump I need to know when the character land on the new block or the same block.

    I would really appreciate if you could share some sample code on that. Apologies if you have already explained that before, but I couldn't find anything like that in the forum.

    Cheers.
  • Hi the technique I use is basic Newtonian gravity:
    - choose the acceleration along the y-axis (0.5 for example)
    - for each frame do:
    var speedY = oldSpeedY + timeInterval * accelerationY;
    var posY = oldPosY + speedY * accelerationY;
    // detect collision detection and stop player if he colide with the ground.

    - for the x-axis movement you have to choice. Either you let the player control it in the same way he control it when he is walking or you make it constant.

    Now this will make the player fall in a natural way. If he fall to fast or to slow just change the acceleration.

    As for retrieving the tile coordinate you can use the tile id. The id has the following format:
    "#gQ_"+name+"_"+row+"_"+column

    So by simple parsing the id you can find what the tile indexes are and compare with the previous one. If you just want to now if the tile is another one than the previous you won't even have to parse the id, just compare it with the previous one.

    Keep in mind that there will be probably more than one tile that the player stand on at some times
  • Thanks a lot Selim,

    I will try that tonight and will let you know about the result.

    Many thanks
  • Hi Selim

    I have had a better look at the code i t all make sense but I have two remaining questions which I would highly appreciate if you could advice me.

    1- does the framework has any inbuilt timerInterval function that I can use? if not then how do you make the timer?What do you base it on? do you use something simple like a loop etc?

    2- regarding the block detection I have used the Tile Map Editor so I am using the automatically generated code. So now I have an array or arrays called map. but how do I actually get the tile id in first place to then get the indexes? is there any method for that? could you please give me sample or direct me to a similar code?



    Many thanks
  • selimselim
    Accepted Answer
    1 - You define the interval when you register the function with $.playground().registerCallback() (http://gamequeryjs.com/documentation/api/#registerCallback) there you provide as the second parameter a time in milliseconds. This time has to be a multiple of the refresh rate you set for your playground (30 by default)

    2 - the collision() methode return a list of colliding elements you can access their ID by doing:
    $(“#player”).collision().each(function(){
    var id = $(this).attr("id");
    // do something with the id
    });
  • Thanks Selim,
    This is exactly what I was missing, I couldn't what registerCallback exactly do.

    Many thanks <:-P

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!