I made a small example game as an answer to this post. Its pretty naive and you cannot die, but it shows how to do the things you are asking and is heavily commented.
Try it here:
http://chrisbenjaminsen.com/stuff/spacegame.swf
And download the source here:
http://chrisbenjaminsen.com/stuff/spacegame.fla
I really need help!
Chris said
a long time ago | Post #2
freetiger18 said
a long time ago | Post #3function onEnterFrame()
{
if (Key.isDown(39))
{
speedX = speedX + 1;
} // end if
if (Key.isDown(37))
{
speedX = speedX - 1;
} // end if
if (Key.isDown(40))
{
speedY = speedY + 1;
} // end if
if (Key.isDown(38))
{
speedY = speedY - 1;
} // end if
if (Key.isDown(32))
{
FireShot();
} // end if
speedX = speedX * 9.000000E-001;
speedY = speedY * 9.000000E-001;
ship._x = ship._x + speedX;
ship._y = ship._y + speedY;
ship._y = Math.min(Math.max(ship._y, 0), 500);
ship._x = Math.min(Math.max(ship._x, 0), 500);
for (var _loc4 = 0; _loc4 < stars.length; ++_loc4)
{
var _loc2 = stars[_loc4];
_loc2._y = _loc2._y + _loc2.size / 2;
if (_loc2._y > 500)
{
_loc2._x = Math.random() * 500;
_loc2._y = -_loc2.size;
} // end if
} // end of for
for (var _loc4 = 0; _loc4 < shots.length; ++_loc4)
{
_loc2 = shots[_loc4];
_loc2._x = _loc2._x - Math.cos(_loc2.angle) * _loc2.speed;
_loc2._y = _loc2._y - Math.sin(_loc2.angle) * _loc2.speed;
if (_loc2._y < 0 || _loc2._x < 0 || _loc2._y > 500 || _loc2._x > 500)
{
shots.splice(_loc4, 1);
_loc2.removeMovieClip();
} // end if
} // end of for
--shotDealy;
for (var _loc4 = 0; _loc4 < effects.length; ++_loc4)
{
var _loc1 = effects[_loc4];
_loc1._x = _loc1._x + Math.cos(_loc1.angle) * _loc1.size;
_loc1._y = _loc1._y + Math.sin(_loc1.angle) * _loc1.size;
_loc1.size = _loc1.size * 9.000000E-001;
_loc1._alpha = _loc1.life * 5;
if (--_loc1.life <= 0)
{
effects.splice(_loc4, 1);
_loc1.removeMovieClip();
} // end if
} // end of for
for (var _loc4 = 0; _loc4 < enemies.length; ++_loc4)
{
_loc1 = enemies[_loc4];
_loc1._y = _loc1._y + _loc1.speed;
if (_loc1._y > 550)
{
enemies.splice(_loc4, 1);
_loc1.removeMovieClip();
continue;
} // end if
for (var _loc3 = 0; _loc3 < shots.length; ++_loc3)
{
_loc2 = shots[_loc3];
var _loc6 = _loc1._x - _loc2._x;
var _loc5 = _loc1._y - _loc2._y;
var _loc7 = Math.sqrt(_loc6 * _loc6 + _loc5 * _loc5);
if (_loc7 < 15)
{
Explode(_loc2._x, _loc2._y, 5);
shots.splice(_loc3, 1);
_loc2.removeMovieClip();
if (--_loc1.life <= 0)
{
Explode(_loc1._x, _loc1._y, 10);
enemies.splice(_loc4, 1);
_loc1.removeMovieClip();
} // end if
} // end if
} // end of for
} // end of for
if (Math.random() < 1.000000E-002)
{
var _loc8 = enemiesContainer.getNextHighestDepth();
enemies.push(enemiesContainer.attachMovie("enemy", "enemy" + _loc8, _loc8, {_y: -30, _x: Math.random() * 500, speed: 2, life: 3}));
} // end if
} // End of the function
function FireShot()
{
if (shotDealy > 0)
{
return;
} // end if
var _loc2 = shotsContainer.getNextHighestDepth();
var _loc1 = shotsContainer.attachMovie("shot", "shot" + _loc2, _loc2, {_x: ship._x, _y: ship._y - 7, speed: 8, angle: 1.570796E+000});
_loc1._rotation = 5.729578E+001 * _loc1.angle;
shots.push(_loc1);
shotDealy = 5;
} // End of the function
function Explode(x, y, size)
{
for (var _loc2 = 0; _loc2 < size; ++_loc2)
{
var _loc1 = Math.random() * size;
var _loc3 = effectContainer.getNextHighestDepth();
effects.push(effectContainer.attachMovie("effect", "effect" + _loc3, _loc3, {_x: x, _y: y, _width: _loc1, _height: _loc1, size: _loc1, angle: Math.random() * 3.141593E+000 * 2, life: 20}));
} // end of for
} // End of the function
var stars = [];
var a = 0;
while (a < 100)
{
var size = Math.random() * 3;
stars.push(starContainer.attachMovie("star", "star" + a, a + 1, {_x: Math.random() * 500, _y: Math.random() * 500, _width: size, _height: size, size: size}));
++a;
} // end while
var shots = [];
var shotDealy = 0;
var enemies = [];
var effects = [];
var speedX = 0;
var speedY = 0;
Key.addListener({onKeyDown: function (e)
{
if (Key.getCode() == 32)
{
FireShot();
} // end if
}});
{
if (Key.isDown(39))
{
speedX = speedX + 1;
} // end if
if (Key.isDown(37))
{
speedX = speedX - 1;
} // end if
if (Key.isDown(40))
{
speedY = speedY + 1;
} // end if
if (Key.isDown(38))
{
speedY = speedY - 1;
} // end if
if (Key.isDown(32))
{
FireShot();
} // end if
speedX = speedX * 9.000000E-001;
speedY = speedY * 9.000000E-001;
ship._x = ship._x + speedX;
ship._y = ship._y + speedY;
ship._y = Math.min(Math.max(ship._y, 0), 500);
ship._x = Math.min(Math.max(ship._x, 0), 500);
for (var _loc4 = 0; _loc4 < stars.length; ++_loc4)
{
var _loc2 = stars[_loc4];
_loc2._y = _loc2._y + _loc2.size / 2;
if (_loc2._y > 500)
{
_loc2._x = Math.random() * 500;
_loc2._y = -_loc2.size;
} // end if
} // end of for
for (var _loc4 = 0; _loc4 < shots.length; ++_loc4)
{
_loc2 = shots[_loc4];
_loc2._x = _loc2._x - Math.cos(_loc2.angle) * _loc2.speed;
_loc2._y = _loc2._y - Math.sin(_loc2.angle) * _loc2.speed;
if (_loc2._y < 0 || _loc2._x < 0 || _loc2._y > 500 || _loc2._x > 500)
{
shots.splice(_loc4, 1);
_loc2.removeMovieClip();
} // end if
} // end of for
--shotDealy;
for (var _loc4 = 0; _loc4 < effects.length; ++_loc4)
{
var _loc1 = effects[_loc4];
_loc1._x = _loc1._x + Math.cos(_loc1.angle) * _loc1.size;
_loc1._y = _loc1._y + Math.sin(_loc1.angle) * _loc1.size;
_loc1.size = _loc1.size * 9.000000E-001;
_loc1._alpha = _loc1.life * 5;
if (--_loc1.life <= 0)
{
effects.splice(_loc4, 1);
_loc1.removeMovieClip();
} // end if
} // end of for
for (var _loc4 = 0; _loc4 < enemies.length; ++_loc4)
{
_loc1 = enemies[_loc4];
_loc1._y = _loc1._y + _loc1.speed;
if (_loc1._y > 550)
{
enemies.splice(_loc4, 1);
_loc1.removeMovieClip();
continue;
} // end if
for (var _loc3 = 0; _loc3 < shots.length; ++_loc3)
{
_loc2 = shots[_loc3];
var _loc6 = _loc1._x - _loc2._x;
var _loc5 = _loc1._y - _loc2._y;
var _loc7 = Math.sqrt(_loc6 * _loc6 + _loc5 * _loc5);
if (_loc7 < 15)
{
Explode(_loc2._x, _loc2._y, 5);
shots.splice(_loc3, 1);
_loc2.removeMovieClip();
if (--_loc1.life <= 0)
{
Explode(_loc1._x, _loc1._y, 10);
enemies.splice(_loc4, 1);
_loc1.removeMovieClip();
} // end if
} // end if
} // end of for
} // end of for
if (Math.random() < 1.000000E-002)
{
var _loc8 = enemiesContainer.getNextHighestDepth();
enemies.push(enemiesContainer.attachMovie("enemy", "enemy" + _loc8, _loc8, {_y: -30, _x: Math.random() * 500, speed: 2, life: 3}));
} // end if
} // End of the function
function FireShot()
{
if (shotDealy > 0)
{
return;
} // end if
var _loc2 = shotsContainer.getNextHighestDepth();
var _loc1 = shotsContainer.attachMovie("shot", "shot" + _loc2, _loc2, {_x: ship._x, _y: ship._y - 7, speed: 8, angle: 1.570796E+000});
_loc1._rotation = 5.729578E+001 * _loc1.angle;
shots.push(_loc1);
shotDealy = 5;
} // End of the function
function Explode(x, y, size)
{
for (var _loc2 = 0; _loc2 < size; ++_loc2)
{
var _loc1 = Math.random() * size;
var _loc3 = effectContainer.getNextHighestDepth();
effects.push(effectContainer.attachMovie("effect", "effect" + _loc3, _loc3, {_x: x, _y: y, _width: _loc1, _height: _loc1, size: _loc1, angle: Math.random() * 3.141593E+000 * 2, life: 20}));
} // end of for
} // End of the function
var stars = [];
var a = 0;
while (a < 100)
{
var size = Math.random() * 3;
stars.push(starContainer.attachMovie("star", "star" + a, a + 1, {_x: Math.random() * 500, _y: Math.random() * 500, _width: size, _height: size, size: size}));
++a;
} // end while
var shots = [];
var shotDealy = 0;
var enemies = [];
var effects = [];
var speedX = 0;
var speedY = 0;
Key.addListener({onKeyDown: function (e)
{
if (Key.getCode() == 32)
{
FireShot();
} // end if
}});
Chris said
a long time ago | Post #4 | in reply to #3Umm why waste time on decompiling the swf file, when I provided the sourcecode with comments?
freetiger18 said
a long time ago | Post #5Thank you but some codes in your .fla file is wrong.
Chris said
a long time ago | Post #6 | in reply to #5Ehh oki? O.o
blissj01 said
a long time ago | Post #7chris, those are some pretty sweet graphics. Im mid way through making a tank game, and the bullets fire towards the mouse using Math.atan2(), but the distance of the mouse affects how fast the bullets go. is there any way to remove this, EG, calculating a gradient by angle?
Chris said
a long time ago | Post #8 | in reply to #7Just give the bullets a fixed speed as I do in the above code?
freetiger18 said
a long time ago | Post #9Chris now I understand your codes at all and I can write my own codes!Thank you!
Post #10 deleted
Reply to thread
Sign up now to reply to threads
freetiger18 said
a long time ago | Post #11.
I treat the bullets as a object in the object plane,so I can only shoot 1 bullet at the same time and unluckily the bullet is rotated when the plane is rotated and it moves as the plane move.I wanna know how to let the bullets to be single-handed objects.
2.
How to make endless enemies?If I insert some objects,there will only exist limited enemies but in many games there exists unlimited enemies.How to make it?
See a example(problem #1):
http://www.nonoba.com/freetiger/fighter
And how the bullet works?Just 'hitTest'?How fast it should be set?