Something was definitely eating me, Ever since I released part 3a-“the complete prototype” I had been thinking why I didn’t explain what I’ve done. Anyways I guess its time I did. Here’s the code for you again for reference.
- stop();
- xspeed = 0;
- yspeed = 0;
- friction = 0.8;
- turntest = 0;
- shotcount = 0;
- shottime = 10;
- motionspeed = 1;
- xmoved = 0;
- mlcounter = 0;
- misslecount = 0;
- xdist = new Array(20, 250, 260, 550, 560, 570, 850, 860, 870, 880, 1250, 1260, 1270, 1280, 1290, 1550, 1560);
- xplace = new Array(100, 410, 110, 380, 220, 125, 260, 300, 110, 350, 155, 300, 205, 65, 370, 105, 210);
- _root.attachMovie("back1", "back1", _root.getNextHighestDepth(), {_x:250, _y:0});
- _root.attachMovie("back2", "back2", _root.getNextHighestDepth(), {_x:250, _y:-400});
- _root.createEmptyMovieClip("shotmovie", _root.getNextHighestDepth());
- _root.createEmptyMovieClip("enemies", _root.getNextHighestDepth());
- _root.createEmptyMovieClip("bonusmovie", _root.getNextHighestDepth());
- _root.createEmptyMovieClip("missles", _root.getNextHighestDepth());
- _root.attachMovie("ship", "ship", _root.getNextHighestDepth(), {_x:250, _y:400});
- ship.onEnterFrame = function() {
- _root.shipmover();
- if (xmoved>1600) {
- xmoved = 0;
- }
- _root.shooter();
- _root.mover();
- _root.renderer();
- _root.hitenemy();
- };
- function shipmover() {
- ship._x += xspeed;
- ship._y += yspeed;
- if (ship._x<10) {
- ship._x = 10;
- }
- if (ship._x>490) {
- ship._x = 490;
- }
- if (ship._y<40) {
- ship._y = 40;
- }
- if (ship._y>390) {
- ship._y = 390;
- }
- xspeed = xspeed*friction;
- yspeed = yspeed*friction;
- if (Key.isDown(Key.UP)) {
- yspeed--;
- }
- if (Key.isDown(Key.DOWN)) {
- yspeed++;
- }
- if (Key.isDown(Key.LEFT)) {
- turntest++;
- xspeed--;
- }
- if (Key.isDown(Key.RIGHT)) {
- turntest--;
- xspeed++;
- }
- if (turntest != 0) {
- ship._width = 35;
- } else {
- ship._width = 40;
- }
- turntest = 0;
- }
- function mover() {
- back1._y += motionspeed;
- back2._y += motionspeed;
- if (back1._y>=400) {
- back1._y = -400;
- }
- if (back2._y>=400) {
- back2._y = -400;
- }
- xmoved++;
- }
- function shooter() {
- shottime--;
- if (shottime == 0) {
- if (shotcount == 7) {
- shotcount = 0;
- }
- shottime = 10;
- shot = shotmovie.attachMovie("shot", "shot"+shotcount, shotmovie.getNextHighestDepth(), {_x:ship._x, _y:ship._y-14});
- shotcount++;
- shot.speed = 8;
- shot.onEnterFrame = function() {
- this._y -= this.speed;
- if (this._y<0) {
- this.removeMovieClip();
- }
- };
- }
- }
- function renderer() {
- for (i=0; i<17; i++) {
- if (xmoved == xdist[i]) {
- enemy = enemies.attachMovie("mlauncher", "mlauncher"+mlcounter, enemies.getNextHighestDepth(), {_x:xplace[i], _y:0});
- enemy.health = 5;
- enemy.shottime = 20;
- enemy.onEnterFrame = function() {
- myRadians = Math.atan2(ship._y-this._y, ship._x-this._x);
- myDegrees = Math.round((myRadians*180/Math.PI));
- _root.yChange = Math.round(ship._y-this._y);
- _root.xChange = Math.round(ship._x-this._x);
- this._rotation = myDegrees+90;
- this.shottime--;
- if (this.shottime == 0) {
- this.shottime = 40;
- missle = missles.attachMovie("missle", "missle"+misslecount, missles.getNextHighestDepth(), {_x:this._x, _y:this._y});
- misslecount++;
- if (misslecount>15) {
- misslecount = 0;
- }
- if (ship._x>=this._x) {
- missle.xspeed = (5*(this._x-ship._x)*(this._x-ship._x))/(((this._x-ship._x)*(this._x-ship._x))+((this._y-ship._y)*(this._y-ship._y)));
- } else {
- missle.xspeed = 0-(5*(this._x-ship._x)*(this._x-ship._x))/(((this._x-ship._x)*(this._x-ship._x))+((this._y-ship._y)*(this._y-ship._y)));
- }
- if (ship._y>=this._y) {
- missle.yspeed = (5*(this._y-ship._y)*(this._y-ship._y))/(((this._x-ship._x)*(this._x-ship._x))+((this._y-ship._y)*(this._y-ship._y)));
- } else {
- missle.yspeed = 0-(5*(this._y-ship._y)*(this._y-ship._y))/(((this._x-ship._x)*(this._x-ship._x))+((this._y-ship._y)*(this._y-ship._y)));
- }
- missle.onEnterFrame = function() {
- this._x += this.xspeed;
- this._y += this.yspeed;
- if (this.hitTest(ship)) {
- ship.gotoAndPlay(2);
- this.removeMovieClip();
- }
- if (this._x<0 or this._x>500 or this._y<0 or this._y>400) {
- this.removeMovieClip();
- }
- };
- }
- this._y += motionspeed;
- if (this._y>400) {
- this.removeMovieClip();
- }
- };
- mlcounter++;
- }
- }
- if (mlcounter == 9) {
- mlcounter = 0;
- }
- }
- function hitenemy() {
- for (i=0; i<9; i++) {
- for (j=0; j<7; j++) {
- if (shotmovie["shot"+j].hitTest(enemies["mlauncher"+i])) {
- shotmovie["shot"+j].removeMovieClip();
- enemies["mlauncher"+i].health--;
- if (enemies["mlauncher"+i].health>0) {
- enemies["mlauncher"+i].gotoAndPlay(2);
- } else {
- for (k=0; k<Math.random()*8; k++) {
- star = bonusmovie.attachMovie("bonus", "bonus"+bonusmovie.getNextHighestDepth(), bonusmovie.getNextHighestDepth(), {_x:((enemies["mlauncher"+i]._x+Math.random()*20)-10), _y:((enemies["mlauncher"+i]._y+Math.random()*20)-10)});
- star.xspeed = (Math.random())-0.5;
- star.yspeed = motionspeed;
- star.onEnterFrame = function() {
- this._x += this.xspeed;
- this._y += this.yspeed;
- if (this.hitTest(ship)) {
- this.removeMovieClip();
- }
- };
- }
- enemies["mlauncher"+i].removeMovieClip();
- }
- }
- }
- }
- }
Anyways here’s the explaination
1. I stop the current frame
2. xspeed is the variable controlling the horizontal motion of the ship
3. yspeed is the variable controlling the vertical motion of the ship
4. friction as always is the stopping force applied
5. turntest is a Boolean checking for horizontal motion, I use it for some animation effects.
6. shotcount is a counter variable counting the number of shots on screen
shottime induces a gap in two shots.. basically a delay variable.
8. motionspeed is the speed of the scrolling background.
9. xmoved, I’ve used this variable to see how long the game has gone in order to spawn enemies at the right locations.
10. mlcounter is a countervariable for the number of missile launcher’s onscreen.
11. misslecount is another counter variable that’s counting the number of enemy missiles on screen.
12. xdist is an array, an enemy spawns whenever the ship has traveled the distance equal to an xdist value. (ie. If xmoved = xdist(i) then spawn enemy)
13. xplace is the x co-ordinate of a new missle launcher, its also an array. (could have made it random but I used an array)
14. attached the background scrolling screen 1
15. attached the other part of the background just above the first screen
16. created an empty movie called the shotmovie which will contain all instances of shots
17. created an empty movie called the enemies which will contain all instances of enemies
18. created an empty movie called the bonusmovie which will contain all bonuses
19. created an empty movie called missles which will contain all instances of missiles
20. attached the ship on screen and made it ship
21. the onenterframe function: this function executes each frame
22. called the function shipmover:
Explanation of shipmover function
31. The definition of the shipmover function starts here.
32. increase ship._x according to the xpseed value
33. increase the ship._y according to yspeed value
34 to 45 : Stopping the ship from going of screen borders.
46 and 47. Lowering the x and y speeds according to friction.
48 to 53: changing the yspeed variable according to up and down arrow key presses
54 to 61: Doing the same with xspeed, but also changing the turntest value so as to detect horizontal motion
62 to 66: animating horizontal motion if detected in previous section
67. Changing turntest back to 0
23 to 25: Resetting the xmoved value, this is essential for my prototype as I have only set a limited number of xdist and xplace array values.
26. Calling the shooter function
Explanation of shooter function
80. The definition of shooter function starts here
81. Decrementing the shottime.
82. This if block runs every time shottime becomes zero
83 to 85: If shotcount reaches seven it’s reset.
86. Shottime reset to 10
87. created a new movie called shot[i] where i is the value of the shotcount, and attached it to the empty movieclip shots.
88. Incremented shotcount
89. declared shot speed = 8
90. The function executed in this instance of shot everytime it enters a frame
91. Its y co-ordinate is changed according to its speed.
92 to 94. If the shot moves off screen, its removed.
27. Called the mover function
Explanation of the mover function.
70 & 71 : Both back1 and back2 are moved down.
72 to 77 : If either of back1 or back2 moves below the frame height its moved right back on top.
78. xmoved is incremented.
28. Called the renderer function
Explanation of the renderer function
98. The renderer function’s definition begins.
99. Running a loop from 0 to 17 (the number of items in the arrays xdist and xplace)
100. if xmoved matches with any element of array xdist this block is executed
101. Place a new enemy at a position specified in the xplace array.
102. Specifies the health of this enemy
103. Specifies the delay time between its shots.
104. The main function of the enemy that is executed on each frame.
105 to 109: Used for calculating the direction of facing of the launcher so it rotates around to face the ship
110. Decrement the shot time.
111. This block executes everytime the shottime reaches zero.
112. Reset the shot time to 40.
113. Attach a new movie missle(i) where I is the value of misslecount variable to the empty movieclip we created for missiles.
114. Increment misslecount variable
115 to 117: Resetting the misslecount variable if it gets too high.
118 to 127: Calculating the x and y speeds of the missile so that it goes towards the ship rather than in a weird direction. If you can’t understand this there are a lot of tutorials out there. If you still can’t do it you have my email address. Post a comment here and I’ll get back to you.
128. The main function of the missle that gets executed every frame
129 and 130: Change the co-ordinates according to speed
131 to 134: Same old hit test… if missle touches the ship, it disappears and the ship moves to its frame 2 for a fraction of a second where I’ve changed the colors so it looks like a flash.
135 to 137: if the missle goes out of bounds it is removed.
140. This moves the missle launcher down along with the scrolling background.
141 to 143: Removing the launcher if it goes out of bounds.
145 to 150. Incrementing the misslelauncher counter variable and resetting it if it gets too high.
29. Called the hitenemy function
Explanation of the hitenemy function
153. Running loop from 0 to 9, that’s the bounds for the mlcounter that I’ve set.
154. Nesting another loop from 0 to 7 that is the bounds for the shotcounts..
155 to 160: Basically checks if any of the shots on screen are hitting any of the misslelaunchers on the screen. If so the shots are removed and the missle launcher blinks if its health is greater than zero.
161 to 171: If the health of a missle launcher is less than zero I run a loop for a random number of times where a bonus star is placed where the missle launcher was and it moves in a random direction very slowly.
If this bonus star hits the ship… you can count a bonus score and the star disappears. Also the missle launcher disappears.
That’s all folks… Hope you understood everything.
Be sure to check back in on the blog soon, I’m going to start a new tutorial series.
