Saturday, October 22, 2011

AS2: Shop System

Source file available here: http://adf.ly/3LnLS

Welcome! Today I will be showing you how to create a shop system in your flash games with the use of ActionScript 2.0.

Let's start by creating a new AS2 Flash document.












For this tutorial, you will only have 4 things in your library. 3 of the 4 are buttons named "item1", "item2", and "item3". The other thing is also a button named "purchase_btn". You will need to create these now. Just select each item individually, and convert them to a symbol (button) by pressing F8 on your keyboard or right clicking them and selecting "Convert to Movie Clip".






















The picture above are the 3 items I created, and my purchase button. You are now done with making the library items.

Now we need to add several Dynamic text boxes. There are 7 total for this tutorial.
Here is a list of the ones you will need and their Instance Names or Variables. The ones highlighted in blue are Variable Names, and the ones in red are Instance Names.

Money - "money"
Cost of Item - "cost"
The Item's Name - "itemName"
Shop Message - "msg"
Amount of Blue Squares - "blueSquares"
Amount of Red Squares - "redSquares"
Amount of Green Squares - "greenSquares"

Here is a visual display of the textboxes:
Do not put any text inside the textboxes, I just wanted to make it clear which ones were which.


















Now that we have named our Dynamic textboxes, we can start adding code.
In the first frame, we need to determine how much money we want to start out with, and how many of
each item we have in our inventory. So in the frame's actions, type this:


money=100;
blueSquares=0;
redSquares=0;
greenSquares=0;


We have now set what we want for each value. Now we need to add coding that lets us purchase items. On the purchase button, type this:



on(release){
if(_root.itemName.text=="Blue Square" && _root.money>=10){
_root.money-=10;
_root.blueSquares+=1;
_root.msg.text="You have bought 1 Blue Square."
}else
if(_root.itemName.text=="Red Square" && _root.money>=20){
_root.money-=20;
_root.redSquares+=1;
_root.msg.text="You have bought 1 Red Square."
}else
if(_root.itemName.text=="Green Square" && _root.money>=30){
_root.money-=30;
_root.greenSquares+=1;
_root.msg.text="You have bought 1 Green Square."
}else
if(_root.itemName.text=="Please select an item."){
_root.msg.text="You have not chosen an item!"
}else{
_root.msg.text="You don't have enough money!";
}
}

Let's take a look at what this means:

if(_root.itemName.text=="Blue Square" && _root.money>=10){
_root.money-=10;
_root.blueSquares+=1;
_root.msg.text="You have bought 1 Blue Square."
}
Each of these "if" statements is checking to see if 1, we have selected the Blue Square item as indicated by the  "itemName" textbox, and 2, if we have enough money to purchase the item.
You will notice that after each "if" statement, there is an "else" action. This means that, if the function above is false, (if we do not have enough money/item not selected) then check for this function, and if this function is not true, check the next function.


if(_root.itemName.text=="Please select an item."){
_root.msg.text="You have not chosen an item!"
}else{
_root.msg.text="You don't have enough money!";
}


The "if" statement here is saying that if we have not selected an item at all, let the user know that he/she has not selected an item from the shop.
The last line is the last function that is checked, that lets the user know that they do not have enough money to purchase the item.

The last coding we need is simple, we just need to be able to click on an item and display the cost and name of it. For each item, type this:


on(release){
_root.cost=10;
_root.itemName.text="Blue Square";
}


You can change the cost and name to whatever you want, but make sure that the "itemName.text" matches the same text you put in the coding for the purchase button.



















That does it! If you need help understanding the code or anything dealt with in this tutorial, please download the source file. It will help you! Thanks for viewing this tutorial!

Source file available here: http://adf.ly/3LnLS

Monday, October 10, 2011

AS2: Mouse Controls Character Rotation

Source File Available Here: http://adf.ly/38qsi

Welcome to Tutorial #9! In this tutorial, I will teach you how to aim your character towards the mouse position.
Let's get started by opening up a new Flash AS2 document:












Now let's draw our character on the stage. Then convert it to a movie clip called "char". Next, put the registration point where you would like to have the character's rotation axis at. In this case, I'm putting mine in the center box, so that the character rotates around the center of itself:











Now let's open up that actions of the character and type:

