google+javascriptbanktwitter@js_bankfacebook@jsbankrss@jsbank






Faire simple RPG avec JavaScript et Crafty Vous ?tes en avoir marre avec les jeux JavaScript et essayer de trouver comment construire des jeux bas?s sur le Web JavaScript ?
Cadre de jeu JavaScript - Crafty - de cr?er un simple JavaScript RPG .


Étiquette: simple jeu de RPG, JavaScript et Crafty, JavaScript RPG jeu, Cadre de jeu JavaScript, Rus?, gameplay, caract?re

Gratuit iPage hébergement Web pour la première année MOMENT



Si vous êtes toujours à la recherche d'un fournisseur d'hébergement Web fiable avec des tarifs abordables, pourquoi vous ne prenez pas un peu de temps pour essayer iPage, seulement avec $1.89/month, inclus $500+ Crédits supplémentaires gratuites pour le paiement de 24 mois ($45)?

Plus de 1.000.000 de clients + existisng peuvent pas avoir tort, vraiment vous n'êtes pas aussi! Plus important encore, lorsque vous enregistrez l'hébergement web à iPage grâce à notre lien, nous allons être heureux de renvoyer un plein remboursement. C'est génial! Vous devriez essayer iPage hébergement web GRATUITEMENT maintenant! Et contactez-nous pour tout ce que vous devez savoir sur iPage.
Essayez iPage GRATUIT première année MOMENT

Entities

view the demo | download demo

Let’s create the player entity already! The source code is getting quite large so I will just show the code from the main scene.

Crafty.scene("main", function() {

  generateWorld();

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

    .attr({x: 160, y: 144, z: 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);

});

We call the generateWorld() function from earlier and create a player entity with some premade components: animate, controls and collision. Animate is a component to create animations for sprites. Similar to Crafty.scene(), you add an animation and play it with the same method with different arguments. The first argument is the name of the animation, the x position in the sprite map, y position in the sprite map and then the x position of the last frame in the sprite map (assuming the sprites all have the same y; if they don’t pass an array of arrays similar to the Crafty.sprite() method).

The controls component transforms keyboard input into Crafty events. Use .bind() to listen to an event. The events triggered in the controls component are keyup</codE> and <code>keydown. The collision component is a very basic method of calling a function if an entity intersects another entity with a specific component (this is where the labels come in handy such as wall_left, wall_right, etc.).

Note: The .attr() method is used to modify properties of the entity. In this case we position the player in the middle of the screen.

Components

It’s about time we really utilise the Entity Component system and create our first component. The component we need right now is something to control movement. There already exists two components for movement (twoway and fourway) but we want finer control and don’t want diagonal movement.

To create a component use the function Crafty.c(), where the first argument is the name of the component and the second is an object with properties and functions. To have a function execute as soon as it is added to an entity, create a function called init. If you need more information before initialising, best practice is to create a function with the same name as the component (commonly known as a constructor).

Crafty.scene("main", function() {
  generateWorld();
  Crafty.c('CustomControls', {

    __move: {left: false, right: false, up: false, down: false},    
    _speed: 3,

    CustomControls: function(speed) {
      if (speed) this._speed = speed;

      var move = this.__move;

      this.bind('enterframe', function() {

        // Move the player in a direction depending on the booleans
        // Only move the player in one direction at a time (up/down/left/right)
        if (move.right) this.x += this._speed; 
        else if (move.left) this.x -= this._speed; 
        else if (move.up) this.y -= this._speed;

        else if (move.down) this.y += this._speed;

      }).bind('keydown', function(e) {
        // Default movement booleans to false

        move.right = move.left = move.down = move.up = false;

        // If keys are down, set the direction
        if (e.keyCode === Crafty.keys.RA) move.right = true;

        if (e.keyCode === Crafty.keys.LA) move.left = true;

        if (e.keyCode === Crafty.keys.UA) move.up = true;

        if (e.keyCode === Crafty.keys.DA) move.down = true;

        this.preventTypeaheadFind(e);
      }).bind('keyup', function(e) {

        // If key is released, stop moving
        if (e.keyCode === Crafty.keys.RA) move.right = false;

        if (e.keyCode === Crafty.keys.LA) move.left = false;

        if (e.keyCode === Crafty.keys.UA) move.up = false;

        if (e.keyCode === Crafty.keys.DA) move.down = false;

        this.preventTypeaheadFind(e);
      });

      return this;

    }
  });

  // 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);
});

Our component has two properties: __move and _speed. The first is an object of booleans used to indicate which direction the player should be moving. The second is how many pixels the character should move by or speed. We then just have one function, the constructor. We could easily just use an init method here and assume a speed of 3, but we want a speed of 1 so a constructor is needed to indicate that.

We use the .bind() method a fair bit in this component. The enterframe event is called on every frame (depending on the FPS) so when the callback is triggered, it will move the player in a direction depending on which direction is true and by the amount/speed we previously decided.

The other two events, keydown and keyup, simply check which key has been pressed (derived from the event object passed as an argument) and then set the movement boolean. There is a reason why we don’t simply move the player as soon as the key is down. The keydown event will trigger once then have a short pause before calling it over and over until a key is up. We don’t want that pause so we use the enterframe event to continuously move the player. The keyup callback does the same as keydown but in reverse, sets the movement booleans to false if the key has been released.

You will also notice our player entity has our new component in the component list as well as calling the constructor. Our player should be able to move now.

Note: Using an underscore before property or function names is the convention we’re using to convey that it is private.

JavaScript par jour


Google Safe Browsing McAfee SiteAdvisor Norton SafeWeb Dr.Web