Wednesday, June 27, 2012

Creating “Flux”: A Simple Flash Game With a Gravity Mechanic

In this tutorial, I’ll explain the major steps and workflow for creating a simple space survival game, based on the gravity mechanic explained in a previous tutorial. This game is written in AS3 using FlashDevelop.

Play the Game

Use the left and right arrow keys to manoeuvre your ship, the up and down arrow keys to increase or reduce the size of the magnetic field it produces, and the space bar to reverse the polarity. Collect the white crystals to increase your fuel supply – but avoid the red ones, because they use it up. Don’t hit a rock, or it’s game over!
In this tutorial, we won’t actually create the full game displayed above; we’ll just get started on it, by making a very simple version with primitive graphics and just one type of object. However, by the end, you should have learned enough to be able to add the other features yourself!
The game itself is very simple in its current state – take a look at this critique for tips on how you can take it from a simple demo to a full game!

Let’s Get Started!

Set up a new AS3 project in FlashDevelop, and set its dimensions to 550x600px.
package
{
 [SWF(width = "550", height = "600")]

 public class Main extends Sprite
 {

 }
}

Step 1: Identifying the Game Objects

There are six objects in particle that you can identify from playing the game above:
  • Energy supply – represented by an white oval shape object
  • Asteroid – represented by a rock-like object
  • Energy consumer – represented by a red star bounded with green light.
  • Stars – the background
  • Range indicator – represented by a white circle
  • Ship – player object
Of course you can add in any other object to make the game more interactive or add a new feature. For this tutorial we’ll just make

Step 2: The Energy Class

From the objects we identified, four of them actually work in exactly the same way: by falling from top to bottom.
They are:
  • Stars
  • Energy supply
  • Energy consumer
  • Asteroid
In this tutorial, we’re only going to make the “energy supply” objects, out of the four above. So let’s begin by creating these objects and making them fall down, with a random spawning position and speed.
Start by creating an Energy class:
package
 {
  import flash.display.MovieClip;
  import flash.events.Event;

  public class Energy extends MovieClip
  {
   private var rSpeed:Number = 0;

   public function Energy(speed:Number)
   {
    graphics.beginFill(0x321312);
    graphics.drawCircle(0, 0 , 8);

    rSpeed = speed;
   }

   // we will call this every frame
   public function move():void
   {
    this.y += rSpeed;
    //rotation speed is linked to moving speed
    this.rotation += rSpeed / 8;
   }
  }
 }

Step 3: The GameScreen Class

This class will eventually control most of the aspects of our game, including the player movement and the game loop.
Create the class:
package
{

 public class GameScreen extends MovieClip
 {

  public function GameScreen()
  {

  }
 }
}
That’s all we need for now.

Step 4: Update The Main Class

We’ll now create an instance of GameScreen within Main:
Read more: Creating “Flux”: A Simple Flash Game With a Gravity Mechanic

Sunday, June 24, 2012

Getting Started With Unity – Finishing Our Game With a Menu

There are several methods used to produce menus within Unity, the main two being the built in GUI system and using GameObjects with Colliders that respond to interactions with the mouse. Unity’s GUI system can be tricky to work with so we’re going to use the GameObject approach which I think is also a bit more fun for what we’re trying to achieve here.

Final Result Preview

Please view the full post to see the Unity content.

Step 1: Determine Your Game Resolution

Before designing a menu you should always determine what resolution you are going to serve it to. Open the Player settings via the top menu, Edit > Project Settings > Player and enter your default screen width and height values into the inspector. I chose to leave mine as the default 600x450px as shown below.
unity tutorial menus UI
You then need to adjust the size of your Game view from the default "Free Aspect" to "Web (600 x 450)", else you could be positioning your menu items off the screen.
unity tutorial menus UI

Step 2: Choosing a Menu Background

As you will have seen in the preview, our menu scene is going to have our game environment in the background so that when you click Play you enter seamlessly into the game. To do this you need to position your player somewhere in the scene where you like the background and round up the Y rotation value. This is so it’s easier to remember and to replicate later, so that the transition can be seamless from the menu into the game.
unity tutorial menus UI
Let’s now get on with the creation of the menu scene!

Step 3: Creating the Menu Scene

Make sure your scene is saved and is called "game" – you’ll see why later. Select the game scene within the Project view and duplicate it using Ctrl/Command + D, then rename the copy to "menu" and double-click it to open it. Note: You can confirm which scene is open by checking the top of the screen, it should say something like "Unity – menu.unity – ProjectName – Web Player / Stand Alone". You don’t want to start deleting parts accidently from your game scene! Now select and delete the GUI and PickupSpawnPoints GameObjects from the Hierarchy. Expand the "Player" and drag the "Main Camera" so that it’s no longer a child of the Player, then delete the Player.
unity tutorial menus UI
Next, select the terrain and remove the Terrain Collider Component by right-clicking and selecting Remove Component. Finally, select the "Main Camera" and remove the MouseLook Component by right-clicking and selecting Remove Component. If you run the game now nothing should happen and you shouldn’t be able to move at all. If you can move or rotate then redo the above steps.

Step 4: Adjusting the Build Settings

