Friday, November 30, 2012

How to make a Facebook social game

Hi and welcome back to my blog!
This time I’m going to talk about the process of making a very basic social game as a Facebook app.
It’s my hope that if you know how to make a Flash game and have a little bit of PHP/database experience by the end of the article you will be set to make your first Facebook app.
This is a technical article covering the implementation of some of the core social game subsystems and not intended to be a discussion about moral justification of social game mechanics or even about how to design a social game – I’ll leave those aspects to game-designers and the media :)
The game I’m going to be making is called ‘Pet My Kitty’.

Click to play the live version of the game on Facebook

Pet My Kitty

In a nod to the now defunct mock-social game, Cow Clicker Pet My Kitty will have an absolute minimal set of functionality necessary to operate as a social game:
  • Interactive mechanic
  • Database persistence
  • Ability to purchase an in-game item
  • Invite friends
  • Social marketing incentive
Let’s quickly review each one of these points to elaborate on why they are key to a social game.
Interactive mechanic
There has to be something interactive about a Facebook game, no matter how simple otherwise it wouldn’t classify as a game. Furthermore, there needs to be something visually rich and rewarding associated with each interaction, so the user understands they are doing things the right way.
Read more: How to make a Facebook social game

Tuesday, November 27, 2012

Using GDB script files with the FlasCC GDB

The Flash C++ Compiler (FlasCC) provides a complete BSD-like C/C++ development environment based on GCC that lets you compile your C/C++ code to target the Adobe® Flash® Runtime (Flash Player and AIR®). With FlasCC you can port almost any existing C/C++ code to the web, across browsers. FlasCC includes tools for building, testing, and debugging C/C++ projects, example projects with source code, and documentation. You can get FlasCC today on the http://gaming.adobe.com/technologies/flascc/ page.
Today I want to share a quick FlasCC debugging tip. The FlasCC based gdb tool is located in the FlasCC download under sdk/usr/bin/gdb (Mac) or sdk/usr/bin/gdb.exe (Win/Cygwin).
Before trying this out we need a simple C app to debug. Go ahead and create a file called test.c and put the following code inside:
C:
#include <stdio.h>
int main(int argc, char **argv)
{
    int i = 0;
    printf("Hello World %d Time\n", i++);
    printf("Hello World %d Time\n", i++);
}
And the command to compile the debug SWF with FlasCC is:
CODE:
sdk/usr/bin/gcc -g test.c -emit-swf -o test.swf
The FlasCC gdb relies on an environmental variable called FLASCC_GDB_RUNTIME, if you do not set it before running gdb you'll see the following message inside of the gdb prompt:
CODE:
(gdb) run
Starting program: hello.swf
Please set the FLASCC_GDB_RUNTIME environment variable to the path to a debugger player or browser.
The FLASCC_GDB_RUNTIME needs to be set to the standalone debug Flash Player or to a browser that has a debug Flash Player. I recommend for the first time that you try using the FlasCC gdb to download a standalone debug Flash Player from here and us it. I downloaded the Mac version and put in a folder called /Code, which means I would run this command in Ternimal before I call gdb:
CODE:
export FLASCC_GDB_RUNTIME=/Code/Flash\ Player\ Debugger.app
Lets take a look at my command line to see how to put this all together:
CODE:
renaun$ sdk/usr/bin/gcc -g test.c -emit-swf -o test.swf
renaun$ export FLASCC_GDB_RUNTIME=/Code/Flash\ Player\ Debugger.app
renaun$ sdk/usr/bin/gdb test.swf
GNU gdb (GDB) 7.3
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "--host=x86_64-apple-darwin10 --target=avm2-elf".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
(gdb) b main
No symbol table is loaded.  Use the "file" command.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (main) pending.
(gdb) run
Starting program: test.swf
0xdddddddd in ?? ()
Breakpoint 1, 0xf0000051 in main (argc=0, argv=0x200ff0) at test.c:5
5       int i = 0;
(gdb)
So I setup export to the debug Flash Player and then ran the gdb against the SWF. But once in side the gdb command prompt I still need to setup a break point. When trying to setup a break point before starting the SWF it will display another warning which needs a yes response. After responding with a yes I can then type run.
Here is the trick, by using gdb -x script.txt we can run it all in one command. Instead of having to type in my break point each time or responding yes. This is how it works, create a script.txt file and put in the following gdb commands:
CODE:
set breakpoint pending on
b main
r
The first line sets the default warning option to be yes so you dont have to worry about it not being set. The second command sets a break point at the main function. And the last command runs the gdb, telling it to start. The command to use the gdb script is below:
CODE:
renaun$ sdk/usr/bin/gdb -x script.txt test.swf
Another handy command to put in script.txt is the set as3namespace com.renaun.test command. This is needed if you create a FlasCC SWC with a custom namespace.

