Latest from the community:


Desolate Defense Released!

Desolate Defense, a tower defense game created by myself, has just been released this last week and can be played here on this site. Also, we are now working on a game for newgrounds’ “Power of Three” summer event, making a medieval defense game.

In other news, the game submission system seems to have stopped working (it won’t upload the flash files for some reason). Hopefully, that will be fixed soon, but for now, email us the information and the links to the files.

-Chad Duncan

Posted: July 7th, 2009
at 10:44pm by Chad Duncan


Categories: Site News

Comments: No comments


Basic Scoring In Flash (AS2, AS3)

Scores are an essential part in all games. They challenge the user while rewarding them at the same time. Keeping track of scores in Flash isn’t tricky to use, but it is important that they are implemented correctly.

Declaring The Variable

The fundamental building block behind a score is the variable. As you ought to have learned, variables are used to store objects, pointers, and raw data. If you haven’t, you only need to know that a variable will keep track of the player’s score for us. This is what a score variable will look like when it is declared:

var score:Number = 0;

Though it should be evident, this line makes a new variable that can be accesed by ’score’ and holds a value of zero. Typically, scores are numbers and thus the above is a strict declaration of Number meaning it can only hold a number (floats and intgers).

Note that where you place this line of code is important. Say if you have a frame for every level and put that on every frame, the score will be erased and reset to zero at a new place in memory. But perhaps you wanted the score to continue incrementing from level to level, then you would need to declare the score variable before the level frames. In most cases  you should declare the variable when the movie is initiated. In AS3, it is best to put it in the the document class; in AS2, the first frame. This way the variable can only be modified and not re-declared.

Changing The Score

Now that there is a variable in place to keep track of the player’s score, the variable must be updated accordingly. This is done with simple arithmetic and assignment operators:

+, -, *, /, %, +=, -=, *=, /=, %=, ++, and –

When iplemented, a typical line should look like this:

score += 5;

This might have to be changed depending on the namespace of score. If it was declared as recommended (Main Timeline), it should always be called directly:

AS2:
_root.score += 5;

AS3:
MovieClip(root).score += 5;

Say if you call score += 5 from an instace of an enemy, it will try accessing an imaginary (unless created for some other reason) variable in the enemy object that does not exist. Thus being the reason the score variable is global, or at the lowest level of the internal namespace. (Unless you’re anal about security and create an object for the game which has a variable with get() and set() functions)

Displaying The Score

Displaying the score is a quick process once you’ve got it down. To start, add a text box to the stage in the place you would like your score to be shown. Select the new box (it should be already) and find the poperties window (Ctrl+F3).  Now there will be two different proccesses depending on the version of actionscript your in.

AS2:
Change the text box’s Text Type to Dynamic Text.
Change the Var to your score variable. (It’s good to keep it on the main stage as mentioned, use _root if you did)

scores_1

And that’s it! Your the score will be displayed exactly as it is in memory.

AS3:
Change the text box’s Text Type to Dynamic Text.
Give it an instance name (ie. txtBx)
Create an onFrame event and use ‘MovieClip(root).scrBx.text = String(MovieClip(root).score).’ Or to be more efficient, just use it when the score is updated.

scores_2

Obviously it takes a bit more work to display the score in AS3, so to cut down a bit, it might be easier to create a function that will be called by some onFrame event running during the game.

public function changeScore(num:Number):void{

MovieClip(root).score += num;
MovieClip(root).scrBx.text = String(MovieClip(root).score);

}

Posted: April 10th, 2009
at 7:04pm by dank

Tagged with , , , , , , , ,


Categories: ActionScript 1 & 2, ActionScript 3, Flash, Programming, Tutorials

Comments: 1 comment


Sound in ActionScript 2

Sound, whether in the form of music or sound effects,  is an important part of any flash game. While you may be able to get away with controlling sound with the timeline in an animation, a game must be a bit more dynamic and you will almost always need to control the sound with actionscript. This tutorial will help you understand not only control sound in actionscript 2, but avoid potential problems.

Example

Below gives an example of actionscript you may use to play a sound. Note: in this example, I attach the sound to a movieclip which should help avoid problems that may occur when implementing something else like mochiads or controlling the volume of music and sound effects separately.