Currently when you build or play your game the only level included within that build is the "game" scene. This means that the menu scene will never appear. So that we can test our menu, we’ll adjust the build settings now. From the top menu select File > Build Settings and drag the menu scene from your Project View into the Build Settings’ "Scenes In Build" list as shown below
unity tutorial menus UI
(Make sure you rearrange the scenes to put "menu.unity" at the top, so that it’s the scene that’s loaded first when the game is played.) Perfect![...]
Read more: Getting Started With Unity – Finishing Our Game With a Menu

Wednesday, June 20, 2012

The Math and ActionScript of Curves: Gradients and Normals

This entry is part 3 of 3 in the series The Math and ActionScript of Curves
We’ve tackled drawing curves, and finding their quadratic and cubic roots, as well as handy applications for using quadratic roots within games. Now, as promised, we’ll look at applications for finding cubic roots, as well as curves’ gradients and normals, like making objects bounce off curved surfaces. Let’s go!


Example

Let’s take a look one practical use of this math:
In this demo, the “ship” bounces off the edges of the SWF and the curve. The yellow dot represents the closest point to the ship that lies on the curve. You can adjust the shape of the curve by dragging the red dots, and adjust the movement of the ship using the arrow keys.

Step 1: Shortest Distance to a Curve

Let’s consider the scenario where a point is located near a quadratic curve. How do you calculate the shortest distance between the point and the curve?
point to quadratic distance
Well, let’s start with Pythagoras’s Theorem.
Read more: The Math and ActionScript of Curves: Gradients and Normals

Adobe Shadow Labs Release 3

Last week the Shadow team hit another milestone: Release number 3. You can go to Adobe Labs and get the latest version and to the application stores (iTunes Store, Google Play, Android Market, and Chrome Web Store). It is important to remember that in order to get your app updated you have to update all the parts (the desktop app, the Chrome extension, and the iOS/Android apps). If you are new to Shadow, then let me tell you what it is. Shadow help you with testing your mobile websites or web pages on iOS and Android devices. How? On short it saves you time. Lot of time. You can enjoy syncronized browsing (the mobile devices display automatically the same URLs you have on your desktop browser), remote web inspector session with any one of the connected mobile devices, and work with localhost URLs to name just a few. So what’s new in the latest release? Here is the list:
  • Screenshots – you can take screenshots of the current page for the connected devices
  • Cache management – you can be sure that the cache was cleared on the devices
  • SSL support – you can use unsigned SSL certificates during development
Here you can watch a video that walks you through these new features.
Read more: Adobe Shadow Labs Release 3

Saturday, June 16, 2012

The Math and ActionScript of Curves: Roots

In the first tutorial of this series, we took a look at drawing curves using equations and AS3. Now, we’re going to tackle solving those equations to find the roots of a curve – that is, the places where the curve crosses a given straight line. We can use this to predict collisions with curved surfaces, and to avoid “tunnelling” in Flash games.

Step 1: Quadratic Roots

First, time for some quick math revision. In this tutorial, we’ll just accept and apply the methods we’ll use, but interested readers can refer to Wikipedia’s page on quadratic equations for information about the mathematical deriviations.
So \(f(x)\) is a quadratic function. If (f(x)) is equivalent to 0, (x) can be obtained by this formula:
[Given f(x) = ax^2+bx+c, ] [f(x) = 0, x = frac{-b pm sqrt{b^2 - 4ac}}{2a} ]
(b^2 – 4ac\) is called the discriminant of the formula. If the discriminant is negative, the square root of the discriminant will produce imaginary roots, which we can’t plot. Conversely, if the discriminat is positive, you will have real number roots and you’ll be able to plot them onto the screen.

Step 2: Visualising Quadratic Roots

So what are roots? Well in our context they are nothing more than intersection points between the quadratic curve and a line. For example, suppose we are interested to find the intersection point(s) of the following set of equations:
(
f(x) = ax^2+bx+c
g(x) = 0
)
This is a typical scenario of looking for the intersection point(s) between a quadratic curve and the x-axis (because the x-axis is the line where y==0). Since by definition the intersection point(s) are shared by (f(x)) and (g(x)), we can conclude that (f(x) = g(x)) for the values of x that we are looking for.
Read more: The Math and ActionScript of Curves: Roots

Wednesday, June 13, 2012

Using the Kongregate API in Your Flash Games

Kongregate is one of the largest Flash game portals on the net, and has its own API that can be integrated into your games (for which Kongregate even rewards you financially). In this tutorial, I’ll show you how to implement the Kongregate API into your games, and also go into detail about what the API is capable of and why you should use it. (Note that this tutorial assumes you already have a Kongregate account; if you don’t, then please create one now.)

Final Result Preview

Let’s take a look at what the API enables us to do:
Badges
Mass Messages
High Scores
There’s another big reason to implement the API…

Step 1: Let’s Get Motivated