© %FIRST Erickson - visit the <renaun.com:flexblog text="{ ModelLocator.myThoughts }"/>


Read more: Using GDB script files with the FlasCC GDB

Monday, November 26, 2012

Quick Tip: The OOP Principle of Inheritance

This entry is part 6 of 6 in the series Beginner's Guide to OOP

We’ve come a long way in this beginner’s guide to object-oriented programming, discussing the principles of cohesion, coupling, encapsulation, and abstraction. In this final article, we’ll discuss the OOP principle of inheritance and its uses in game development. Note: Although this tutorial is written using Java, you should be able to use the same techniques and concepts in almost any game development environment.

What Is Inheritance?

Inheritance is the principle of class hierarchy. It is the ability for one object to take on the states, behaviors, and functionality of another object. A real-world example of inheritance is genetic inheritance. We all receive genes from both our parents that then define who we are. We share qualities of both our parents, and yet at the same time are different from them. Objects in OOP can do the same thing. Parent classes can have child classes (also known as superclasses and subclasses respectively) which can have the same properties of the parent class, and can define new states, behaviors, and functionality of their own. As an example, consider the following class that could be used as a parent class to different shapes:
public class Shape {
  protected int height;
  protected int width;

  public Shape(int h, int w) {
    height = h;
    width = w;
  }

  public int area() {
    return height * width;
  }

  public int getHeight() { return height; }
  public int getWidth() { return width; }
  public void setHeight(int h) { return height; }
  public void setWidth(int w) { return width; }
}
To extend this class to implement a triangle, it would look like this:
public class Triangle extends Shape {
  public Triangle(int h, int w) {
    super(h, w);
  }

  public int area() {
    return super.area() / 2;
  }
}
Triangle has all the same states and functions as Shape, but redefines the area() function to return the proper area of a Triangle (half base times height). The keyword super is used to reference the superclass and any of its states and functions. This is why we can use super() to call the constructor of the superclass and super.area() to call the area() function of the superclass. So, in this case, super.area() returns height * width. The protected keyword is the last access level modifier. It acts like the private access level modifier but also allows any subclasses to have access to the variable or function.

Why Is It Helpful?

As you can see, inheritance can greatly help reduce code redundancy between similar objects by taking what those objects have in common and putting them in one place. This also creates more maintainable code because it helps to comply with the principle of DRY and to prevent the ripple effect in code changes. If all of this seems familiar, it’s probably because abstraction had very similar benefits (as well as most of the other principles of OOP). Abstraction is closely related to inheritance as an abstracted class can be used as a superclass to create subclasses. The only difference between an abstract class and a normal class is that an abstract class cannot be used to create an object.

How to Apply It

Lets go back to our three games one more time to describe how to apply inheritance.

Asteroids

Recall that we defined an abstract class for moving objects across a screen. Also recall that we defined a Ship class for the ship object. To apply inheritance to Asteroids, we can have the Ship class extend the Movable class as follows:
/**
 * The Ship Class
 */
public class Ship extends Movable {
  /**
   * Function to rotate the ship
   */
  public void rotate() {
    // Code that turns the ship
  }

  /**
   * Function to fire
   */
  public void fire() {
    // Code to fire
  }
}
The code needed to move the ship is taken care of in the Movable abstract class, so we can remove it from the Ship class. All the other objects for Asteroids could also inherit from the Movable class, making it extremely easy to change how to move an object. One thing to note about inheritance is the ability to have multiple inheritance, or the ability of one class to inherit from multiple classes at the same time. Some languages allow it, others do not. Java is one of the languages that does not allow multiple inheritance. Therefore, you couldn’t have the Ship object inherit from both a Moveable class and a Drawable class. Make sure you are familiar with what your programming language allows before you try to design inheritance into your game.

Tetris

Inheritance can be applied to Tetris by having the Tetrimino and all of the game visuals inherit from the Drawable class, which we defined in the last article.

Pac-Man

