Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm working on an engine for a game, too learn javascript and just because its fun. I'm a flashdeveloper, I know how to build websites. Now making games is a different challenge, javascript is a challenge, but I'd love to learn how to structure code and what patterns are common. I dont mind if the game ever finish, I'm mostly interested in the programming part of it. I dont have a particular endresult in mind, so I'll see where it takes me.

I currently have a system where you can buy items. The items cost a specified amount of gold, silver, diamonds etc. When you have selected and bought the item, it takes time before getting rewarded. When time is over, you are getting rewarded with other properties (gold, energy, diamonds). For example, you can buy an apple for 50gold, It takes a minute, you get rewarded with 75energy. Or if you take a run, it cost 50energy, it takes 5minutes, reward is 25gold and 25silver. These definitions is what i call actions. Currently I already have a system where this already works and I can define as much actions with as much properties as I want.

The definitions I have kinda looks like this:

{id:101, category:544, onInit:{gold:-75}, onComplete:{energy:75}, time:2000, name:"Apple", locked: false}
{id:102, category:544, onInit:{gold:-135}, onComplete:{energy:145}, time:2000, name:"Banana", locked: false}
{id:106, category:302, onInit:{energy:-50, power: -25}, onComplete:{gold:100, diamonds:2}, time:10000, name:"Run", locked: false}
{id:107, category:302, onInit:{energy:-70, silver: -55}, onComplete:{gold:100}, time:10000, name:"Dance", locked: false}
{id:108, category:302, onInit:{energy:-230, power: -355}, onComplete:{gold:70, silver:70}, time:10000, name:"Fitness", locked: false}

Now, I would love to add a system where I can lock/unlock the actions using achievement rules. Lets say, if you buy 10 apples, you unlock a new action, like bananas which cost more, and reward more. In the future I maybe want to restrict achievements and actions to levels. I am kinda stuck how to structure this.

I have 2 questions:

  1. Which patterns are used to define achievements? How/where are they defined? Should it be part of the action, or should it be a separate controller? Is it a good idea to register all completed actions to it? I think I want multiple types of achievement rules, Id love to hear some ideas how to develop it.

  2. How do you create/find a good balance, so the user does not get stuck or can cheat by repeat a pattern of actions to get too much rewards. I know there is not a simple answer and i'm lacking of a good game-concept, but I wonder if anyone created such a game and how you dealed and played with it.

share|improve this question
A good read on creating balance: altdevblogaday.com/2012/02/17/… –  John McDonald Jun 29 '12 at 21:34

1 Answer 1

up vote 1 down vote accepted

Balancing is too vague a problem to be answered here, but this might work for your first question:

  • Define actionLog, an array of the form actionID => numberOfFinishedActionsOfThatType data structure to store how often the player has already completed each action.
  • Add a property called "restrictions" to your actions. Restrictions is a list, with each restriction being an array with the properties "type" and "parameters".
  • Before a player starts an action, go through all the restrictions of that action to see if they are met.

For example, the checkIfRequirementsAreMet method might look something like this:

restrictionsAreMet = true;
for each (var restriction in action.restrictions)
    switch(restriction.type) {
        // your achievement case
        case "numberOfActionsFinished":
            if(actionLog[restriction.parameters.action] < restriction.parameters.n) {
                restricitonsAreMet = false;
        // some other type of restriction
        case "level":
            if(character.level < restriction.parameters.n) {
                restricitonsAreMet = false;
return restrictionsAreMet;

Note that this is a very crude and straight-forward way of implementing achievements and restrictions. If you want to improve on this suggestion, you might want to think about some of the following options as well:

  • Define an class Restriction with a method "met()" that takes the game state as a parameter and returns either true or false. Subclass Restrictions to define specific types of restrictions, like NumberOfActionsRestriction, LevelRestriction, etc.
  • Define a class Action and raise events whenever a player finishes an action. Then define event listeners in other game components to react to these events in any way they want.
  • For example, you could define Achievement objects that listen to Action.onFinished and increase a counter property every time a relevant action was finished. You could store these Achievement objects as a list in your GameState data structure that is passed to Restrictions's met() method, so that your restrictions can access these Achievements.

Read http://www.javascriptkit.com/javatutors/oopjs.shtml or some other tutorial to get started with object-oriented programming in JS.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.