Showing posts with label Flixel. Show all posts
Showing posts with label Flixel. Show all posts

Wednesday, February 23, 2011

Welcome stencyl!

Hmm.. I notice I haven't been errr... talking on this blog a lot. Seems more Game Releases this few months than actual blogging.... anyway....

Lets give a good hand for stencyl. (Not sure how long has it been around but I just heard about it... wait... it says 2011 at the bottom is the page). Tried to copy the logo but it aint happening cause I can't see it in the html code... arggg What's stencyl and what does it have to do with game programming?

Well err.. you can read about it at their site or just read below...

  • It helps you make games... flash game.. currently

  • It's actually a packaged Flixel with a very useful and easy user interface to handle game logics... (that's what it looks like in their screen shot... not sure if it's true... yet)

  • Still blurr? Let me try again....

  • It lets you create flash game without coding? No?

  • How about it helps non programmers to make flash game!


Ok.. I am a bit blurr about this until they actually approve me to join their BETA but meanwhile you can try out their games created by their approved (envied) BETA testers.

PS:Ouch... I noticed my link to the Miniportriats 2 game actually pointed to Miniportraits 1... aww... fixed it :D

Friday, August 20, 2010

Keyboards with Flixel.. my moving ship.

Okay.. we will be continuing from the previous tutorial of clowness clownship. Now where were we? Ahh.. lets see.... we have the ship on screen (Better read the previous tutorial if you are a bit on the blurr side.... Previous Tutorial)

On this tutorial what we are gonna achieve is :

- Import bitmap into the game Done
- Add the imported bitmap as a sprite to the game Done
- Extend the clownship class (Adds more functionality to the Clownship class)
- Taking care of keyboard presses
- Introduction to movement
- Checking for boundaries so that our ship does not go beyond the viewable area

Meanwhile flash has moved to CS6 ... nah not yet as far as I know... still stuck with CS5. Anyway we are using Flash Develop so the only concern is the Flex SDK ... really... no really... really really...

Back to the codes... your directory should look as below.



Note the 3 files...

1. ClownShip.as
2. LoadMeShip.as
3. Main.as

The Main.as sets up the entire flash player and calls LoadMeShip.as.
LoadMeShip.as then creates a new ClownShip instance called cship.

Below is the code for Main.as


package

{
import org.flixel.*;

public class ClownShip extends FlxSprite
{

[Embed(source = "data/clownship.png")] private var ImgShip:Class;

public function ClownShip()
{
loadGraphic(ImgShip);
}

}

}


Yet again the code for LoadMeShip.as


package
{
import org.flixel.FlxState;

public class LoadMeShip extends FlxState
{
private var cShip:ClownShip;

public function LoadMeShip():void
{

cShip = new ClownShip(100, 100);
add(cShip);
}
}
}


Wait a minute... it's not the same as the previous example... what's with the cShip = new ClownShip(100,100);
It was cShip = new ClownShip(); before.

Well my furry friend, let me introduce you to these things called parameters... what we are doing here is passing in 100 and 100 again as parameter numero uno no 1 and no 2 to the ClownShip. This also means that our previous ClownShip.as has to be modified to accept and use those parameters.

It's like when you get an aiskrim icecream for free you don't pass any money to the seller. The seller don't even need to count the money... but when you purchase the icecream with money, the seller has to have the ability to count the money.... whatever... what kind of sick example is this?

The entry point (constructor) to the ClownShip.as class is the function which has the similar name to the class name.
Previously on X-Files we have ...


public function ClownShip()
{
loadGraphic(ImgShip);
}


as the entry point (constructor... get used to the term constructor cause this is widely used by people that talks funny...)

To make it able to accept parameters we need to add it in the function declaration...


public function ClownShip(x:int)
{
loadGraphic(ImgShip);
}


Note the x:int. This means accepting 1 parameter named x of type integer (32-bit signed integer). Yes you need to declare what kind of parameter this bozo constructor is accepting. But wait... there are 2 of them.. 2 jedi knights? Now there are 2 of them!
So us having a higher plain of intelligence will quickly deduced that the constructor should look like this...


public function ClownShip(x:int,y:int)
{
loadGraphic(ImgShip);
}


Get it? I do hope you do. Now loadGraphic has been explained about a million times in the previous tutorial so I am not gonna go into that.

Ok now you can accept 2 parameters into the constructor, now what? The 2 parameters from cShip = new ClownShip(100, 100); is actually the X and Y coordinates where we want the ClownShip to appear, so within the ClownShip main function we call "super(x, y);". It should now look like this...


