Insert Clever As:Main Joke Here
Well, this is my first topic, so I dont know exactly how to do this, and i couldn't really think up of a good name. Also, not all of these codes are mine, but I took them and edited them to make them work. I dont know if this was allowed, but I did it anyway. Ok, onto the topic.
What you will be learning
After this, you will be able to create a game with a main char, enimies, walls, health, gold, and potions. Here is an example of what you will be making.
Part A- Main Character
First off, you need to make some drawings. Draw your main character, and make 3 MCs into 3 different animations. They are standing, walking, and attacking. Now put off off these into one MC, with each of these on a seperate frame. Name(the instance name) each of the 3 animations p1walking, p1standing, p1attacking. Then put them on the 3 frames in this order- Standing, walking, attacking, Name the outside MC p1(instance name). Good. Now add a stop(); action to each of the frames inside of p1. This is a little confusing, but let me recap it all for you. You have a MC with the instance name of p1. Inside of it there are 3 frames, one called p1standing, with the person stnadng, one called p1walking, with the person walking, and on the last fram eit is called p1 attacking, with the person attacking. Good, and we are almost done with making the main character. Now go to the Mc with the 3 animations in it and give it this code. Make sure all of the instance names are right.
I almost forgot. Go to the animation of your player attackign, and make his weapon the instance name of sword.
onClipEvent (mouseMove) {
Xd = _root._xmouse-_x;
Yd = _root._ymouse-_y;
radAngle = Math.atan2(Yd, Xd);
_rotation = int((radAngle*360/(2*Math.PI))+90);
updateAfterEvent();
}
onClipEvent (enterFrame) {
_root.standing = true;
if (Key.isDown(Key.UP)) {
_root.attacking = false;
_root.standing = false;
_root.p1.gotoAndStop(2);
_root.p1.p1walking.play();
if (_rotation>180) {
_y -= (5*Math.cos(Math.PI/180*(_rotation)));
_x += (5*Math.sin(Math.PI/180*(_rotation)));
} else {
_y += (5*Math.cos(Math.PI/180*(_rotation)));
_x -= (5*Math.sin(Math.PI/180*(_rotation)));
}
}
if (Key.isDown(Key.DOWN)) {
_root.standing = false;
_root.attacking = false;
_root.p1.gotoAndStop(2);
_root.p1.p1walking.play();
if (_rotation>180) {
_y += (5*Math.cos(Math.PI/180*(_rotation)));
_x -= (5*Math.sin(Math.PI/180*(_rotation)));
} else {
_y -= (5*Math.cos(Math.PI/180*(_rotation)));
_x += (5*Math.sin(Math.PI/180*(_rotation)));
}
}
}
onClipEvent (enterFrame) {
if (_root.standing == true && _root.attacking == false) {
_root.p1.gotoAndStop(1);
_root.p1.p1standing.play();
}
}
onClipEvent (mouseDown) {
_root.attacking = true;
_root.p1.gotoAndPlay(3);
_root.p1.p1attacking.play();
}
Now this might of gotten messed up because the BBS does it. Now, this code basicly makes the Character rotate towards the mouse, and when you press keys it makes it move towards it. The rest is just variables that check what it is doing. Another makes the character attack when the mouse is clicked. I am not going to explain all of this, because it would take forever. Now, your main character should walk towards you mouse. If not, check all of your instance names to see if they are named correctly. Now onto enimies.
Part B-Enemies
Ok, draw your enemy, and make an animation of it walking/flying, ect.. Make this into a MC. Now, make an animation of it dying. I made it glow red, and then dissapear. At the last frame make an empty MC.Now on the first frame of that MC, add a stop(); action.
At the last frame of the MC add these actions-
stop();
_root.gold += Math.ceil(Math.random()*100);
(This will be used for later, dont worry about it now.)
Ok, now on the ouside of your MC, add these actions to it.
onClipEvent (enterFrame) {
if (this.hitTest(_root.p1.p1standing)) {
_root.health.nextFrame();
}
}
onClipEvent (enterFrame) {
if (this.hitTest(_root.p1.p1walking)) {
_root.health.nextFrame();
}
}
onClipEvent (enterFrame) {
if (_root.p1.p1attacking.sword.hitTest(this)) {
this.play();
}
}
onClipEvent (load) {
spd = 2;
}
onClipEvent (enterFrame) {
Xdiff = _parent.p1._x-_x;
Ydiff = _parent.p1._y-_y;
radAngle = Math.atan2(Ydiff, Xdiff);
_rotation = int((radAngle*360/(2*Math.PI))+90);
updateAfterEvent();
if (this.hitTest(_parent.p1)) {
} else {
if (_rotation>180) {
_y += (spd*Math.cos(Math.PI/180*_rotation));
_x -= (spd*Math.sin(Math.PI/180*_rotation));
} else {
_y -= (spd*Math.cos(Math.PI/180*_rotation));
_x += (spd*Math.sin(Math.PI/180*_rotation));
}
}
}
I cannot explain of of this as well, but it makes the enemy rotate towards the player, and then walking towards him/her. The rest just makes it so that when the enemy hits the player, the player loses health. The other stuff makes the enemy play when it is hit by the player attacking. I think this is it for the enemy.
Part C- Health
Make a health bar and make it a MC. Now make an animation of it going down to nothing. On the first frame add a stop(); action. On the last frame add these action _root.gotoAndStop("death"); change death to the name of the frame where you die. Now give the health bar the instance name of health. Thats it for the health bar.
Part D- Walls
Now this was the hard part. There was a problem, becuase the player and the enemies both needed to be stopped by walls, and I needed them both to be copy and pasteable. (which they are) Now, the walls are a little confusing to make. First, make a a drawing of your wall. Then, make 4 Mc's out of it. One on the top, one on the bottem, =and one on each side. Not add these actions to the the top part of the wall.
onClipEvent (enterFrame) {
for (i in _root) {
if (_root[i]._name.indexOf("enemy") != -1) {
if (_root[i].hitTest(this)) {
_root[i]._y -= 6;
}
}
}
}
onClipEvent (enterFrame) {
if (_root.p1.hitTest(this)) {
_root.p1._y -= 6;
}
}
Now because of space issues, I can't post all the codes for all the walls. You will have to change the codes your slef (the x and y) to make it work. I am out of space, so I will explain this code in the next post. Dont post!