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 .
- Demo
- Agrandir
- Recharger
- New window
G�n�rez vos vid�os d'entreprise par l'IA avec la voix ou simplement du texte
Votre premi�re application vid�o AI GRATUITE ! Automatisez votre premi�re vid�o AI. Cr�ez votre vid�o professionnelle en 5 minutes gr�ce � l'IA. Aucun �quipement ni comp�tence en montage vid�o requis. Production vid�o sans effort pour les sp�cialistes du marketing de contenu.
Entities
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.
- Sent (0)
- Nouveau
Générez vos vidéos d'entreprise par l'IA avec la voix ou simplement du texte
chatGPTaz.com
Parlez à ChatGPT dans votre langue maternelle
AppAIVidéo
Votre première application vidéo AI GRATUITE
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 en ligne
Échangez des visages, des vidéos, des photos et des GIF instantanément avec de puissants outils d'IA - Faceswap AI Online GRATUIT
Faceswap AI en ligne
Échangez des visages, des vidéos, des photos et des GIF instantanément avec de puissants outils d'IA - Faceswap AI Online GRATUIT
Faceswap AI en ligne
Échangez des visages, des vidéos, des photos et des GIF instantanément avec de puissants outils d'IA - Faceswap AI Online GRATUIT
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 gratuit 500 $ pour les nouveaux utilisateurs
Claim Free Temu $500 Credit via Affiliate & Influencer Program
Crédits publicitaires TikTok gratuits
Maîtrisez les publicités TikTok pour le marketing de votre entreprise
Dall-E-OpenAI.com
Générez automatiquement des images créatives avec l'IA
chatGPT4.win
Parlez à ChatGPT dans votre langue maternelle
Premier produit d'intelligence artificielle d'Elon Musk - Grok/UN.com
Parlez au chatbot Grok AI dans votre langue
Outily.win
Centre d'outils ouvert et gratuit, utilisable par tous et pour tous, avec des centaines d'outils
GateIO.gomymobi.com
Airdrops gratuits à réclamer et à partager jusqu'à 150 000 $ par projet
iPhoneKer.com
Économisez jusqu'à 630 $ à l'achat d'un nouvel iPhone 16
Acheter le robot Tesla Optimus
Commandez votre robot Tesla Bot : Optimus Gen 2 dès aujourd'hui pour moins de 20 000 $