Talking about Worms game, Actionscript 3, Flash, Game development and Users contributions.
After another truck of Aspirin, Jordi Sanglas Molist is back with the 3rd step of his Worms tutorial.
I’ve finished part 3. I almost got a headache. Forgive me if there are some mistakes, I wrote this post and the comments quickly.
Now the character is affected by the explosions. To do that, I added an horizontal speed and a friction. However, I don’t want the character to slide when I’m using the arrow keys, so:
* The arrow keys won’t use horizontal speed
* If the horizontal speed is different than 0 (an explosion is moving the character), I can’t use the arrow keys
To calculate the impulse, we will need to use trigonometry:
I’ve also solved another bug: if the character was falling (without using the space bar), it could jump in the air. Here I removed the jumping variable and I check if the character is on the ground.
Pay attention!!! Sometimes I check if the character is ON the ground (character.y+10
) and sometimes I check if the character is TOUCHING the ground (character.y+9
).
KNOWN BUGS:
* The horizontal speed won’t decrease faster or more slowly if the character is on a slope.
* The for
loop isn’t exact when the horizontal speed is a decimal number (I think).
What should I do in the next part? I must do it before I start school, or I won’t have time.
While you think about the next part to request, here it is the script:
package {
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.BlendMode;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.geom.Matrix;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
public class worms extends Sprite {
public var terrain_bmpd:BitmapData=new BitmapData(550,200,true,0xFF00FF00);
public var terrain_bmp:Bitmap=new Bitmap(terrain_bmpd);
public var character:Sprite=new Sprite();
public var x_speed:Number=0;
public var y_speed:Number=0;
public var hole:Sprite=new Sprite();
public var hole_matrix:Matrix;
public var left_key:Boolean;
public var right_key:Boolean;
public var space_key:Boolean;
public var i:int;
public var x_diff:Number;//x_diff and y_diff will be the difference between the character and the explosion centre
public var y_diff:Number;
public var explosion_radius:Number=100;//That's the radius of the explosion. If the character is inside the radius, it will move away.
public var explosion_intensity:Number=10;//The explosion intensity is the speed (pixels/frame) that the character will
//get if the distance between the explosion and the character is 0 (the closest one).
public var distance:Number;//We will use that variable to store the distance between the character and the explosion
public var angle:Number;//and that one to store the angle
public var applied_speed:Number;//That will be the total speed we need to apply (the vector length).
public var applied_x:Number;//Then, we will separate the applied_speed in applied_x
public var applied_y:Number;//and applied_y
public var ground_friction:Number=0.2;//The ground has more friction than the air,
public var air_friction:Number=0.05;//so I have used different variables
public function worms() {
draw_objects();
stage.addEventListener(Event.ENTER_FRAME,move_character);
stage.addEventListener(MouseEvent.MOUSE_UP,mouse_up);
stage.addEventListener(KeyboardEvent.KEY_DOWN,key_down);
stage.addEventListener(KeyboardEvent.KEY_UP,key_up);
}
public function move_character(e:Event) {
if (left_key&&x_speed==0) {//If the player is moving, we won't be able to control it
for (i=0; i<3; i++) {
if (! terrain_bmpd.hitTest(new Point(terrain_bmp.x,terrain_bmp.y),0x01,new Rectangle(character.x-6,character.y-10,1,17))) {
character.x--;
while (terrain_bmpd.hitTest(new Point(terrain_bmp.x,terrain_bmp.y),0x01,new Rectangle(character.x-5,character.y+9,10,1))) {
character.y--;
}
}
}
}
if (right_key&&x_speed==0) {//Let's do the same with the right key...
for (i=0; i<3; i++) {
if (! terrain_bmpd.hitTest(new Point(terrain_bmp.x,terrain_bmp.y),0x01,new Rectangle(character.x+5,character.y-10,1,17))) {
character.x++;
while (terrain_bmpd.hitTest(new Point(terrain_bmp.x,terrain_bmp.y),0x01,new Rectangle(character.x-5,character.y+9,10,1))) {
character.y--;
}
}
}
}
if (x_speed<0) {//This block is similar; however, we don't know the speed,
for (i=0; i0) {
for (i=0; i character.y+10
character.y++;
} else {
y_speed=0;
}
}
} else {
for (i=0; i
And this is the result:
As usual, left and right to move the character, spacebar to make it jump and mouse click to create explosions.
Download the source code and don't forget to request the next part.
Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.