public function ClownShip(x:int,y:int)
{
super(x, y);
loadGraphic(ImgShip);
}


Wha? super? What the? Ok I think we need some words about the "super" function.
Here goes...

Long ago in a galaxy far far away.. the hope of freedom ..

Now which class was ClownShip extended (derived) from? Any answers? Yes.. the FlxSprite class... (from the "public class ClownShip extends FlxSprite") so whenever we call "superman", it simply means that we are calling the superclass's constructor...
...
..
.
which in this case is the constructor function of FlxSprite, which is

"public function FlxSprite(X:Number = 0, Y:Number = 0, SimpleGraphic:Class = null)" from the FlxSprite.as file.

No don't look into the FlxSprite.as... just look into the documentation that comes with Flixel. From there you will also find out that the first and second parameter passed into the super corresponded with the X and Y coordinate of the created instance.

Okay so now "super" simply means referring to the parents (extended from) object. You can even call the parents object's function / method ... lets say you wanna call the "update" function from FlxSprite.. you can simply call "super.update();" which you are gonna use in the near future. :D

So I hope you pretty much get the idea of "super". While "duper" simply means calling the grand...

Now we are going to add a function which Flixel calls each game loop, the ultimate most important function of them all the "update" function. Now if you look into the FlxSprite there is already an update function which has been inherited into the ClownShip.as class so why do we need to add it somemore?

You are right.. it has already been inherited but then we wanna modified that update function to add in listeners for keyboard strokes so we want to override it's parents update function. How are we gonna do this? Well we use override...


override public function update():void {
}


What this means now is whenever Flixel calls the update for the ClownShip... it totally ignores the one it inherited from the FlxSprite and goes straight into the overrided one above. Overrided? Is this even a word? Should it be overrode or overlord or something... all hail Starcraft! SO far it does nothing...

We need to fix this. We need to add something to it... we need the FlxG.keys.xxx


override public function update():void
{
if(FlxG.keys.LEFT)
{
x -= 3;
}
}


FlxG.keys contains the keyboard stuffs you are looking for. To test for the left arrow key presses the "FlxG.keys.LEFT" is used
So here we are telling if the LEFT key is pressed we need to minus the x coordinate of the ClosnShip by 3 pixel making it move to the left by 3 pixels.
x -= 3; is the same as x = x - 3; It was implemented long ago during the Mayan time
Now which is what key as what who where?
Simple... in Flash Develop, when you key in FlxG.keys. ->upon hitting the "." after "keys", a whole list of keys will appear for you to choose so you don't need to remember what key as what who where.

Now cater for RIGHT, DOWN and UP...


override public function update():void {
if(FlxG.keys.LEFT)
{
x -= 3;
}
else if(FlxG.keys.RIGHT)
{
x += 3;
}
else if(FlxG.keys.DOWN)
{
y += 3;
}
else if(FlxG.keys.UP)
{
y -= 3;
}
}


Now run the game... tadaaa... you have keyboard control of the ship :D Yeah it goes beyond the viewable boundaries so we add...


if(x > FlxG.width-width-4)
x = FlxG.width-width-4; //Checking and setting the right side boundary
if(x < 4)
x = 4;

if(y > FlxG.height-height-4)
y = FlxG.height-height-4; //Checking and setting the right side boundary
if(y < 4)
y = 4;


This should stop it from going beyond the boundaries with a 4 megapixel margin :D

The final ClownShip.as should look like this..