onClipEvent (enterFrame) {
disx = _root._xmouse-_x;
disy = _root._ymouse-_y;
Radians = Math.atan2(disy, disx);
Degrees = 360*Radians/(2*Math.PI);
_rotation = Degrees;
}


1.) disx and disy are setting variable for the x and y coordinates of the mouse cursor
2.) Radians is settings the variable for the distance between disx and disy
3.) Degrees calculating the rotation angle
4.) _rotation is settings the angle to the Degrees variable


Now you are done! Test your game to see your character aim at the mouse.
This concludes Tutorial #9! Thanks for viewing and remember to subscribe to my blog for more tutorials!

Source File Available Here: http://adf.ly/38qsi

Sunday, October 2, 2011

AS2: Paypal Donate Button

Source File Available Here: http://adf.ly/30e2V
Welcome to Tutorial #8! In this tutorial I will show you how to add a Paypal option in Flash for purchasing items.

Let's start by opening up a new Flash AS2 document:












Now lets create a button that says "Paypal" on it. Convert it to a button movieclip and name it "paypal_btn".













Also, make sure you give the button an instance name of "myPurchaseButton". If you don't give it that instance name, your button will be non-functional.













Now let's open up the actions of the button by clicking on it and hitting F9 on your keyboard or right clicking it and selecting "actions". Type this:


myPurchaseButton.onRelease = function() {
formData = new LoadVars();
formData.cmd = "_xclick";
formData.business = "my-email-example@domain.com";
formData.item_name = "Donation";
formData.amount = "$10.00";
formData.send("https://www.paypal.com/cgi-bin/webscr", formData, "POST");
};


The code is pretty much self explanatory. Just change the email, item_name, and amount to your preferences and you're done! Once you click the button, it should bring you to the purchase page in Paypal, prompting you of your pre-purchase.

Thank you for viewing this tutorial! Be sure to become a member to receive notifications of all new weekly tutorials on my blog!

Source File Available Here: http://adf.ly/30e2V

AS2: Timer

Source File Available Here: http://adf.ly/30cZq

Welcome to Tutorial #7! In this tutorial, I will show you a quick an easy way to make a countdown timer for your game.

Let's start by opening up a new Flash document and choose AS2 as your coding language.












Now lets create a Dynamic text box on the stage. Give it an variable name of "timer" in the properties panel.

















Lastly, we need to open up the frame's actions. So click on the frame you are on in the timeline, and hit F9 on your keyboard or right click the frame and choose "Actions". Type this:

stop();
timer = 20;
countdown = function(){
_root.timer--;
if(_root.timer<=0){
_root.timer=0;
}
}
countdownInterval = setInterval(countdown,1000);


Lets take a look at what this means:


1.) stop(); means to stop the timeline from continuing the timeline animation.
2.) timer = 20; is setting the timer text box we made to start at 20 seconds.
3.) countdown = function(){ is setting a variable name (countdown) as a function.
4.) _root.timer--; means to constantly subtract seconds from the timer.
5.) if(_root.timer<=0){ means if the timer is less than or equal to 0 seconds, then execute the following command.
6.) _root.timer=0; means to set the timer to 0, so it won't subtract into negative numbers.
7.) countdownInterval = setInterval(countdown,1000); is setting an interval of subtracting seconds as close to a normal second as possible.

Now you're done! It should look like this:









Thanks for viewing this tutorial! Don't forget to become a member of this blog to get notifications of weekly tutorials!

Source file available here: http://adf.ly/30cZq

AS2: Character Interactions

Source File Available Here: http://adf.ly/30bZr

Welcome to Tutorial #6! In this tutorial, I will teach you how to interact with NPCs. By the end, we will have a character and 2 NPCs that will say a message when the user hits the space bar, and will exit the message when the user presses control.

Let's get started by opening up a new AS2 document in Flash:














Now lets draw our 2 or 3 characters. For me, I am doing 1 character and 2 NPCs.











Convert each character to a movie clip by selecting one and pressing F8 on your keyboard.
I called my main character "char".
I called NPC1 "computer1"
And I called NPC2 "computer2"
So now we have 3 movie clips in our library. Next, let's give our "char" and instance name of "char" by going into the properties panel (Window > Properties).








Now we need a message box to display the text when the user presses the space bar when he/she is close enough to the NPCs. So create a text box and make sure it is set to Dynamic. Notice how I gave the text box an instance name of "msg".













