Pre-release
Adventure.js Docs Downloads
Score: 0 Moves: 0
Tutorial explaining how to create a player character in Adventurejs. tutorial, create player character

Get Started:Create a Player

Creating a player is really pretty simple. We do it by way of Game.createAsset(), which is the same function we'll be using to create everything in the game world.

var MyGame = new adventurejs.Game( "MyGame", "MyGameDisplay" )
  .set({
    title: "Dungeon Drifter",
    author: "Dungeon Master Dan",
    description: "Welcome to the dungeon, baby!",
    version: "0.0.1",
  });
MyGame.createAsset({
  class: "Player",
  name: "Mighty Hero",
});

And that is really all there is to creating a player character! There will be plenty to customize in future tutorials, but that's all we need to get started.

You can see that we've repeated the game constructor block in the above example code. We did that to give you a sense of how we flow from game construction right into asset creation. In future examples, we'll omit prior code blocks in order to focus on the code being introduced. You can take it as writ that all games include a constructor block, a character block, and at least one room block.

#TMI About MyGame.createAsset({})

If you're new to Javascript or development in general, it's worth understanding what's going on with MyGame.createAsset({}), and specifically what's happening in this part: ({}).

As you know from Create a Game, MyGame is a reference to your new Game instance. createAsset() is a function, or method, on MyGame, so what MyGame.createAsset() is doing is calling, or executing, that method. Methods can take bits of information called parameters to work with. Sometimes those parameters are objects which can contain whole collections of information.

MyGame.createAsset({
  class: "Player",
  name: "Mighty Hero",
  is: { active: true, },
});

That's what we're doing here - we're passing a collection of properties to the createAsset method. This is how we're going to build every asset in the game world.

It may be the case that you'll want multiple player characters, Maniac Mansion-style. And you can do that! Just note that you'll need to set one player.is.active=true at a time. It's easy to create multiple players, as in this next example. (And remember, we're going to start excluding earlier code blocks from examples.)

// game constructor block...
MyGame.createAsset({
  class: "Player",
  name: "Mighty Hero",
  is: { active: true, },
});
MyGame.createAsset({
  class: "Player",
  name: "Mrs. Mighty Hero",
});
#TMI About Multiple Players

If you do create multiple player characters and set more than one player.is.active, Adventurejs will print a warning like this to the browser's developer console:

Adventurejs will choose the first active player it finds and deactivate any others.

#TMI About is:{active:true} vs is.active=true

You might have noticed where we referred to player.is.active=true which appears to be different from player:{is:{active:true,},} in this player block.

MyGame.createAsset({
  class: "Player",
  name: "Mighty Hero",
  is: { active: true, },
});

You'll see both of these methods used throughout these docs. Both of these are ways of setting / getting object properties, and the difference between them is an important concept in Javascript.

Coming back to this block, see where we've added two is: properties to this player. Properties shouldn't be listed more than once in a block like this. This example may not throw any errors, but it also may lead to broken results.

MyGame.createAsset({
  class: "Player",
  name: "Mighty Hero",
  is: { active: true, },
  is: { male: true, },
});

The proper way to do this is to only list the is: property once, and then list whatever nested properties that you want.

MyGame.createAsset({
  class: "Player",
  name: "Mighty Hero",
  is: { 
    active: true,
    male: true, 
  },
});

However, there is another valid way to set nested properties using property accessors, as in this example.

var hero = MyGame.createAsset({
  class: "Player",
  name: "Mighty Hero",
  is: { active: true, },
});
hero.is.male = true;
hero.is.human = true;

Note how we saved the new player character to a variable called hero. Later we will introduce built-in methods for accessing assets in the game world, but if you prefer, you can save asset references to your own variables.

NEXT: Create a Room