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!