Now for the Action Script. Let's go ahead and open up the character's actions by clicking on its movie clip, and pressing F9. Put this:

onClipEvent(load){
speed=3;
}
onClipEvent(enterFrame){
if(Key.isDown(Key.UP)){
this._y-=speed;
}
if(Key.isDown(Key.DOWN)){
this._y+=speed;
}
if(Key.isDown(Key.LEFT)){
this._x-=speed;
this.gotoAndStop(1);
}else
if(Key.isDown(Key.RIGHT)){
this._x+=speed;
this.gotoAndStop(2);
}

}
All this is, is movement code. That is all you need in the character. Notice how on the right and left keys, the character "gotoAndStop(2);". that means if the right key is pressed the character movie clip will go to the 2nd frame of its timeline. On the second frame of the character's timeline, it is facing in the right direction. So basically gotoAndStop(2) means to face right. gotoAndStop(1) means face left.

Next, go into one of the NPCs actions. Type this:

onClipEvent(enterFrame){
distance=500;
ux = _root.char._x;
uy = _root.char._y;
ex = this._x;
ey = this._y;
if(Math.sqrt((ux-ex)*(ux-ex)*(uy-ey)*(uy-ey))<distance && Key.isDown(Key.SPACE)){
_root.msg.text="Computer 1 Says: Press CONTROL to Exit This messsage."
}else
if(Key.isDown(Key.CONTROL)){
_root.msg.text="";
}
}
1.)The variable "distance" is setting how far away the player has to be in order to receive the message.
2.) "ux", "uy", "ex", and "ey" are variables for the char's x and y coordinates and the NPCs x and y coordinates.
3.)The math equation in the "if" statement is calculating how much space is in between the player and the NPC. If the player and the NPC are within 500 pixels (distance variable) of each other, and if the space bar is being pressed, then execute the following action
4.)_root.msg.text="words here"; means to apply the quoted text into the "msg" text box that we made earlier
5.)if(Key.isDown(Key.CONTROL)){ means that if the CTRL button is pressed, execute the following action:
6.) _root.msg.text=""; simply means, make the text box have no words in it.

If you want to add a new message for a different NPC, just copy and paste this code into the new NPC and simply change the message in quotations on line 8 of the code.

The end result should look like this:





















Well that does it! Thank you guys for reading this tutorial and make sure you become a member on my blog so that you get notifications of all of my new tutorials that come out every week!

Feel free to use the source file here: http://adf.ly/30bZr

SmartFoxServer: Multiplayer Flash Games!

Have you ever wanted to create Flash games that you can play online with your friends, but you have only a small amount of knowledge when programming AS2 or AS3? Then SmartFoxServer is the answer for you!

SmartFoxServer (SFS) is a simple program/application that you run on your computer that handles all of the connections and complicated material automatically for you. What is nice about SFS, is that it comes with an Examples folder in the installation path. This allows you to open up AS2 or AS3 example flash documents to view the coding and the structure of how to build chats, games, etc.

So today I will show you how to install SFS and use the "SimpleChat" example in Flash AS2.

First, download it: SmartFoxServer Pro. This is free, but you can only have 20 maximum connections at a time. If you want more people to be able to play at the same time, you have to buy a license for more connections.
Select which OS you are using and open up the installer.

Accept the terms of agreement














Instead of installing to your Program Files, put it on your hard drive or in My Documents. For me, I'm putting it straight onto my hard drive.














After SFS is installed, we need to Port Foward our router so people can connect to our server.
So open up Command Prompt by hitting the start button and searching for "cmd" or "command prompt".
In Command Prompt, type: ipconfig







Find your Default Gateway and IPv4 Address by scrolling to the top and take note of them.







Now, in your browser, type your Default Gateway (mine is 192.168.1.1)
Log in to your router (Some routers default user/pass is: user: admin pass: admin) Once logged in, select the option for Port Forwarding. On a Linksys router, just select "Applications and Gaming".
Use the following options when adding a port:






Notice that for the IP Address, I put the last number from my IPv4 Address.
Now save your options and close your browser.

Last thing we'll look at is the SimpleChat example that is given to you when you install SFS Pro.
Go to the installation path of SFS and find the "Examples" folder. In that folder, choose either "AS2" or "AS3" as your programming language. I'm using AS2. Now choose the "SimpleChat" folder and open up the SimpleChat.fla file in Flash.