Recall that for Pac-Man we identified thee objects: Pac-Man, a Ghost, and a pac-dot. Throughout this series we’ve only discussed these three objects and have put off mentioning anything about the last critical piece of Pac-Man: the power pellet. With inheritance, we are now ready to talk about it. A power pellet is a special pac-dot that allows Pac-Man to eat ghosts. Its states and behaviors are exactly the same as a pac-dot, with really the only difference being its size and the ability to blink (remember that to keep the game loosely coupled, we want another class to monitor when a power pellet is eaten and active the changeState() method of the ghosts). This is when inheritance comes in handy. Since a pac-dot and power pellet are practically the same object, we can create a PowerPellet class which extends the PacDot class. The PowerPellet class would just need to modify a few states to make it bigger and add the behavior of growing and shrinking to create a blinking effect. And that’s it – we now have a power pellet with little extra work. Not too shabby. The code for how this would look could be as follows:
Read more: Quick Tip: The OOP Principle of Inheritance

Tuesday, November 20, 2012

How to make a multi-player game – part 2

Hello and welcome back to my blog! This is part 2 in the series where I talk about making a multi-player game.
Last time we built a TCP socket server in node.js and we’re able to send and receive complex types. Read the first article if you’ve not already done so here.
Here is a live version of the game I’m describing in this series:

Clock synchronisation

It’s important that both the client and server’s clocks are synchronised because if there is any time based interpolation, you want both server and client to agree on what time it is and therefore at what position your interpolated object is.

Asteroids on an interpolation orbit
I use this technique in 2D Space MMO to ensure the orbiting asteroids are in the same position across all clients and on the server. The asteroids are actually interpolating on an orbit around a central location – there are no update messages getting sent to correct their positions, the only thing which keeps them synchronised is having the same time value across all clients and server.
Before we can try to synchronise clocks, we need to be sure we’re using the same concept of time on both server and client. We want to calculate the number of seconds since 1970/01/01, which is the same in both javascript and actionscript:
Read more: How to make a multi-player game – part 2

Monday, November 19, 2012

Quick Tip: The OOP Principle of Abstraction

This entry is part 5 of 5 in the series Beginner's Guide to OOP
We’re almost done with this series on object-oriented programming, and in this article we’ll discuss the OOP principle of abstraction – that is, generalising an object – and its use in game development.
Note: Although this tutorial is written using Java, you should be able to use the same techniques and concepts in almost any game development environment.

What is Abstraction?

Abstraction is the principle of generalization. This requires that we move from a specific instance to a more generalized concept by thinking about the most basic information and function of an object.
This may sound a bit strange, but we are already familiar with the concept of abstraction. For example, if I say the word “car”, what do you think of? Odds are we weren’t thinking about the same car. I was thinking about a black Mustang Boss 302, which is a specific instance of a car. Neither of us were wrong because the word car is a very general concept of a vehicle that we use for transportation (or recreation in my case).
The same goes for video games. Video games are categorized into groups such as RTS, RPG, Racing, etc.. These groups are all generalized concepts that describe the gameplay of a game. StarCraft II, Elder Scrolls V: Skyrim, and Need for Speed are all specific instances of these generalized concepts.
Thus, abstraction takes many specific instances of objects and extracts their common information and functions to create a single generalized concept that can be used to describe all the specific instances as one.

Why is it Helpful?

Abstraction is helpful because it strips everything down to its most basic principles. This can help when encapsulating functionality of an object because it can help identify the important information that should be made visible and the unimportant information which can be made hidden.
Abstraction also helps with the Don’t Repeat Yourself principle. By taking what a group of objects have in common and abstracting it, we can help prevent redundant code in each object which in turn creates more maintainable code.

How to Apply This Principle

As before, let’s use our three games to see some concrete examples of this principle in action.

Asteroids

[...]
Read more: Quick Tip: The OOP Principle of Abstraction

Thursday, November 15, 2012

Quick Tip: The OOP Principle of Encapsulation

This entry is part 4 of 4 in the series Beginner's Guide to OOP
We’ve discussed object-oriented programming for game developers in general and the specific OOP principles of cohesion and coupling. Now let’s take a look at encapsulation and how it helps to keep code loosely coupled and more maintainable. Note: Although this Quick Tip is explained using Java, you should be able to use the same techniques and concepts in almost any game development environment.

What Is Encapsulation?