var music:Sound = new Sound(music_mc);
music.attachSound(”song”);
music.start(0, 10000);
//0 is the offset (we want it to start at the beginning
//the 10000 is how many times I want the sound to play in a loop

In the above example, I’m trying to play a sound with the indentifier “song”. You’ll need to go to the library of your flash file and find the sound you want to control. In the right-click menu, press linkage to take you to the linkage menu of the sound file. Give it the identifier you want and make sure “export for actionscript” is checked, but make sure “export in first frame” is not checked if you want to have a preloader. Otherwise, if the sound is exported to the first frame, the preloader will not load until after the sound is loaded. Therefore, if “export in first frame” is not checked, on a frame on the timeline somewhere after the preloader, set the sound to start in the properties panel, but don’t allow the timeline to play through the frame. I would suggest making the sound start in the second frame of a  movieclip and making the movieclip stop on the first to prevent it from playing. Having the sound technically start in the flash somewhere will allow the sound to be compiled with your swf file and not mess up your loader.

Other stuff

music.stop(); – can be used to stop the sound
music.setVolume(); – can be used to set the volume (ex: setVolume(80))
music.getVolume(); – can be used to find out what the volume is set to
music.loadSound(”song.mp3″, false); – use this instead of attachSound() if you want to load a sound externally. The first parameter is the file url and the second is whether you want the sound to stream or not

Posted: March 30th, 2009
at 10:02am by Chad Duncan


Categories: ActionScript 1 & 2, Game Development, Programming, Tutorials

Comments: No comments


Aiming and Shooting

Having the knowledge of how to simply aim and/or shoot is essential to many types of games. It can be done in many ways depending on the type of game but generally relies on the same concept.

Aiming

To make a movieclip rotate towards the mouse, we need to find the difference in coordinates in the two, use some math to find the angle in radians, and then convert it to degrees.

xDifference = player._x – _xmouse;
yDifference = player._y – _ymouse;
var angleRadians:Number = Math.atan2(yDifference, xDifference);
var angleDegrees:Number = Math.round(angleRadians*180/Math.PI);
player._rotation = angleDegrees;

In the first two lines, the difference in x and y coordinates is found. With Math.atan2, we can change the points to radians. Then we must convert it to degrees with the formula: angle in degrees = angle in radians * 180 / Pi. Now we have what we need to tell the movieclip to rotate to that angle.

Shooting

You’ll need to create a movieclip to be the bullet and give it that instance name. Make another movieclip (which could be a tank, cannon, protagonist, etc) with the instance name: player. The bullet movieclip should be placed at the center of the player with actionscript. We want the bullet to duplicate, so with actionscript 2 you could simply use the duplicateMovieClip function. Otherwise, if you use AS3, you may want to see another tutorial for that part. Make sure the original movieclip does not move (ex: _name can be used to distinguish it).

Next, place the aiming code on the player. With an onMouseDown function or something equivalent, the bullet should be set to duplicate. Then use the following code for the duplicated bullets (especially if you use as3, change it a bit if you need it to be on the timeline):

speed = 20; //This can be changed however you like.
xTarget = _xmouse;
yTarget = _ymouse;
if(xChange==undefined){
//A proportion is found here between x and y coordinates
xChange = (xTarget/this._x);
yChange = (yTarget/this._y);
}
//Using the proportion, we can make it move in a line.
//The proportion is multiplied by speed to make the bullet move quickly
_x+=xChange*speed;
_y+=yChange*speed;

I know that for some, this code may seem a bit weird, but it’s simple and it works. This isn’t the only way to do it either. The rest is up to you, but I hope this helps a bit.

Posted: March 29th, 2009
at 8:15pm by Chad Duncan


Categories: ActionScript 1 & 2, Game Development, Programming, Tutorials

Comments: No comments


Functions

Functions are very helpful in reducing the amount of code used throughout a flash for actionscript that may repeat several times throughout. They are simple to learn and use, and they are a common part of making Flash games.

Example:

