Vanilla JS Tutorials

JsIso Tutorials

RSS feed

03 - Adding a controllable player

Currently all we have covered is creating a map. Not any old map though, a map which is generated from a two dimensional array, a map which brings graphical jealousy to those with even the upper most advanced Photoshop skills.
For this is an isometric map. An isometric map we will now add a magnificent controllable cuboid square pyramid.

A square pyramid with depth like Ralph.



As we will be moving Ralph over our fields he will need to be like everything else a .png for the transparency, plus it is his preferred file type, bless him. You can download Ralph from this and bump him into your image folder:



For simplicity of this tutorial we will just load Ralph into our current list of images being loaded. Ideally you could use multiple image arrays, for land, characters, objects etc to help keep things organized and separate.

var tileGraphicsToLoad = ["/tutorials/images/water.png","/tutorials/images/land.png", "/tutorials/img/ralph.png"],

Next we can throw Ralph into the map pretty easily by giving him a tile position within the map. We set the initial position after our map array creation:
var playerX = 0;
var playerY = 3;

These will be the X and Y coordinates of Ralph and shall be updated whenever a player uses the arrows keys.

Add the following to the drawMap() function after the drawing of the tile:
if (playerX === i && playerY === j) {
    ctx.drawImage(tileGraphics[2], (i - j) * tileH + mapX, (i + j) * tileH / 2 + mapY - tileH);
}

We know Ralph is sitting as element 2 in the image array so we just draw him whenever our tile draw loops match the X and Y position.

Testing your script now you should see your player standing somewhere on your map. Obviously Ralph can move, so the last thing to do is add the key movement and let him be free.

For this we will be using the arrow keys, however WASD as an alternative can be easily used etc if you console.log the keyCode. We apply an event listener in our initial init call like this:
    isometric.addEventListener("keyup", function(e) {
      switch(e.keyCode) {
        case 37:
          playerX--;
        break;
        case 39:
          playerX++;
        break;
        case 38:
          playerY--;
        break;
        case 40:
          playerY++;
        break;
      }
      drawMap();
    });

Depending on the keyCode result from a key up we send Ralph that direction. Note, we also call drawMap after each press, so that visually Ralph moves.
And that should be everything you need to add a moveable player.
One thing you may want to add is a clearRect() to the draw, this prevents fragments of Ralph still being displayed if the new drawMap() doesn't cover all of the previous draw:

ctx.clearRect(0, 0, 500, 500);

Tutorial Outcome (View & Run Source)