Encapsulation is the principle of information hiding. That is, the implementation (the internal workings) of an object is hidden from the rest of the program. A popular example you’ll hear for encapsulation is driving a car. Do you need to know exactly how every aspect of a car works (engine, carburettor, alternator, and so on)? No – you need to know how to use the steering wheel, brakes, accelerator, and so on. Another example is searching for a value in an array. In Java, you can do the following:
int myArray[] = {1, 2, 3, 5, 7, 9, 11, 13};
Arrays.asList(myArray).contains(11);
The above code will return true if the value 11 is in myArray, otherwise it will return false. How does the contains() method work? Which searching technique does it use? Does it pre-sort the array before searching? The answer is it doesn’t matter because the exact implementation of the method is hidden.

Why Is Encapsulation Helpful?

Encapsulation helps to create code that is loosely coupled. Because the details are hidden, it reduces the ability of other objects to directly modify an object’s state and behavior. It also greatly helps when you must change the data type of a variable. Lets say you decided to use a String to keep track of time in “hh:mm:ss” format. After awhile, you come to realize that an int representing seconds might be a better data type for time. Not only must you change the data type in the object, but also every time you referenced the object’s time in the entire program! Instead, you can use what are known as getter and setter functions. Getters and setters are usually small functions that return and set a variable respectively. A getter function to get the time would look as follows:
public String getTime() {
  return time;
}
The getter will return a String value: the variable time. Now when we want to change time to an int, instead of changing all calls to the getter we can just change the getter function to change the int data type into a String data type.
Read more: Quick Tip: The OOP Principle of Encapsulation

Thursday, November 8, 2012

Quick Tip: The OOP Principle of Coupling

This entry is part 3 of 3 in the series Beginner's Guide to OOP So far in this series, we’ve discussed object-oriented programming in general, and the OOP principle of cohesion. In this article, we’ll look at the principle of coupling and how it helps in game development. Note: Although this tutorial is written using Java, you should be able to use the same techniques and concepts in almost any game development environment.

What Is Coupling?

Coupling is the principle of “separation of concerns”. This means that one object doesn’t directly change or modify the state or behavior of another object. Coupling looks at the relationship between objects and how closely connected they are. A Relations Diagram is a great way to visualise the connections between objects. In such a diagram, boxes represent objects and arrows represent a connection between two objects where one object can directly affect another object.
A relations diagram A relations diagram
A good example of coupling is HTML and CSS. Before CSS, HTML was used for both markup and presentation. This created bloated code that was hard to change and difficult to maintain. With the advent of CSS, HTML became used just for markup, and CSS took over for presentation. This made the code fairly clean and easily changeable. The concerns of presentation and markup were separated.

Why Is Coupling Helpful?

Objects that are independent from one another and do not directly modify the state of other objects are said to be loosely coupled. Loose coupling lets the code be more flexible, more changeable, and easier to work with.
A loosely coupled system A loosely coupled system
Objects that rely on other objects and can modify the states of other objects are said to be tightly coupled. Tight coupling creates situations where modifying the code of one object also requires changing the code of other objects (also known as a ripple effect). Tightly coupled code is also harder to reuse because it can’t be separated.
A tightly coupled system A tightly coupled system
A common phrase you’ll hear is “strive for low coupling and high cohesion“. This phrase is a helpful reminder that we should strive for code that separates tasks and doesn’t rely heavily on each other. Thus, low (or loose) coupling is generally good, while high (or tight) coupling is generally bad.

How to Apply It

Asteroids

First, lets look at the objects of Asteroids and how they are connected. Recall that the objects are a ship, an asteroid, a flying saucer, and a bullet. How are these objects related or connected to each other? In Asteroids, a ship can fire a bullet, a bullet can hit an asteroid and a flying saucer, and an asteroid and a flying saucer can hit the ship. Our relations diagram then looks as follows:
The Asteroids relations diagram
As you can see the objects are all pretty well interrelated. Because of this, we have to be careful of how we write the code, otherwise we will end up with a tightly coupled system. Lets take for example the ship firing a bullet. If the ship were to create a bullet object, keep track of its position, and then modify the asteroid when the bullet hits, our system would be very tightly coupled. Instead, the ship should create a bullet object, but not worry about it after that point. Another class would be responsible for keeping track of the bullet’s position as well as what happens when a bullet hits an asteroid. With an intermediary class in between our relationships, the diagram would look as follows:
Read more: Quick Tip: The OOP Principle of Coupling