You can see that everything is done and ready for you. All you need to do is turn on the server. To do this, go to your SFS folder, and choose "server.bat". This is the program that you run to turn on your server. 































Once its loaded, you can test the chat. Congrats! You're done! If you wish to furthermore change options and functions of the chat or other example just take a look at the forums of the SmartFoxServer.com website and there are plenty of tutorials for you to check out! Thanks for viewing this tutorial!

Thursday, September 29, 2011

AS2: Health Bar

Source file available at: http://adf.ly/2xrNS

Welcome to Tutorial #4 on how to create a health bar in flash using AS2!

We will will work with dynamic text box variables and the _xscale command.

Let's start off by starting a new AS2 document in Flash.
First, draw your HpBar and convert it to a symbol by selecting it and hitting F8 on your keyboard.
I am naming my health bar, "hpBar". Put the registration point where you want the bar to compress down. For example, putting the reg point on the left center block will make the hpBar compress to the left when health is running out. No instance name is required for this tutorial.














Next, we need to create a Dynamic text box.
Give the text box a Variable name of "hp".













After that, go to the frame you are on, open its actions, and type:
hp=100;
"100" is how much health you want your player to have.

Now let's click on our hpBar movie clip, and open its actions. Type this:


onClipEvent(enterFrame){
this._xscale = _root.hp*(100/100)
        //keep the hpBar 100% to scale no matter how much health I have.
if(_root.hp<=0){
        //if the health is lower than 0, then make the hp text 0.
_root.hp=0;
}

         if(_root.hp>=100){
        //if the health is higher than 100, then make the hp text 100. _root.hp=100;
}

}


In the second line of this coding, look at "_root.hp*(100/100)"
The red 100 is required for a 100% scale hpBar. So keep this number.
The blue 100 is your maxHp. Change this to the highest amount of health the player can have.

Last thing we need to do is create a custom way for a player to lose health. As practice I will just create a simple button that once clicked, will decrease the health by a random amount. You can use your imagination to what way you want your player to lose health since I can't cover all of them. Anyways, I am creating my button and putting these actions on it:


on(release){
_root.hp-=random(10);
        //the 10 is the highest number of damage you want to cause.
}





















So once you test your movie, you should be able to lose health randomly! This concludes our tutorial and I hope it helped you! Feel free to download the source file for this tutorial.

Source file available at: http://adf.ly/2xrNS

Tuesday, September 27, 2011

AS2: Score System

Source code for this tutorial is available at: http://adf.ly/2vt0t

Welcome to Tutorial #3! In this tutorial I will teach you how to create your own customizable scoring system for your games.

Let's get started by opening up a new document in Actionscript 2.0
First, create your character movie clip. So draw a shape for your character a call it "character". The registration point does not entirely matter. Once you have a movie clip called "character", give it an instance name "char". Don't forget to give the character it's movement actions! If you need the code to move the character, go here: http://ciretutorials.blogspot.com/2011/09/as2-arrow-keywasd-movement.html















*Hint: You can give instance names through the properties panel.


There are many different ways to utilize a scoring system. In this tutorial I will show you how to collect coins to increase your score. So create an instance of a coin on the stage. Convert it to a movie clip, and call it "coin". Once again, registration point does not matter. You do not need to give the coin an instance name. You can now make duplicates of the coin anywhere you want on the stage.

Next, go into the coin movie clip. Select the shape inside the movie clip and convert it to another movie clip called "coin_child".









Once you have created the child movie clip inside of the parent coin movie clip, open up the coin_child's actions and type:

onClipEvent(enterFrame){
if(_root.char.hitTest(this._parent)){
unloadMovie(this._parent);
_root.score+=1;
}
}

unloadMovie(this._parent) means, if the character hits the coin, then delete the movie clip.

Now we just need to set up the score textbox that lets the player know what their score is. Simply create
a new dynamic textbox on the stage, and give it a variable name of "score".































*Hint: While Instance Names deal with movie clips and text in textboxes, Variable Names are used to deal with numbers such as score, health, distance etc.

Finally, the last piece of code you need, goes in the main timeline frame:
score=0;
This sets the score to be 0 at the beginning of the game.














