google+javascriptbanktwitter@js_bankfacebook@jsbankrss@jsbank






Making Simple RPG Game with JavaScript and Crafty You are being fed up with the JavaScript games and trying to find out how to build Web-based JavaScript games? This JavaScript article tutorial may be a valuable reference source for you in this case.

This JavaScript game making tutorial guides us use a JavaScript game framework - Crafty - to create a simple JavaScript RPG game. This JavaScript RPG game is quite simple but it's so awesome with beautiful graphics, and its gameplay is not too easy to make us feel boredom. Basically, we need to do some necessary tasks such as define the characters, build JavaScript game scenes, create gameplay, making game's animations and combine them into a unified result.

The inner post page will show you all detailed instructions and comments, as well as demo to play with full JavaScript source code for downloading.


Label: simple RPG game, JavaScript and Crafty, JavaScript RPG game, JavaScript game framework, Crafty, gameplay, character

Free iPage Web Hosting for First Year NOW



If you're still looking for a reliable web host provider with affordable rates, why you don't take a little of time to try iPage, only with $1.89/month, included $500+ Free Extra Credits for the payment of 24 months ($45)?

Over 1,000,000+ existisng customers can not be wrong, definitely you're not, too! More important, when you register the web hosting at iPage through our link, we're going to be happy for resending a full refund to you. That's awesome! You should try iPage web hosting for FREE now! And contact us for anything you need to know about iPage.
Try iPage for FREE First Year NOW

Animation

view the demo | download demo

Now that the player can move, we want to play the animation we setup earlier.

// Create our player entity with some premade components
var player = Crafty.e("2D, DOM, player, controls, CustomControls, animate, collision")

  .attr({x: 160, y: 144, z: 1})

  .CustomControls(1)
  .animate("walk_left", 6, 3, 8)

  .animate("walk_right", 9, 3, 11)
  .animate("walk_up", 3, 3, 5)

  .animate("walk_down", 0, 3, 2)
  .bind("enterframe", function(e) {

    if (this.__move.left) {
      if (!this.isPlaying("walk_left"))

        this.stop().animate("walk_left", 10);
    }
    if (this.__move.right) {

      if (!this.isPlaying("walk_right"))
        this.stop().animate("walk_right", 10);

    }
    if (this.__move.up) {
      if (!this.isPlaying("walk_up"))

        this.stop().animate("walk_up", 10);
    }
    if (this.__move.down) {

      if (!this.isPlaying("walk_down"))
        this.stop().animate("walk_down", 10);

    }
  }).bind("keyup", function(e) {

    this.stop();
  });

On the enterframe event we want to know the direction the player is moving (using the movement booleans created in our component) and play the appropriate animation. We don’t want to play it if it is already playing however, so we use the .isPlaying() function. If it isn’t playing, stop whatever animation is playing with the .stop() function and play the correct one. Playing an animation is a matter of calling .animate() with the name of the animation and a duration in frames. Because we only have 3 frames for the animation, we want it to be fairly quick. We also want to stop any animation if a key is up.

Collision

Crafty provides collision detection between any two convex polygons. A collision exists when two entities intersect each other. We use the pre-made collision component to detect collisions with the boundary so the player can’t leave the stage.

// Create our player entity with some premade components
var player = Crafty.e("2D, DOM, player, controls, CustomControls, animate, collision")

  .attr({x: 160, y: 144, z: 1})

  .CustomControls(1)
  .animate("walk_left", 6, 3, 8)

  .animate("walk_right", 9, 3, 11)
  .animate("walk_up", 3, 3, 5)

  .animate("walk_down", 0, 3, 2)
  .bind("enterframe", function(e) {

    if (this.__move.left) {
      if (!this.isPlaying("walk_left"))

        this.stop().animate("walk_left", 10);
    }
    if (this.__move.right) {

      if (!this.isPlaying("walk_right"))
        this.stop().animate("walk_right", 10);

    }
    if (this.__move.up) {
      if (!this.isPlaying("walk_up"))

        this.stop().animate("walk_up", 10);
    }
    if (this.__move.down) {

      if (!this.isPlaying("walk_down"))
        this.stop().animate("walk_down", 10);

    }
  }).bind("keyup", function(e) {

    this.stop();
  }).collision()
  .onhit("wall_left", function() {

    this.x += this._speed;
    this.stop();

  }).onhit("wall_right", function() {
    this.x -= this._speed;

    this.stop();
  }).onhit("wall_bottom", function() {

    this.y -= this._speed;
    this.stop();

  }).onhit("wall_top", function() {
    this.y += this._speed;

    this.stop();
  });

Remember those labels we put on the bushes earlier? Now is when they become useful. The function .collision() is the constructor and accepts a polygon object (see Crafty.polygon) or if empty will create one based on the entity’s x, y, w and h values.

.onhit() takes two arguments, the first is the component to watch for collisions and the second is the function called if a collision occurs. If the player collides with any entity with the component wall_left, we need to move the player away from the wall at the same speed it is moving towards it. We need to do this for all other walls so depending on the direction, move the player in the opposite direction at the same speed. I also added the .stop() function so that it doesn’t keep animating when it isn’t moving.

Final Code

Putting together everything we learnt, we should have the following: crafty.js.

Now you should have the basics of an RPG!

If you need any support using Crafty, please visit the Crafty forums and Crafty documentation.

AIVideo-App.com
Generate your business videos by AI with voice or just text

chatGPTaz.com
Talk to ChatGPT by your mother language

AppAIVideo
Your first FREE AI Video App

Deepfake Video
Deepfake AI Video Maker

Deepfake
Deepfake AI Video Maker

AI Deep Fake
Deepfake AI Video Maker

AIvidio
AI Video Mobile Solutions

AIvideos
AI Video Platform & Solutions

AIvedio
AI Video App Maker

Artificial General Intelligence
Ai and higher level Artificial General Intelligence (AGI)

Artificial General Intelligence
Ai and higher level Artificial General Intelligence (AGI)

Faceswap AI Online
Swap Faces Video, Photo & GIFs Instantly with Powerful AI Tools - Faceswap AI Online FREE

Faceswap AI Online
Swap Faces Video, Photo & GIFs Instantly with Powerful AI Tools - Faceswap AI Online FREE

Faceswap AI Online
Swap Faces Video, Photo & GIFs Instantly with Powerful AI Tools - Faceswap AI Online FREE

Powerful AI Presentation PPT Maker for FREE
Build an impressive presentation with our free online AI presentation app

Your next top AI Assistant
Claude AI, developed by Anthropic

Your next top AI Assistant
Claude AI, developed by Anthropic

Temu Free $500 for New Users
Claim Free Temu $500 Credit via Affiliate & Influencer Program

Free TikTok Ads Credit
Master TikTok Ads for Your Business Marketing

Dall-E-OpenAI.com
Generate creative images automatically with AI

chatGPT4.win
Talk to ChatGPT by your mother language

First AI Product from Elon Musk - Grok/UN.com
Speak to Grok AI Chatbot with Your Language

Tooly.win
Open tool hub for free to use by any one for every one with hundreds of tools

GateIO.gomymobi.com
Free Airdrops to Claim, Share Up to $150,000 per Project

iPhoneKer.com
Save up to 630$ when buy new iPhone 16

Buy Tesla Optimus Robot
Order Your Tesla Bot: Optimus Gen 2 Robot Today for less than $20k

JavaScript by day


Google Safe Browsing McAfee SiteAdvisor Norton SafeWeb Dr.Web