Please Note: Follow all instructions carefully. Missing any of them will make the whole thing Fail!
Hit The Bug!
Now its time to make your first game. Here we will be using some Maths, so be ready for it. First download the following zip file and extract it in to a folder.
images.zip | |
File Size: | 12 kb |
File Type: | zip |
Importing files
File➜Import➜To Stage
Import all the pictures that you have extracted into flash. You may have to move the imported pictures apart
Converting pictures to objects
To convert bitmaps(pictures) to objects( in flash objects are movieclips)
Give an instance name bug for the button bug
Now comes the coding!
File➜Import➜To Stage
Import all the pictures that you have extracted into flash. You may have to move the imported pictures apart
Converting pictures to objects
To convert bitmaps(pictures) to objects( in flash objects are movieclips)
- Select the object
- Right Click ➜ convert to symbol
- Choose movieclip for the swatter and button for the bug
- chose a name
Give an instance name bug for the button bug
Now comes the coding!
Place this code in the first frame in the timeline.
Select frame 1 and hit F9 and type the following code
bug._x = random(400) + 35; bug._y = random(300) + 75; // give a random position for the bug // random(x) is a function that generates a random number from 0 to x count = 1; // count is a variable that keeps time speed = 35; // speed is the variable that determines the speed of the game // the value of speed is inversly proportional to the actual speed hit = false; // hit is a booloean datatype // it is set to false as the bug has not yet been hit // as mentioned before onEnterFrame= function {code ;code;} //the code will repeat periodically as long as the control is in this frame onEnterFrame = function () { count++;// increasing the count per unit time // after some time the following if condition is satisfied if (count % speed == 0) { // resetting the time to 1 count = 1; // giving new position bug._x = random(500) + 35; bug._y = random(325) + 45; // setting the bug hit to false hit = false; } };
Place this code in the button bug
Make sure you click the bug and only the bug. Then hit F9
on (press) { if (hit ==false) { hit=true count=speed-1 } }
ctr + Enter to run the movie
Code Explanation.
The above code has been commented well. Read it carefully and understand the code. It is important that you understand the code and not simply copy paste. Here is a brief explanation of what the code does.
All code before the onEnterFrame=function() will get executed once in the beginning. This will give the initial values to our game. The first 2 lines of code gives the initial random location for the bug.
All code within onEnterFrame=function() { code ; code; code ;} will get executed continuously. The speed depends on the frame rate.
for ex : if the frame rate is 30 FPS( frames per second) , then the code gets executed 30 times a second!
in the code line if(count%speed==0) , it simply means count variable is divided by speed variable . But instead of getting the quotient , we get the remainder. This "%" is called the modulus operator. So count starts from 1 and and is incremented(increased by 1) every time.
NOTE: count++ is increment(Increase by 1) while count-- is decrement(decrease by 1) .
So in our code speed = 35 . That means count has to be incremented 35 times to be divisible by 35 ( remainder should be equal to zero: count%speed==0 ) . Once its divisible , a certain code is executed - in our case the code gives a new random location to our bug and resets the variables hit and count .
So in order to increase the speed of the game , you need to decrease the value of speed variable and vice versa
Our button code is pretty simple. If the bug has not been hit , then you set hit = true . The tricky part is count= speed -1 .This may take a bit of time to understand but once you understand its very straight forward. When you set count = speed -1 , What happens is that the value of count is equal to one less than the value of speed. So since speed =35, now count =34 .The reason we do this is , the next time our onEnterFrame=function() is executed, count variable will be incremented to 35 -> therefore being divisible by 35 if(count%speed==0) will be satisfied . The code gets executed and new location will be set to the bug. We do this because we want to give our bug a new location immediately after hitting it.
Once you understand the code, you can move on to the next exciting part where we will replace the mouse with the swatter and more stuff.
All code before the onEnterFrame=function() will get executed once in the beginning. This will give the initial values to our game. The first 2 lines of code gives the initial random location for the bug.
All code within onEnterFrame=function() { code ; code; code ;} will get executed continuously. The speed depends on the frame rate.
for ex : if the frame rate is 30 FPS( frames per second) , then the code gets executed 30 times a second!
in the code line if(count%speed==0) , it simply means count variable is divided by speed variable . But instead of getting the quotient , we get the remainder. This "%" is called the modulus operator. So count starts from 1 and and is incremented(increased by 1) every time.
NOTE: count++ is increment(Increase by 1) while count-- is decrement(decrease by 1) .
So in our code speed = 35 . That means count has to be incremented 35 times to be divisible by 35 ( remainder should be equal to zero: count%speed==0 ) . Once its divisible , a certain code is executed - in our case the code gives a new random location to our bug and resets the variables hit and count .
So in order to increase the speed of the game , you need to decrease the value of speed variable and vice versa
Our button code is pretty simple. If the bug has not been hit , then you set hit = true . The tricky part is count= speed -1 .This may take a bit of time to understand but once you understand its very straight forward. When you set count = speed -1 , What happens is that the value of count is equal to one less than the value of speed. So since speed =35, now count =34 .The reason we do this is , the next time our onEnterFrame=function() is executed, count variable will be incremented to 35 -> therefore being divisible by 35 if(count%speed==0) will be satisfied . The code gets executed and new location will be set to the bug. We do this because we want to give our bug a new location immediately after hitting it.
Once you understand the code, you can move on to the next exciting part where we will replace the mouse with the swatter and more stuff.