package
{
import org.flixel.*;

public class ClownShip extends FlxSprite
{
[Embed(source = "data/clownship.png")] private var ImgShip:Class;


public function ClownShip(x:int)
{
super(x, y);
loadGraphic(ImgShip);
}
override public function update():void {

if(FlxG.keys.LEFT)
{
x -= 3;
}
else if(FlxG.keys.RIGHT)
{
x += 3;
}
else if(FlxG.keys.DOWN)
{
y += 3;
}
else if(FlxG.keys.UP)
{
y -= 3;
}

if(x > FlxG.width-width-4)
x = FlxG.width-width-4; //Checking and setting the right side boundary
if(x < 4)
x = 4;
if(y > FlxG.height-height-4)
y = FlxG.height-height-4; //Checking and setting the right side boundary
if(y < 4)
y = 4;
}
}


You should now have some idea about "super" and the all important "update" function which is called each game loop.
Also important is the concept of parent and child function overriding :D

The end product should look like this ...



That's about it this time :D This is turning out to be an introdution and understanding of classes and stuffs rather than game programming... hmm... well next time we are going to look into Flixel specific variables like Speed, Acceleration, Drag and how to handle multi key presses which you already know :D from the code above hehehe

Updates on Flixel...

Hmm somehow Flixel has stopped with updates since version v2.43 which is like 65 days ago.. while on the other hand FlashPunk is showing constant updates up till v1.4 on August 11.

Hope ChevyRay is doing well as we eagerly awaits more updates to Flixel.

Sunday, June 27, 2010

Of bitmaps and sprites in Flixel

Previously on winterglass, we learn how to setup and run an empty flixel project. If it works, congrats! If it doesn't it's time to call for help... :D cause I am moving forward to the next tutorial.
What we are going to do here is to :

- Import a bitmap into the game
- Add the imported bitmap as a sprite to the game
- Eat with your nose!

Since we are not working in the Adobe Flash Pro interface we do not have the tools to draw lavishly beautiful vector sprites. We are left with the option of importing Starcraft SUV sprites from the mpg files from external file.
Importing bitmap in Flixel does not restrict you to bitmap as in bmp files but you get the variety of png, jpg and bmp to choose from each with their own unique advantages. I will be dealing with png since it's clean unlike jpegs lossy compression and has transparency :D
Yes, transparency information in png. We will need to draw a 'ClownShip' with external drawing packages like GIMP, Photoshop, CorelDraw... MS Paint.

Here is my ClownShip below!



I call it clownship.png. Cool right? I mean if you compare with the old vector ClownShip from my previous tutorials. This is a side view instead of the old top down view. You got it right, I am gonna make a left right scrolling shooter instead of the old top down. Enough about directions, since I am writing this tutorial I minus well do it nicely with a good game to end up with in the end.
You might consider buying a Wacom tablet if you are into drawings. It makes drawing sprites ....... easier.

The sprites we are importing is a 64 x 32 pixel sprite. The filename I choosed is clownship.png located at 'data' directory. Yes I created a directory called 'data' inside of 'src'



Now we are going to create a class for the clownship. Why? because creating classes for stuffs makes it easier to control / manage once the project gets big. You will need classes for the player, the enemy...
Under 'src', right click and Add->New Class. Call the new class 'ClownShip'. You will get a ClownShip.as file created for you in the 'src' directory.

Your directory should now look like this.



Now open up the file in the editor. Again you are not interested in the default items in the file. Replace them with these.


package

{

import org.flixel.*;



public class ClownShip extends FlxSprite

{

[Embed(source = "data/clownship.png")] private var ImgShip:Class;

public function ClownShip()

{

loadGraphic(ImgShip);

}

}



}



Lets go line by line again. In the previous episode, we started with classes that extends FlxGame then goes into FlxState. It simply means... We create a game base and put in a stage to the game. All game needs a stage even if it's just a title screen, a game level 1, 2, 3...
Now we need to put in a player or sprite so we extends the FlxSprite thus the public class ClownShip extends FlxSprite

[Embed(source = "data/clownship.png")] private var ImgShip:Class;
- This is the way to tell it to import a bitmap picture called clownship.png in the 'data' subdirectory and refer to it as ImgShip.

loadGraphic(ImgShip);
- Ok loadGraphic is a function inherited from the FlxSprite class. Just go ahead and open the FlxSprite.as from the 'src/org/flixel/FlxSprite.as' directory and search for loadGraphic
- Since it's in the constructor function of ClownShip, it simple loads the bitmap whenever it's created / instantiated.
- The are other parameters which can be passed into the loadGraphic function. I will go that later but if you are interested you can just view the doc that comes with flixel.

Ok now that you have created the ClownShip class, try hitting 'F5'... waaa laaaa.. nothing happens. Why? Because you didn't add it in the stage / state. Go back to LoadMeShip.as
Declare the ClownShip and add it in the state! The code should look like this.


package

{

import org.flixel.FlxState;



public class LoadMeShip extends FlxState

{

private var cShip:ClownShip;



public function LoadMeShip():void

{

cShip = new ClownShip();

add(cShip);

//trace('I am running');

}

}

}



private var cShip:ClownShip;
- You declare a ClownShip and call it cShip. You kinda need to grasp the concept here.
- You are telling it that there is a thing called cShip which is a (type) ClownShip.

cShip = new ClownShip();
- You are instantiating / creating the cShip as a new ClownShip. Basically you can create / instantiate lots of ClownShips with a different name but make sure you declare them first. This one so happends to be named cShip. Am I making sense? I am sounding weird here...

add(cShip);
- Now you are adding the cShip, which is a type ClownShip to the playing stage! By default it's located top left ( x = 0, y = 0 )
- What, where did 'add' come from? Take a guess.... yes it's from the FlxState class. To see what it does, just go to the doc under FlxState class and read on.. it tells you it needs an FlxObject to be passed in. FlxObject? Well now take a look at FlxSprite doc file. You see it says it inherits from FlxObject so making it a FlxObject... confuse?... you need to look into these docs often to get up and running on these libraries.

//trace('I am running');
- The // means I commented out this line of code. I think you all knew this already... I just needed this article to be a bit longer.

Now hit 'F5' again. ta daaa.... you have the ship up at the top and it's ready to move around. Now hit the arrow keys. Yes.... yes..... it's not moving.
You will have to wait for the next episode for that to happen! So stay tuned...

Monday, May 31, 2010

While I am away....

While I am away finishing off C&C 4, Flashpunk has gone Version 1. It was 0.8 before. Much improvement has been added includes the all mighty...

  • ability to choose frame or time based rate... framerate or timestep based game engine.

  • Various helper class for animation, collision and ...

  • basically a redesign of the classes for objects and inputs

  • powerful motion tweening stuffs

  • path based motion

  • advanced godlike AI

  • particle effects and emitters

  • we even have a more powerful sound department now... with pan, fading,... dolby and skywalker sound


Not to mentione the older pre - V1 source codes are uncompilable now with the V1 :(
No worries... a quick porting over by your local flash programmer will do the trick. not

With these new stuffs in place and comparing the activities and updates on flixel and flashpunk, I can't help but to reevaluate which one should I concerntrate on...
hmmm.... more news after the commercials

Thursday, April 15, 2010

Absolute Ultra Beginners guide to Flixel with Flash Develop

In the previous episode, we see the introduction of game libraries in flash scripting language namely Actionscript 3 or AS3. Ok I am going to make some huge assumptions here. What I am assuming are...

- You are rather well to do with AS3
- You have already looked through my previous tutorials
- You know how to setup and already installed Flash Develop
- You have already link to the Flex SDK (FREE download btw) and compiled the 'Hello World' example I previously wrote about.
- You do not wear skirts
- You are geeky

Ok.. here I am also assuming that

- You want to program a game
- You want to program a 2D game
- You want to program a 2D game with sprites
- You want to program a 2D game with sprites in AS3
- You want to go head on with Blizzard WOW

If you are looking for 3D Flash, look elsewhere or maybe look here in like a few years later. I know we already have Papervision and other stuffs but I am still trying to grab 2D.

First I will need you to download a game library... it will be Flixel.. yeah... Wha? Why not Flashpunk? Do not argue with the master programmer
Well err... currently I have already started with Flixel. Yes I lied... I started with Flashpunk but it kinda links me to Flixel
Ok the reason being that flixel have less letters to type. There!

Start an 'AS3 Project' under Flash Develop. Flash Develop will generate those default files. I call my project name Ship. What we are going to do is to :

- Add a AS3 Project
- Setup the Flixel Library
- Initialises the FlxGame and FlxState
- View the prebuilt in features

After creating a new project, you will get a Main.as default file and if you press 'F5' to test the movie, it will be a blank screen.

Now it will look like this in the Main.as.



Clear all those texts and import stuffs because we are going to use flixel and when we use flixel, flixel handles the import for us in the library itself whatever that means.


package
{
import org.flixel.*;
[SWF(width = "500", height = "350", backgroundColor = "#000000")]

public class Main extends FlxGame
{
public function Main():void
{
super(500, 350, LoadMeShip, 1);
}

}

}


Just copy whatever is in above and replace whatever which was in the Main.as with it. There... hit 'F5'. You got a game running! Yeah!
...
..
.

Sadly no... it's not that easy.

You did download the flixel library already right? Just unzip it and link to the library via the properties in the..... wait... there is a less confusing way... just copy the 'org' folder in the 'src' folder in the project.
It should look like this in the Project view.



Ok now that you have copied the items into the correct place, hit 'F5' Wha? Again? You should get an error stating some undefined properties LoadMeShip. It's ok.



Let's go line by line on the code we just put in.

import org.flixel.*;
- This simply means we are importing the flixel library located at org/flixel folder relative to the main.as and * simply means everything. So it translate to 'gimme everything from org/flixel'

[SWF(width = "500", height = "350", backgroundColor = "#000000")]
- Since we don't have a nice interface to tell how big we want the flash movie to be like in Flash Pro, we have to do it here like this. The parameters should explain themselves. Or should they?

public class Main extends FlxGame
- Why? extends? What? The class name 'Main' directly relates the the filename Main.as. When compile, the system will look for the filename as the class name so basically this is the entry point.
- extends? If you have some kind of programming background this is equivalent to inherit. I hope I am right but basically this means the Main class inherits the FlxGame class (which by the way is located in org/flixel/FlxGame.as) and is capable of extending it's functionality. Gosh correct me if I am wrong...
- If you look into FlxGame.as you will see it imports flash.display.Bitmap, Sprites and all so basically this FlxGame imports those stuffs and when you import extends FlxGame.as you kinda use those too.
- Ok why do we need to extend from FlxGame? Well this is the kinda core class to extends from when using Flixel. To actually learn more about this, you can view in the documentation that comes with the downloaded library under docs/index.html. You will need to refer to these docs VERY frequently as I have to really understand the library you are using. It helps. It really does... and if you are wondering... wow.. these documents are cool.. how do they do that and keep up with all the updates. The answer is 'You can do it too :D' In Flash Develop, look under Tools->Flash Tools->Documentation Generator... wait I am going off topic... later then.

public function Main():void
- Hmm.. how do I explain this. This is actually the constructor function of the class. A function with the same name as the class will be it's constructor, the one that executes upon initialisation of the class , much like Protoss Executor from Starcraft I... I am talking rubbish again
- This is what will run first. As a further reading for you guys, read up on destructor.

super(500, 350, LoadMeShip, 1);
- No relation to Superman, try to guess what it does?
- Although not immediately clear, the 'super' actually calls the class's superclass's constructor!
- Yes... Main being an extention of FlxGame, so Main's superclass is actually FlxGame class and FlxGame constructor is actually the FlxGame function in the FlxGame class which is in FlxGame.as file in org/flixel folder :p. Get it? I really hop you do because, there will be lots of super calling in the future and I really hope you know what it actually means.
- Note the parameter passed in and relate it to the FlxGame constructor function : FlxGame(GameSizeX:uint,GameSizeY:uint,InitialState:Class,Zoom:uint=2)
- 500 = GameSizeX:uint (Width of hte game screen)
- 350 = GameSizeY:uint (Height of the game screen)
- LoadMeShip = InitialState:Class (It will look for the LoadMeShip class, in this case, it's not found cause we have not add it yet!)
- 1 = Zoom:uint=2 (Zoom level with a default of 2)

So now that you realised the error came from the missing LoadMeShip class, all you need to do is to add that class in. Right click on the 'src' folder and Add->New Class



Call it 'LoadMeShip'. Again we don't want the default items in the class so just clear them out and replace with ...


package
{
import org.flixel.FlxState;

public class LoadMeShip extends FlxState
{

public function LoadMeShip():void
{
trace('I am running');
}
}
}


FlxState? What's that? Lets just put it as errr.... game states. You know like menu screen, level 1, level 2... so basically it's a game state we are dealing with here.
You can read more about it in the docs. Now hit() 'F5' to run the test movie. If you get the

'Warning: This compilation unit did not have a xxx specified in Frame metadata to load the configured runtime shared libraries. To compile without runtime shared libraries either set the -static-link-runtime-shared-libraries option to true or remove the -runtime-shared-libraries option.'


You should already know how to solve this (for Flex SDK 4) in my previous post. Just add

-static-link-runtime-shared-libraries=true


in the Project->Properties->Compiler Options->Additional Compiler Options
Everything should run fine now. If you are fast enough you can catch the volume control sliding up out of view. This is a default for the volume control. You have lots of other prebuilt stuffs too. Try pressing '`'. You get a console telling your framerate and other infos. If you lose focus on the flash window, your game pauses and a pause screen shows :D
So basically these are the stuffs which are handled by the Flixel game library.



You should also be able to see the 'I am running' in the output panel in Flash Develop! If not you might be using a non debug flash player (solution also explain in my previous blog entry).

Ok that's about as much I am able to write here for today. Sleeping already? Yes it's kinda boring at first but all these needs to be done for the progress to the next tutorial.