26 comments

  1. Really appreciate the tutorials, helping me wrap my head around everything, thanks!

    winnipeg dj services Sun, 28 Apr 2019

  2. I really enjoyed reading the blog and wish the writer congrats for writing such an informative blog JSiso. candy crush soda saga

    — samuelddarden Wed, 29 May 2019

  3. This release is really informative and shows a good example of a nature-friendly project we can all spend our time on.

    Thatcher's Tree Service Gainesville FL Thu, 30 May 2019

  4. Your current web-site is fairly quickly growing to be certainly one of my top feature. So, I just stumbled on creative weblog and I just need to state that this amazing is a nice blog post. Bless you pertaining to this kind of knowledge. click here

    — Joshua A. Price Mon, 10 Jun 2019

  5. I agree with your article, I would like to share this with my friends for another resource.
    aluminum signs

    — Daniel Perez Wed, 26 Jun 2019

  6. Really great! Thanks for this step by step process of adding a controllable player, this will help a ton.

    Bikram Yoga Fri, 12 Jul 2019

  7. Neat!

    Interested in making your home "smart"? click here

    John K Sun, 14 Jul 2019

  8. Thanks for your step by step tutorial. I really appreciate it!
    remedy app

    — Meggy Tue, 23 Jul 2019

  9. I appreciate sharing your knowledge regarding about this topic. Thank you! carpet cleaning

    — fredluis Thu, 25 Jul 2019

  10. Your blog provided us with valuable information to work with coding. Each & every tips of your post are awesome. pixonic

    — Patricia J. Clark Thu, 25 Jul 2019

  11. Really I Appreciate The Effort You Made To Share The Knowledge. This Is Really A Great Stuff For Sharing. Keep It Up . Thanks For Sharing.
    Marketing Project Help

    Marketing Project Help Sat, 27 Jul 2019

  12. Your instructions on your tutorials are well-explained. Thank you so much. :)
    Crown Sterling

    Crown Sterling Wed, 31 Jul 2019

  13. Codes have been applied on one of our telemarketing merchant account site and it worked well! Kudos! :)

    — Horea Kaii Tue, 6 Aug 2019

  14. This is great thanks!!

    MMA Gym Vancouver Wed, 7 Aug 2019

  15. Please keep us up to date like this. Thanks for sharing… car alarm

    — Cassandra D. Everhart Mon, 12 Aug 2019

  16. Wow, your invention is amazing! Thanks for this tutorial, I finally learned something new.

    tabby concrete charleston Mon, 12 Aug 2019

  17. Thank you for this tutorial! This is really helpful and useful I'm glad I found your site. Thanks so much!

    theuselessweb.com

    — Matt Tue, 13 Aug 2019

  18. Keep the goods coming! Tree Services 

    Keizer Tue, 13 Aug 2019

  19. I was always good at math, but this seems pretty complex. For those that understand, it seems like a thorough article. I'll try and learn the language and be back to check it out again, thank you!
    Tree Pruning Service
    Deep Cleaning Service
    Cement Work

    — Hank Gilbertson Thu, 15 Aug 2019

  20. I admire how it works, I usually did this before. Thanks | Commercial Roofing Cincinnati Ohio

    — Kyle Thu, 15 Aug 2019

  21. Dog Walking Service Vancouver if you're looking for a great dog waker in Vancouver or Toronto
    pool liner repair If you need pool liner repair in Ajax or anywhere in the Durham Region
    Bad credit car loan if you're looking for a bad credit car loan these are your guys for sure!
    Acting school If you or anyone in Vancouver are looking for acting classes in Vancouver look these guys up.
    tow truck youngstown If you need a towing service in Youngstown Ohio, please take a look at Ytown Towing.
    tree service Not surprised the are the leading tree service in Erie PA.
    towing Richmond another great towing company although they are in Richmond BC Canada.
    mobile mechanic Victoria You never know when you're gonna need a mobile mechanic to come to your car!
    junk hauling in case you have any rubbish you need removed.
    moving company if you need a great moving company in Erie Pa or check out movers youngstown if youre in Youngstown Ohio. Dont forget to check out
    water softener if you want some clean and healthy water and you live in Winnipeg Manitoba.
    dog trainers if you need your doggy trained. Also great contractors in Erie PA at https://eriepacontractors.com/">general contractor

    dog trainers if you need dog obedience training

    — Tim Smith Wed, 21 Aug 2019

  22. You have a nice flow of information, coding tutorial is well appreciated!| led car kits

    — Danny Thu, 22 Aug 2019

  23. Thanks for sharing your knowledge, I really appreciate your work. Hope to see more posts from you. | System For Awards Renewals

    — Anna Smith Mon, 26 Aug 2019

  24. Hi! I like this website because i have a lot of options to choose and good info.thanks alot god blessed.click site

    click site Tue, 3 Sep 2019

  25. "Bron: Wikipedia. Een vloer is de bodem van een ruimte
    of vertrek in een gebouw. Een vloer is een horizontaal
    en constructief element in een gebouw, dat een
    dragende en tevens een scheidende functie heeft.
    Het draagt wat op de vloer aanwezig is, zoals meubilair,
    apparatuur, mens en wanden.

    Het scheidt ruimten binnen eenzelfde gebouw, het scheidt
    binnen en buiten door middel van een dakvloer en het
    scheidt woningen als een woningscheidende vloer.

    Inrichting: Vloerbedekking, tapijt, parket, hout,
    laminaat etc. Isolatiefolie of geluidswerende vloer-
    platen voor reductie geluid en temperatuur lekken.

    Houdt altijd rekening welk materiaal u wilt gebruiken
    zeker als u vloerverwarming heeft en de isolatie
    waarde bepalend is voor de warmte doorgifte.
    Een ervaren vloerenspecialist kan u uiteraard
    volledig informeren welke vloer of ondergrond
    toepasbaar is in de ruimte die moet worden voorzien
    van een nieuwe vloer.

    Spreek altijd duidelijk van te voren de prijs af
    ook de kosten van het leggen zodat u niet voor
    verrassingen komt te staan en u binnen uw
    budget blijft en misverstanden te voorkomen." Verkoop vloeren of inhuren vloerenlegger

    — muneer ahmed Tue, 3 Sep 2019

  26. Thank you for sharing this article publicly, everything you have written is useful. Thanks | septic tank service asheville nc

    — Ariadna Mules Wed, 4 Sep 2019

Please insert the result of the arithmetical operation from the following image:

Please insert the result of the arithmetical operation from this image. =