function loseHealth(amount){
_root.player.health -= amount;

if(_root.player.health<=0){
_root.player.gotoAndStop(”dead”);

}else{
_root.player.gotoAndPlay(”hurt”);
}
}

This example creates a function to be executed when a player needs to be hurt. For example, if an enemy hits the player, you could use loseHealth(10); to make the player lose health. To create your own function use something like:

function theFunctionsName(parameter1, parameter2, etc){
//your code here
}

You first put “function ” then the name of the function (can be basically anything) and then in parentheses ( ) you put the parameters, which will be variables passed to the function and can be as many as you want with whatever name. You can use the function anywhere else as the name of your function followed by the parameters in parenthesis.

I know this tutorial is simple, but it should help you understand how functions work.

Posted: March 29th, 2009
at 5:10pm by Chad Duncan


Categories: ActionScript 1 & 2, Game Development, Programming, Tutorials

Comments: No comments


Find Distances

Sometimes in game development, you may need to find the distances from one movieclip to another. For example, if you are creating enemy AI and you do not wish for the AI to see player at certain distances or want gunshots to do more damage at closer distances, it would be helpful to know this.

The Code

xDifference = player._x - enemy._x;
yDifference = player._y - enemy._y;
distance = Math.sqrt(Math.pow(xDifference, 2) + Math.pow((yDifference), 2));

Explanation

You need to find the distance between the x and y coordinates first. Using the Pythagorean theorem, c^2 = a^2 + b^2 (distance^2 = xDifference^2 + yDifference^2 in our case), we are able to calculate the distance by rewriting the formula as c = ? a^2 + b^2.  So Math.sqrt() gets the square root. Math.pow() squares the variables.

Note: This code can actually be written several different ways.

Posted: March 29th, 2009
at 4:41pm by Chad Duncan


Categories: ActionScript 1 & 2, Programming, Tutorials

Comments: No comments


Game Submission System

A new part of the site has just been finished–you can now submit games to the site. However, the games must be reviewed at this time and all games must be pg13 or below. So, now more games will be able to appear on the site than just mine and danks. For you devs, please feel free to start uploading.

:D

Chad Duncan

Posted: March 25th, 2009
at 9:43pm by Chad Duncan


Categories: Site News

Comments: No comments


MiniGame Arcade 2 Released!

The sequel to Minigame Arcade is finally complete! Because I thought the original could’ve been a lot better, I decided to make another but with improved quality. If you have played the original, you would know that it’s a collection of a dozen minigames. It took several months to make, but you can see it now.

Go Play It!

Have Fun!

- Chad

Posted: December 9th, 2008
at 5:26pm by Chad Duncan


Categories: Site News

Comments: No comments


Fire Storm 2

check it out: http://www.freewebs.com/miked-games/fire_storm2.htm

Posted: November 7th, 2008
at 5:22pm by Miked


Categories: Game Design

Comments: No comments


Multi-Dimensional Arrays in Flash

Arrays make a great way to store and manage data in actionscript. However, sometimes just a single dimension may not be adequate for some situations such as when storing table data. Then you will need to either: define an array for each column for every row, or just make a one two-dimensional array.

A two-dimensional array can be thought of an array nested in an array. To instantiate an array, we can either do:

var arr1:Array = new Array(1, 2, 3);
var arr2:Array = new Array(4, 5, 6);
var arr3:Array = new Array(7, 8, 9);
var arr:Array = new Array(arr1, arr2, arr3);

Or the quicker way:

var arr:Array = new Array([1, 2, 3], [4, 5, 6], [7, 8, 9]);

All we’re doing here is defining arrays inside of an array. To reference a two-dimensional array is practically the same as a normal array except for we have a second element to reference. This means we just need to add another “[n-1]:”

// Output: 6
trace(arr[1][2]);

To add a new element to the first array, we can use the same syntax as we would with a single-dimension array, but we would be pushing an array data type:

// Adds another element to the outer array
arr.push(arr1);

To add an element to one of our existing arrays, we reference the array’s element in the outer array:

// Ads a sub element to arr1[1] (= 4,5,6)
arr[1].push(arr1);

The rest of the functions in the Array package are used in the same way, but with different references to the caller.

« Older Entries