Shaking it up – Making your game’s screen shake

Hi everyone,

In this tutorial, I will be showing you my method for making a shaking screen effect in Cocod2D. This is a good effect to add some overall “feeling” to your game. It is like the vibration feature of an xbox controller. It’s not really needed, but adds ambience and “juice” to your games. It immerses the player more deeply into the game.

Here is how I go about doing it. All of the code should be written to affect a CCLayer, in my case, it is called GameWorld.h/.m

In your .h file, you will create three new variables:

@interface GameWorld : CCLayerColor {
BOOL shouldBeShaking;
int shakeLength;
int shakeIntensity;

shouldBeShaking is a boolean value that will let us know whether or not to shake the screen.
shakeLength and shakeIntensity will affect how long to shake the screen for and hard to shake the screen.

Now, in your .m file, schedule an update method like so (inside your init method!)

[self scheduleUpdate];

Next, setup that update method somewhere in your code

- (void)update:(ccTime)dt {


Before we go on, let me explain how this is going to work. Every frame, we will be checking whether or not shouldBeShaking is true. If it is true, then we will:

  1. offset the layer by some random amount
  2. decrement the shakeTime variable by one
  3. check if shakeTime is 0
  4. if it is, then set shouldBeShaking to false

Now, in code this looks like:

if (shouldBeShaking) {
float x = (arc4random() % shakeIntensity) - 0.5f; //Generate a random x coordinate
float y = (arc4random() % shakeIntensity) - 0.5f; //Do the same for the y coordinate
self.position = ccp(x,y); //Offset the layer's position by x and y
shakeLength--; //Subtract one from the length
if (shakeLength <= 0) { //Have we reached the end?
self.position = ccp(0,0); //Set us back to align with the screen
shouldBeShaking = NO; //Stop shaking
shakeLength = 0; //Reset shakeLength
shakeIntensity = 0; //Reset shakeIntensity

From there, you just need to set shouldBeShaking to true, set a length, and set an intensity, and your screen will be shaking accordingly.

I usually wrap those three steps up in a helper method to make things more organized.

Hope this tutorial helped someone.


New Programming Languages

I found this rather interesting article on InfoWorld. It goes over some new programming languages that show promise in changing the way developers need to program. It is definitely worth checking out. The article can be found here.

The ones that look the most enticing to me are Fantom, and X10. Let me know which one you think is the best.

Programming Experience and Minecraft.

We all know and love Minecraft.  From it’s randomly generated worlds, to sandbox style gameplay, there is so much that you can do.  Now, what if I told you that it could help you become a better programmer.

Yes, I know, mind blown.  But really, modding Minecraft is a great way to become better at programming.  It lets you add things to an already built game, eliminating the need to write boilerplate code (love it already).  Minecraft is written in Java, which has a similar syntax to C/C++.

I have been messing around in the Minecraft source code (from MCP) for a little bit now, and it has taught me and clarified many things.  Know that I started modding the game with very minimal Java knowledge and experience. It definitely takes some getting used to, but is really simple once you get the hang of it.

I highly suggest you give this a try, both for fun and for experience.

1: Simple TCP Python Server

Hi everyone!  In this tutorial I will be showing you how to create a simple TCP (socket) server in the python programming language.

Lets begin:

In this tutorial, we will be using the twisted module.  If you are using a mac (like me) this should already be installed.  If you are on Windows, you may need to download it here: Twisted Web

Now, create a new document in your editor of choice (I like TextWrangler for Python) and name it

We need to import twisted, so at the top of the file put:

from twisted.internet.protocol import factory, Protocol
from twisted.internet import reactor

Now that we have everything, lets get the server up and running. Add this after the imports:

fact = Factory()
reactor.listenTCP(1234, fact)

The first line creates a new variable called fact which is an instance of Factory()

The second line is where you specify what port you want your server to be “listening” on. In this case, the port is 1234.

Finally, we tell the reactor to run. (Starts the server!)

You now have a fully functional TCP server! (Albeit you can’t really do anything with it)

Run it with the Terminal command:  python

Now that we have that, I’m going to show you how to actually do something with your server.

In between the import statements and the rest of the code, write the following:

class MyServer(Protocol):
     def connectionMade(self):
          print 'A client done diddely connected!'

This creates a subclass ,if you will, of Protocol, and  connectionMade(self): will be called every time a client connects.

Add a print statement before saying “Server started”, or something similar.

So far, your entire code should look like this:

from twisted.internet.protocol import factory, Protocol
from twisted.internet import reactor

class MyServer(Protocol):
     def connectionMade(self):
          print 'A client done diddely connected!'

fact = Factory()
reactor.listenTCP(1234, fact)
print 'Server started, listening for clients'

To test this out, run it the same way I told you earlier.  Open another terminal window and in it type:

telnet localhost 1234

If all goes well, your server window should display your client connected message!

Congratulations, you have created a very simple socket server in Python!

Please leave any comments or questions below, make sure to follow me on twitter for updates on more tutorials and tips!

Till’ then,

Tate [AllTheWay Apps]

**Read this for more defs that can be used to create a more powerful and useful server: Writing Servers

Tutorials shall go here!

Tutorials and other tips and tricks will be posted here.  Check back soon for the first tutorial on making a simple server in Python.