Well, that concludes Tutorial #3 on Scoring Systems! Hope this helped! Thanks for viewing

Source code is available here: http://adf.ly/2vt0t

AS2: Gravity

Source code available at: http://adf.ly/2vrFM


Welcome to Tutorial #2! In this tutorial, I will show you how to program your characters in your game to respond to gravity. By the end of this tutorial, your character should be able to jump with either the space bar or "W".

Let's get started  by creating a new flash document. Select "AS2" as the programming language.

First off, let's make a movie clip for our character:













Notice how the registration point is at the bottom center square. This is where we want the character to react with the ground.

Next, we need to create the ground movie clip.










After you have created 2 movie clips (character and ground), give the ground an instance name of "ground". You can find this option in the properties panel.








Now you are ready for the code. Enter the following code into the actions of your "character" movie clip:


onClipEvent(load) {
falling=false;
hitting=false;
gravity=10;
speed=6;
jump=0;
jumping=false;
}
onClipEvent(enterFrame) {
if(!_root.ground.hitTest(this._x, this._y, true) && !jumping) {
_y+=gravity;
gravity=10;
falling=true;
}
if(Key.isDown(Key.SPACE) && jumping==false) {
jumping=true;
}
if(jumping) {
this.gotoAndStop("jump")
this._y-=jump;
jump-=1.7;
if(jump<0) {
falling=true;
}
if(jump<-15) {
jump=-15;
}
}
if(_root.ground.hitTest(this._x, this._y, true) && falling) {
jump=12;
jumping=false;
falling=false;
}
}


If you would like to edit the gravity, simply edit the following:
jump-=1.7;
Increasing the number in this variable will make the character jump lower.

If you would like to use the "W" key instead of the Space Bar, change this:
if(Key.isDown(Key.SPACE) && jumping==false) {
To:
if(Key.isDown(87) && jumping==false) {


Well, that concludes today's tutorial on gravity! Feel free to download the source file for this
tutorial: http://adf.ly/2vrFM

Monday, September 26, 2011

AS2: Arrow Key/WASD Movement

Welcome to Tutorial #1, in this tutorial, I will show you how to make a character or a simple movie clip in your game move using the arrow keys and/or WASD.

First off, lets create a new document.
Select AS2 as the programming language.
Draw your character.
Once you have finished drawing your character select the entire shape, and
press F8 to convert it to a movie clip.
When prompted, give the movie clip a name such as "character" or "player" and
for the registration point, keep it in the center of the 9 squares.

*Hint: The registration point is basically the point of rotation. So in this case,
it is the center


After hitting "OK", open up the actions of the new movie clip by either pressing
F9, or right clicking it and selecting "Actions".

In the actions, type:

onClipEvent(load){
speed=4;
}
onClipEvent(enterFrame){
if(Key.isDown(Key.RIGHT)){
this._x+=speed;
}

if(Key.isDown(Key.LEFT)){
this._x-=speed;
}
if(Key.isDown(Key.UP)){
this._y-=speed;
}

if(Key.isDown(Key.DOWN)){
this._y+=speed;
}

Let's take a look at what this code means:


onClipEvent(load){
speed=4;
}


This means, increase the players coordinate values by 4 when any of the buttons are pressed.


onClipEvent(enterFrame){
if(Key.isDown(Key.RIGHT)){
this._x+=speed;


The first line opens up the basic Movie Clip start action function that repeats the action again and again
The second line checks to see if the right arrow key is pressed.
The third line says that if the right key is pressed, then increase the "x" coordinates on the stage by whatever the speed variable is. In this case "speed" is set to 4 pixels per frame.
The other keys are basically the same, just going in different directions:

this._x+=speed;
Right Key
this._x-=speed;
Left Key
this._y-=speed;
Up Key
this._y+=speed;
Down Key


Now, you are all done with arrow keys. But what if you want to use WASD to control the player? Simply change one piece of the coding:

When on the arrow key function, you use this:

if(Key.isDown(Key.RIGHT)){


On WASD function, you use this:

if(Key.isDown(68)){


So pretty much, the code for a letter or number on the keyboard is a number value. You can check number
values by visiting:
http://people.uncw.edu/tompkinsj/112/flashactionscript/keycodes.htm

Well, that's all for Tutorial #1! Thank you for reading and I hoped this helped you!