Load up Flash Professional and click 'Create New Flash Document.'
It will load up the main screen. Click on the oval tool on the right and drag the mouse across the white part of the screen and it will make a black oval.
Click the mouse button on the upper left then select the oval.
Push F8 and it will bring up a pop-up. Name the object, in this case we will name it dot. Make the type Movie Clip. Push ok. ![]() Now click the 'Window' bar and move over 'Workspace Layout' and click 'Default.' This will make the several windows open in Flash. ![]() Now select the dot and click 'Properties' on the bottom of the screen. In the empty box where is says menu, in our case, 'dot'. ![]() Now click the oval/circle you just made. There should be an empty box called actions toward the bottom of the screen. In there, type of paste the following: onClipEvent(enterFrame){ if(Key.isDown(Key.LEFT)) { this._x -=5; } else if(Key.isDown(Key.RIGHT)) { this._x +=5; } } Now, click the control tab at the top and click test movie. You should have a be able to move your dot left and right. Also, there is an empty circle left behind, we'll remove that. Scoot your dot over and click on the empty circle and hit delete. Now we're gonna see what the code does. Okay onClipEvent(enterFrame){ means that whatever is in this function will be executed as soon as the object is loaded, in our case, the moving commands. if(Key.isDown(Key.LEFT)) simply means that if the left key is down, do the following statements. this._x -=5; means that the varible _x, in 'this' object needs to be decreased by five. The varible _x is that stores the x position of the current object. Now add the following line of code on the dot. else if(Key.isDown(Key.UP)) { this._y -=5; } else if(Key.isDown(Key.DOWN)) { this._y +=5; } It allows you to move your little dot up and down now. Artifical Intelligence(Computer players) Now let's make a computer player. Draw another dot somewhere on the screen. Select it and push F8 again. Name him bad_dot. Go in the bottom of the screen and hit properties and name it bad_dot there as well. Now, in the action for the bad_dot, paste the following: onClipEvent(enterFrame){ run_x = random(10);//Random movement generator for x between 0 and 2 run_y = random(10);//Random movement generator for y between 0 and 2 this._x += run_x//It makes the random movement for the x dimension. //It takes the random number in run_x and stores it in _x. this._y += run_y// } This basically let's our little dot move randomly down and to the right. Go to control and click 'Test Movie' again. You'll see the dot moving. Yet we have a problem, when the bad dot hits the end of the screen he will keep going on forver. To fix this we need to some statements to tell the to turn around when it hits the end of the screen. The following is the whole code right now for our bad dot: onClipEvent(load){ var x_direction="right"; var y_direction="up"; } onClipEvent(enterFrame){ run_x = random(10);//Random movement generator for x run_y = random(10);//Random movement generator for y if(this._x <=0){ x_direction = "right"; } else if (this._x >=550){ x_direction ="left"; } if(this._y <=0){ y_direction = "down"; } else if (this._y >=400){ y_direction ="up"; } if(x_direction=="right"){//If the direction is right, go right this._x += run_x; //It makes the random movement for the x dimension. } else { this._x -= run_x; } if(y_direction=="down"){//If the direction is right, go right this._y += run_y; //It makes the random movement for the x dimension. } else if (y_direction=="up") { this._y -= run_y; } } It looks complicated, but we'll break down each piece. The onClipEvent(load){ starts as soon the program loads. The var x_direction="right and var y_direction="up"; are going to be our default directions of our bad dot. if(this._x <=0){ says that if it's 'X' position is less than or equal to zero, meaning on the far left of the screen, it will do x_direction = "right"; which means bad dot's 'x' position will become right if it hits the left edge of the screen. else if (this._x >=550){ says that if it hits the right edge of the screen at pixel point 550. x_direction ="left"; say that it will change directions to left if it hits the far right of the screen. if(this._y <=0) andelse if (this._y >=400) do the same thing with the 'y' dimension. The statement if(x_direction=="right") simply says that if the direction is right, tell the bad dot to go right instead of left. this._x += run_x dimension of this dot have the randomly generated number added to it. You probally noticed that there is circles behind each of your objects. click on them and push delete. Now we're going to make the graphics look nice. Now double-click on the bad dot, now we're in image editing mode. Select the paintbrush and draw some pretty graphics. You can change the color and shape and stuff with the tools. ![]() Once your done, double click off it. Here's what mine look like:
Now let's a put in a background. Towards the top of the screen right click on 'layer 1' and 'insert new layer.' A new tab should appear called 'Layer 2'. Drag the layer so it's below Layer 1. Now click on the dot below the lock of layer 1. This locks layer 1 so you can't accidently mess something up. ![]() Now select the rectangle tool on the right toolbox and then pick a background color with the fill color button. We'll pick the rainbox color. ![]() Now drag across the working area to make your background.
There you have it. A game with player motion, artificial intelligence, graphics, and a background. Collsion Detection Collision detection is basically letting the programming seeing when two objects and touching each other. You might notice that when our two objects run into each other they don't act any differently. We need to fix that. We'll start by clicking on our bad dot and adding the following code: if (this.hitTest(_root.dot)) { if(x_direction=="right") x_direction="left"; if(x_direction=="left") x_direction="right"; } This basically says that if the bad_dot is hit by dot, it will change it's direction. if (this.hitTest(_root.dot)) says that if, this object, being the bad_dot, is hit by _root.dot,meaning our other dot. if(x_direction=="right") x_direction="left"; and if(x_direction=="left") x_direction="right"; just say that if it's going right, turn left, and vice versa. Now we have some basic collision detection and let's add a point to this game. We'll add health to our bad dot and let the normal dot be able to do some damage. We're going to create a new varible that saves the bad dot's health. Well call it bad_dot_health and give it an intial value of a hundred.
Now we're going to set it so everytime the bad dot runs into the dot, it loses one health. We'll add bad_bot_health--; in the hitTest section. Like so:
By now, your code for your bad_bot object should be this. v onClipEvent(load){ var x_direction="right"; var y_direction="up"; var bad_bot_health=100; } onClipEvent(enterFrame){ run_x = random(10)+speed;//Random movement generator for x run_y = random(10)+speed;//Random movement generator for y if(this._x <=0){ x_direction = "right"; } else if (this._x >=550){ x_direction ="left"; } if(this._y <=0){ y_direction = "down"; } else if (this._y >=450){ y_direction ="up"; } if(x_direction=="right"){//If the direction is right, go right this._x += run_x; //It makes the random movement for the x dimension. } else { this._x -= run_x; } if(y_direction=="down"){//If the direction is right, go right this._y += run_y; //It makes the random movement for the x dimension. } else if (y_direction=="up") { this._y -= run_y; } //Collision Detection if (this.hitTest(_root.dot)) { bad_bot_health--; if(x_direction=="right") x_direction="left"; if(x_direction=="left") x_direction="right"; } } And this is the code for the dot: onClipEvent(enterFrame){ if(Key.isDown(Key.LEFT)) { this._x -=5; } else if(Key.isDown(Key.RIGHT)) { this._x +=5; } if(Key.isDown(Key.UP)) { this._y -=5; } else if(Key.isDown(Key.DOWN)) { this._y +=5; } } Displaying Data Now we have our bad dot getting hurt. But the problem is we don't know what his health is. We'll fix this by making a text box on the screen. Click the text box button on the left toolbox and drag it on the screen to make a blank text box. ![]() Now select the text box and open up the properties at the bottom of the screen. Make 'Dynamic Text' is selected and not 'Static Text.' We also need to name our text box. Right below 'Dynamic Text' type 'status' in the box called instance name. Farther other in the 'Var' box, type status there as well. Now we're going to make it display bad dot's health in the text box. All we have to do is add the following line of code:_root.status = bad_bot_health; ![]() This says that the text box status is given the value of bad_bot_health. Which we already made go down everytime he runs into dot. Test the program and you'll see it displays bad dot's health on the screen. *Note: Make sure the text color of the text box isn't white because you won't be able to see the text. As of now this is how the program should look ![]() SoundsEvery game needs sounds and music. What we are going to do is make it play a sound each time bad dot gets attacked. First we need to import sounds. You can import from anywhere on your computer but we are going to from the file on the desktop in Flash Tutorials in the sfx folder(C:\Documents and Settings\Labtech\Desktop\Flash Tutorials\sfx). Press file, import, and import to stage. Find the music file 'damage' andopen it. Now click on bad bot. At the top of file in the onClip load section add this code: var playedonce = false; var damagesound = new Sound(); damagesound.attachSound("damage"); And inside of the hitTest section add the following code: soundlength = damagesound.duration; soundpos = damagesound.position; if (soundpos==soundlength || !playedonce){ damagesound.start(); playedonce = true; } At the top, we made two varibles playedonce and damagesound. playedonce will be false until the sound is played at least once. damagesound is given the value of the sound we just imported, "damage". Further down in the code are the varibles soundlength and soundpos. soundlength is simply the length of the current sound and soundpos is the current position of the sound being played. Soundlength is given the value of duration of the sound 'damage' and damagesound is given the value of the position of the damage sound. Then we test to see if soundpos is equal to soundlength. Basically asking if the sound is at the end. This makes it so when play a sound it will finish before starting the next one, so we don't loop the sounds. Now test it and it will play the damage sound each time you run into bad bot.
int * pointer=new int; *pointer = 15; cout << *pointer; delete pointer; |