Before we dive into the technical aspects of implementing the Kongregate API, let’s get ourselves hyped up a little bit, and make sure that we actually want to implement it. There are many reasons to implement the API, but to most developers, nothing speaks louder than money, and there’s plenty of that involved. When you upload your game to Kongregate, you automatically earn 25% of all ad revenue generated by your game’s page. It gets better; if you implement their “Statistics & Challenges API”, you’ll receive an additional 10%! Finally, if your game is exclusive to Kongregate, or sponsored by them, you receive an additional 15%. This gives you the opportunity to earn up to 50% of the ad revenue for your game on Kongregate. If you’re wondering how much that is, check out some of my personal stats:

Step 2: Setting Up Our Work Environment

Read more: Using the Kongregate API in Your Flash Games

Monday, June 11, 2012

A sneak peek: Concurrency with ActionScript Workers

This week we just shipped Flash Player 11.3/AIR 3.3 with lots of cool features for mobile and desktop. But, we are already finalizing the last details on a brand new feature coming very soon, ActionScript Workers. Planned for the next Flash Player/AIR release, I am super excited about this feature. Here's why. The concept of concurrency is kind of the holy grail for most ActionScript developers who are working on complex projects. How many times, have you had issues with the UI locking and having to distribute computations to frames like crazy? How many times did you have to optimize your algorithms like crazy to minimize the cost of execution and reduce the risk of UI locking? Yeah, you have been there. Concurrency has always been popular in the Jira(290 votes), I remember having conversations over there with many developers following the status of this feature, I am sure some of you may think, well, better late than never. Again, the focus we have now on gaming or video, allows us to focus, so deliver valuable features like those much quicker. See all the features we introduced between Flash Player 11 and 11.3, most of the biggest requests from ActionScript developers are getting delivered and we will not stop there :) We started working on this feature last year, at that time, we were debating over how we would introduce concurrency to ActionScript developers, through a simple and safe way. We decided to go with a workers approach, cause it is a design most developers are today familiar with, which also can be API based rather than language based. We knew that we would not introduce a new keyword (for now) to leverage concurrency, so really workers made a lot of sense. How does it work? [...]
Read more: A sneak peek: Concurrency with ActionScript Workers

Saturday, June 9, 2012

State Machine for Continuous Movement: Desktop, iPad, iPhone

Heli iPadCovering the Bases
On the HTML5 side of the world, most developers/designers have been aware that when you make a site, you need to consider the different environments used to view your site. With Flash and AcitonScript 3.0, there’s a new dichotomy: Web and App. Because mobile devices like smart phones and tablets will not display Flash, it’s pretty much a decision to create a mobile app or to try and do the animation with JavaScript and related technology (e.g., jQuery). As more and more users have mobile hardware, the Web is a funny place. Apps are so much handier than any Web-based app that given the choice, I’ll use a device-based app. The reason is simple: you’re not always within range of a wifi. That’s it. No wifi; no working app. So in sticking with Flash and AS3, I am thinking in terms of the following:
  • Desktop: Standard Flash
  • Table: Mobile Flash Based App
  • Phone: Mobile Flash Based App
Having recently got an iPad 3, I’ve been able to compare the results on it with my iPhone and desktop. Basically, the tablet is right between the two in terms of performance. Using Flash Pro CS6 to develop a revised version of the Aid Game, I was able to create versions for the iPhone and iPad as well as a desktop version. You can play the desktop version on a non-mobile device and play the mobile versions on mobile table and phone devices. Click the buttons below for desktop Play or download all of the versions, including versions for Flash CS5 and Flash CS6.
Read more: State Machine for Continuous Movement: Desktop, iPad, iPhone

Run your Flash apps in Xcode iOS Simulator with AIR 3.3

Flash apps in Xcode iOS Simulator with AIR 3.3
AIR 3.3 adds a new feature to preview and debug your apps directly in iOS Simulator that is part of Xcode on Mac. This rapidly speeds up testing and preview of an app or a game especially when you use native extensions.
[Download AIR 3.3 SDK]
Simulator support is not yet part of the Flash Builder, but you can already use it via command-line:
Compile for iOS Simulator
There two new options available with adt for iOS Simulator compilation:
1) ipa-test-interpreter-simulator (for testing)
2) ipa-debug-interpreter-simulator (for debugging)
PATH_TO_AIR33SDK/bin/adt -package
-target ipa-test-interpreter-simulator
-provisioning-profile DEMO.mobileprovision
-keystore CERTIFICATE.p12
-storetype PKCS12
-storepass PASSWORD FINALAPP.ipa DESCRIPTOR.xml APP.swf


Read more: Run your Flash apps in Xcode iOS Simulator with AIR 3.3

Tuesday, June 5, 2012

Easily handle right mouse button in Flash movies

Detecting and handling right mouse clicks in Flash has always been a pain. I blogged about it four years ago in the post preventing the right mouse cheat in your Flash games where I explained a dirty trick to trigger right mouse button with As2, and guess what? Things got even worse with As3, although I found a workaround preventing players to cheat in my Flash game Stringy. Finally, the pain is over. With Flash Player 11.2, you can finally do everything you want in Flash movies, without exporting them for Air. Look at this movie: You can use left mouse button to drag boxes, and right mouse button to destroy them. Also notice, the default right click menu does not appear. Look at the source code: [...]
Read more: Easily handle right mouse button in Flash movies