Start creating your first game--craftyjs

Source: Internet
Author: User

Original link: http://craftyjs.com/getting-started/

Buildnewgames.com's author, Darren Torpey, has written a great introductory guide on how to start learning crafty.js.

http://buildnewgames.com/introduction-to-crafty/installing Crafty.js is easy, just put it in the <script> tab and run:
<HTML>  <Head></Head>  <Body>    <DivID= "Game"></Div>    <Scripttype= "Text/javascript"src= "Https://rawgithub.com/craftyjs/Crafty/release/dist/crafty-min.js"></Script>    <Script>Crafty.init ( -, -, document.getElementById ('Game')); </Script>  </Body></HTML>

A crafty.js game is made up of various entities that may have your hero, or your enemy. Here are the simplest entities you can create a presentation in the game.
CRAFTY.E (' All, DOM, Color '). attr ({x:0, y:0, w:100, h:100}). Color (' #F00 ');

As you can see, a string of strings are passed into the E () method, which are crafty.js available entity components. You can think of it as a building block. Here our entities include color blocks and are also a component of the components that make it possible to put them into the game. Below you will learn more about components. The entire code will look like this:
<HTML>  <Head></Head>  <Body>    <DivID= "Game"></Div>    <Scripttype= "Text/javascript"src= "Https://rawgithub.com/craftyjs/Crafty/release/dist/crafty-min.js"></Script>    <Script>Crafty.init ( -, -, document.getElementById ('Game')); CRAFTY.E (', DOM, Color'). attr ({x:0, y:0, W: -, H: -}). Color ('#F00'); </Script>  </Body></HTML>
After execution:

Now that we can show something on the screen, let's try to move it by using the keyboard arrow keys. May be implemented through the "Fourway" component.
CRAFTY.E (' All, DOM, Color, Fourway ')  0, y:0, w:100, h:100})  . Color (' #F00 ')  . Fourway (4);

Notice that we've added the component behind the string color. When we add a component to the accessible method like ". Fourway". The number passed to the method determines the speed of the operation, so if the number is higher, it will move more quickly.

Let's try to make it look like a platform game where entities can be affected by gravity. This can be achieved through the "Gravity" component. But when we add the gravity component now, the entity will fall because there is nothing to prevent it from falling. So we're going to add a "floor". Notice how we added a component named floor. Because it uses the "group" entity, we can select them. The gravity component can only be added to the entity you want to drop, so we don't need to add it to the floor entity.
CRAFTY.E (' Floor, I, Canvas, Color ')  0, y:250, w:250, H:10})  . Color (' green ');

We then add the red box and add the gravity component to the entity.
CRAFTY.E (' All, Canvas, Color, Fourway, Gravity ')  0, y:0, w:50, h:50})  . Color (' #F00 ')  . Fourway (4)  . Gravity (' floor ');

You may notice the parameter "floor" of the ". Gravity ()" method. This means that all entities can be prevented from falling further by the floor component.

Start creating your first game--craftyjs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.