CREATING A BASIC BULLET
- Draw yours.
- Convert it to MOVIECLIP (F8) and assign an instance name (something like "bulletL", since the bullet we're making will move to the left.
- Put this code on it:
onClipEvent(load)
{
speedX = 5
}
onClipEvent(enterframe)
{
for (var i = 0; i < speedX; i++)
{
_x--;
}
}
What this code does:
-> speedX = 5 assigns a speed to the bullet;
-> the for {} sentence makes it move to the left. We do it this way for better collision detection.
Good job! You've just made a bullet moving to the left. Repeat the steps, change some stuff, and you'll have a bullet that moves to the right. The stuff you'll have to change is: the instance name to "bulletR" and, in the code, _x-- to _x++.
SHOOTING
On the main character who does the shooting, put this code:
onClipEvent (load)
{
i = 0
timer = 0
dir = "right"
}
onClipEvent(enterframe)
{
if (!Key.isDown(Key.CONTROL))
{
timer = 10
}
if (Key.isDown(Key.CONTROL))
{
if (timer >=10)
{
i++
if (dir == "left")
{
duplicateMovieClip("_root.bulletL", "bulletL" + i, i);
_root["bulletL" + i]._x = this._x
_root["bulletL" + i]._y = this._y - 20
_root["bulletL" + i]._visible = true
}
if (dir == "right")
{
duplicateMovieClip("_root.bulletR", "bulletR" + i, i);
_root["bulletR" + i]._x = this._x
_root["bulletR" + i]._y = this._y - 20
_root["bulletR" + i]._visible = true
}
timer = 0
}
timer++
}
}
Quite a lot, but it's easy to figure out.
The onClipEvent (load) {} statement sets the variables: "i" is the bullet counter, "timer" (I'll explain later) and "dir" which sets the direction of shooting.
In order to make this code work, where you make the character move left and right, make sure to include the "dir" variable.
Example:
if (key.isdown(key.left))
{
_root.character._x -= 10
dir = "left"
}
if (key.isdown(key.right))
{
_root.character._x += 10
dir = "right"
}
Inside the onClipEvent(enterframe), the code:
if (dir == "left")
{
duplicateMovieClip("_root.bulletLl", "bulletL" + i, i);
_root["bulletL" + i]._x = this._x
_root["bulletL" + i]._y = this._y - 20
_root["bulletL" + i]._visible = true
}
checks the direction of shooting, then the bullet's movieclip is duplicated and its position on the stage is set. Also the bullet is made visible. There's a similar statement for the "right" direction.
The code:
if (!Key.isDown(Key.CONTROL))
{
timer = 10
}
sets the timer. The timer puts an interval between the bullets, so instead of this: , you get this:
The bigger the number you put in place of 10, the bigger the gap between the bullets.
If the user isn't pressing the CTRL button, the timer is automatically set to 10, so that the next time the user presses the button, the timer won't have to count before a bullet is released.
Finally, the code:
if (Key.isDown(Key.CONTROL))
{
if (timer >=10)
{
...
...
timer = 0
}
timer++
}
basically sets the timer to zero after duplicating a bullet, and if the user is holding down the CTRL key, it forces the timer to count to 10 before another bullet can be duplicated. |