sactownboy10
09-03-2005, 07:31 PM
This is my first tutorial i worked really hard on it and it took about 3 hours i tried to make it like Pols and explain everything and i hope i did a good job.
After reading this tutorial u should have enough knowledge to make your own simple game in flash. First thing u need to know how to do is make movieclips. To make a movieclip u must make the object u want to be a movieclip then take the mouse and click over the object and with it highlighted press f8 which should bring up another window click on movieclip and give it a name (note this is not the instance name which ill be referring to later)then click ok and the object should be surrounded in a blue square which means its selected. Now to add the script, with the object selected click the actions tab.
NOTE: ALL ACTIONSCRIPT IS IN BLUE
Event Handlers:
First thing i'm gonna talk about is movie clip event handlers. Event handlers start the script. onClipEvent(enterFrame) and onClipEvent(load) are the two event handlers. U use onClipEvent(enterFrame) when u want the actionscript to work everytime u enter a certain frame in the movie. onClipEvent(load) is used when u want the actionscript to happen only when the movie or game loads. Next thing im gonna talk about is button event handlers. A button event handler is event handlers u use to start off the actionscript of a button. Heres all the button event handlers: on(press) and on(release).
Operators:
Operators are signs such as + and = here is a list of operators:
+ means addition, - means subtraction, * means multiplication, / means division, % means Modulo(remainder of division), ++ means Increment, -- means Decrement, = means Equals(numeric operator used to assign variables, properties, methods, etc.), == means is equal to(comparison operator used to compare values), === means Is equal to (comparison operator used to compare non-string values and their data types), != means Is not equal to(comparison operator used to compare values), < means Less than (comparison operator used to compare values), > means Greater than (comparison operator used to compare values), <= means Less than or equal to (comparison operator used to compare values), >= means Greater than or equal to (comparison operator used to compare values), && means AND (logical operator that returns TRUE if both operands are true), || means OR (logical operator that returns TRUE if the left or right operand is true), ! means NOT (logical operator that returns the Boolean opposite of the operand following the ! operator.
Commenting your code:
Commenting your code is used very often to remind yourself what your code does. To comment your code u type // and everything after that is comments and doesnt affect the code.
Variables:
In this code
onClipEvent(enterFrame){
moveSpeed = 10;
}
moveSpeed would be the variable.
Making a button:
Theres many ways to make a button first u must make the object u want to be pressed then when u press f8 instead of clicking movieclip click button and in the actions tab u must first start with a button event handler either on(press){ or on(release){ then in the next line u must put what action u want to happen when the button is pressed. So if u wanted to go to frame 5 when the button is released u would put
on(release){
gotoAndPlay(5);
}
when u make a button such as a play button where u dont want anything to happen until the button is pressed in the timeline u must click the frame the button is on and then open up the actions tab and put this code stop(); which stops the animation until a button is pressed to make it play again
Instance Names:
Instance names are what u use in actionscript to set the instance name u must first make the object then press f8 and make it a movieclip then in the properties tab there is a box that says instance name in there type what u want the instance name to be
Putting it all together:
Heres the basic moving script
onClipEvent(load){
moveSpeed = 5;
}
This piece of script tells the program to set the variable moveSpeed at 5
onClipEvent(enterFrame){
if(Key.isDown(Key.UP)){
this._y -= moveSpeed;
}
In this script onClipEvent(enterFrame){ is the event handler, if(Key.isDown(Key.UP)){ tells the program to perform the action in the next line when the up key is pressed, and -= is the operator which means if the down key is pressed subtract the speed by moveSpeed which would make the object stop
This is the scripts for down, right, and left
if(Key.isDown(Key.DOWN)){
this._y += moveSpeed;
}
if(Key.isDown(Key.RIGHT)){
this._x += moveSpeed;
}
if(Key.isDown(Key.LEFT)){
this._x -= moveSpeed;
}
}
Hittest:
Hittest is a script that does something when one object hits another object. Hittest always starts with onClipEvent(enterFrame){, and the second line goes like this if(this.hitTest(_root.instance name of object u want to show is being hit)){ then the third line shows what action u want to happen when one object hits another object. If u want to go to frame 7 when an object with instance name "player" hits an object with an instance name "enemy" the code would look like this.
onClipEvent(enterFrame){
if(this.hitTest(_root.enemy)){
gotoAndPlay(7);
}
}
when u want to make a wall type effect u must use this code (the wall has an instance name of wall)
right wall
onClipEvent(enterFrame){
if(this.hitTest(_root.wall)){
this._x -= moveSpeed;
}
}
left wall
onClipEvent(enterFrame){
if(this.hitTest(_root.wall)){
this._x += moveSpeed;
}
}
top wall
onClipEvent(enterFrame){
if(this.hitTest(_root.wall)){
this._y -= moveSpeed;
}
}
bottom wall
onClipEvent(enterFrame){
if(this.hitTest(_root.wall)){
this._y += moveSpeed;
}
}
Extra Scripts:
to make a movieclip with the instance name "enemy" move towards a movieclip with an instance name "player" at a speed of 5 the code would be
onClipEvent(load) {
speedX = 5;
speedY = 5;
}
onClipEvent(enterFrame) {
if(_root.player._x > this._x) {
this._x += speedX;
}
if(_root.player._x < this._x) {
this._x -= speedX;
} else if((_root.player._x - this._x) <= 2 && (_root.player._x - this._x) >= -2) {
randFrame = 2 + Math.floor(Math.random()*(2-1+1));
this.gotoAndPlay(randFrame);
}
if(_root.player._y > this._y) {
this._y += speedY;
}
if(_root.player._y < this._y) {
this._y -= speedY;
} else if((_root.player._y - this._y) <= 2 && (_root.player._y - this._y) >= -2) {
randFrame = 2 + Math.floor(Math.random()*(2-1+1));
this.gotoAndPlay(randFrame);
}
}
I used this script in my portal game.
Random Movement
onClipEvent (load) {
time=0;
dir=2;
speed=20;
}
onClipEvent (enterFrame) {
maxTime = random(5);
time++;
if (time>=maxTime) {
time = 0;
}
if (time == 0) {
dir = random(20);
}
if (dir == 0) {
this._y -= speed;
} else if (dir == 1) {
this._y += speed;
} else if (dir == 2) {
this._x -= speed;
} else if (dir == 3) {
this._x += speed;
}
}
Jumping Script
onClipEvent (load) {
grav_y = 0;
jumping = false;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.SPACE) && !jumping) {
grav_y = 20;
jumping = true;
}
if (jumping == true) {
grav_y -= 1;
if (grav_y<=-10) {
grav_y = -10;
}
this._y -= grav_y;
}
if (_root.ground.hitTest(this._x, this._y+45, true)) {
grav_y = 0;
jumping = false;
}
}
NOTE: if you want to have the character to stop at a ground point, you must make a movieclip and label it ground
I hope this tutorial helped if it did dont be afraid to push the rep button
After reading this tutorial u should have enough knowledge to make your own simple game in flash. First thing u need to know how to do is make movieclips. To make a movieclip u must make the object u want to be a movieclip then take the mouse and click over the object and with it highlighted press f8 which should bring up another window click on movieclip and give it a name (note this is not the instance name which ill be referring to later)then click ok and the object should be surrounded in a blue square which means its selected. Now to add the script, with the object selected click the actions tab.
NOTE: ALL ACTIONSCRIPT IS IN BLUE
Event Handlers:
First thing i'm gonna talk about is movie clip event handlers. Event handlers start the script. onClipEvent(enterFrame) and onClipEvent(load) are the two event handlers. U use onClipEvent(enterFrame) when u want the actionscript to work everytime u enter a certain frame in the movie. onClipEvent(load) is used when u want the actionscript to happen only when the movie or game loads. Next thing im gonna talk about is button event handlers. A button event handler is event handlers u use to start off the actionscript of a button. Heres all the button event handlers: on(press) and on(release).
Operators:
Operators are signs such as + and = here is a list of operators:
+ means addition, - means subtraction, * means multiplication, / means division, % means Modulo(remainder of division), ++ means Increment, -- means Decrement, = means Equals(numeric operator used to assign variables, properties, methods, etc.), == means is equal to(comparison operator used to compare values), === means Is equal to (comparison operator used to compare non-string values and their data types), != means Is not equal to(comparison operator used to compare values), < means Less than (comparison operator used to compare values), > means Greater than (comparison operator used to compare values), <= means Less than or equal to (comparison operator used to compare values), >= means Greater than or equal to (comparison operator used to compare values), && means AND (logical operator that returns TRUE if both operands are true), || means OR (logical operator that returns TRUE if the left or right operand is true), ! means NOT (logical operator that returns the Boolean opposite of the operand following the ! operator.
Commenting your code:
Commenting your code is used very often to remind yourself what your code does. To comment your code u type // and everything after that is comments and doesnt affect the code.
Variables:
In this code
onClipEvent(enterFrame){
moveSpeed = 10;
}
moveSpeed would be the variable.
Making a button:
Theres many ways to make a button first u must make the object u want to be pressed then when u press f8 instead of clicking movieclip click button and in the actions tab u must first start with a button event handler either on(press){ or on(release){ then in the next line u must put what action u want to happen when the button is pressed. So if u wanted to go to frame 5 when the button is released u would put
on(release){
gotoAndPlay(5);
}
when u make a button such as a play button where u dont want anything to happen until the button is pressed in the timeline u must click the frame the button is on and then open up the actions tab and put this code stop(); which stops the animation until a button is pressed to make it play again
Instance Names:
Instance names are what u use in actionscript to set the instance name u must first make the object then press f8 and make it a movieclip then in the properties tab there is a box that says instance name in there type what u want the instance name to be
Putting it all together:
Heres the basic moving script
onClipEvent(load){
moveSpeed = 5;
}
This piece of script tells the program to set the variable moveSpeed at 5
onClipEvent(enterFrame){
if(Key.isDown(Key.UP)){
this._y -= moveSpeed;
}
In this script onClipEvent(enterFrame){ is the event handler, if(Key.isDown(Key.UP)){ tells the program to perform the action in the next line when the up key is pressed, and -= is the operator which means if the down key is pressed subtract the speed by moveSpeed which would make the object stop
This is the scripts for down, right, and left
if(Key.isDown(Key.DOWN)){
this._y += moveSpeed;
}
if(Key.isDown(Key.RIGHT)){
this._x += moveSpeed;
}
if(Key.isDown(Key.LEFT)){
this._x -= moveSpeed;
}
}
Hittest:
Hittest is a script that does something when one object hits another object. Hittest always starts with onClipEvent(enterFrame){, and the second line goes like this if(this.hitTest(_root.instance name of object u want to show is being hit)){ then the third line shows what action u want to happen when one object hits another object. If u want to go to frame 7 when an object with instance name "player" hits an object with an instance name "enemy" the code would look like this.
onClipEvent(enterFrame){
if(this.hitTest(_root.enemy)){
gotoAndPlay(7);
}
}
when u want to make a wall type effect u must use this code (the wall has an instance name of wall)
right wall
onClipEvent(enterFrame){
if(this.hitTest(_root.wall)){
this._x -= moveSpeed;
}
}
left wall
onClipEvent(enterFrame){
if(this.hitTest(_root.wall)){
this._x += moveSpeed;
}
}
top wall
onClipEvent(enterFrame){
if(this.hitTest(_root.wall)){
this._y -= moveSpeed;
}
}
bottom wall
onClipEvent(enterFrame){
if(this.hitTest(_root.wall)){
this._y += moveSpeed;
}
}
Extra Scripts:
to make a movieclip with the instance name "enemy" move towards a movieclip with an instance name "player" at a speed of 5 the code would be
onClipEvent(load) {
speedX = 5;
speedY = 5;
}
onClipEvent(enterFrame) {
if(_root.player._x > this._x) {
this._x += speedX;
}
if(_root.player._x < this._x) {
this._x -= speedX;
} else if((_root.player._x - this._x) <= 2 && (_root.player._x - this._x) >= -2) {
randFrame = 2 + Math.floor(Math.random()*(2-1+1));
this.gotoAndPlay(randFrame);
}
if(_root.player._y > this._y) {
this._y += speedY;
}
if(_root.player._y < this._y) {
this._y -= speedY;
} else if((_root.player._y - this._y) <= 2 && (_root.player._y - this._y) >= -2) {
randFrame = 2 + Math.floor(Math.random()*(2-1+1));
this.gotoAndPlay(randFrame);
}
}
I used this script in my portal game.
Random Movement
onClipEvent (load) {
time=0;
dir=2;
speed=20;
}
onClipEvent (enterFrame) {
maxTime = random(5);
time++;
if (time>=maxTime) {
time = 0;
}
if (time == 0) {
dir = random(20);
}
if (dir == 0) {
this._y -= speed;
} else if (dir == 1) {
this._y += speed;
} else if (dir == 2) {
this._x -= speed;
} else if (dir == 3) {
this._x += speed;
}
}
Jumping Script
onClipEvent (load) {
grav_y = 0;
jumping = false;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.SPACE) && !jumping) {
grav_y = 20;
jumping = true;
}
if (jumping == true) {
grav_y -= 1;
if (grav_y<=-10) {
grav_y = -10;
}
this._y -= grav_y;
}
if (_root.ground.hitTest(this._x, this._y+45, true)) {
grav_y = 0;
jumping = false;
}
}
NOTE: if you want to have the character to stop at a ground point, you must make a movieclip and label it ground
I hope this tutorial helped if it did dont be afraid to push the rep button