PDA

View Full Version : Help with shooter game (Flash8)



Dancso
June 24th, 2007, 05:15 PM
Hi,

I've been making a doom shooter game in my free time, it's top-down view, and i have the shooting system like i have a shooter button that shoots when the player presses it. There are zombies which the player must kill, but i have the problem, that i can't find a way to remove the bullet, when it hits the zombie. (so the bullet won't "fly" above/through the zombie.)

I know about the method of using bullet.removeMovieClip(); but it only works, if there's only one bullet on the screen, and the problem is that i want to have more weapons, some of them with a fast shoot-rate, so this way wouldn't work.

I also tought about scripting the bullet movieclip itself, but then it would be annoying, because i would need to set an instance name to all the enemies, and script it for all of them, so this isnt the right way either.

Here's the shooter button's code:

on(press)
{
_root.player.body.wep.weapon.play()
var bullet:MovieClip = _root.attachMovie("bullet", "bullet_"+_root.getNextHighestDepth(), _root.getNextHighestDepth());
var point = {x:player.bullet_spawn._x, y:player.bullet_spawn._y};
player.localToGlobal(point);

// now use point.x and point.y as coordinates for bullet in conjunction with the rotation you just gave it.
bullet._x = point.x;
bullet._y = point.y;
bullet._rotation = player._rotation
bullet.onEnterFrame = function() {
this._x += Math.cos(this._rotation*Math.PI/180)*_root.bulletSpeed;
this._y += Math.sin(this._rotation*Math.PI/180)*_root.bulletSpeed;
if (_root.wall.hitTest(this._x, this._y, true)) {
this.removeMovieClip();
}
}
}Any ideas how i could do it?

Dwight
June 25th, 2007, 11:03 AM
ActionScript Code:

if (_root.wall.hitTest(this._x, this._y, true)) {
this.removeMovieClip();
} else if (_root.zombie.hitTest(this._x, this._y, true)) {
this.removeMovieClip();
}





Something like this?

Dancso
July 15th, 2007, 06:44 PM
Yes, that would be good, but only one problem:

If there are multiple zombies with the same instance name, it just doesnt work, or only work with one, the first zombie.

I just can't code it for all the zombies, there are too much of them.

By the way, any ideas on how could i make it so a movieclip faces another one? I've made it so the placer faces the mouse cursor, but how can i do it for another movieclip? Here is the code for facing the mouse:

_root.player._rotation += Math.atan2(_root.player._ymouse, _root.player._xmouse)*180/Math.PI;

Aquilonian
July 16th, 2007, 05:24 AM
so put all the zombies in a array and loop on that array

Dancso
July 16th, 2007, 04:21 PM
so put all the zombies in a array and loop on that array

I dont quite understand that.
How do i put movieclips in an array?

rahul_7star
July 17th, 2007, 12:25 AM
well use flag concept in order to get rid of
when bullet is on (make flag=1) n when it hit d target den in collision code make it flag=0
and when d loop finishes make it again =0
dis will definately work
i can send u fla if u need



Hi,

I've been making a doom shooter game in my free time, it's top-down view, and i have the shooting system like i have a shooter button that shoots when the player presses it. There are zombies which the player must kill, but i have the problem, that i can't find a way to remove the bullet, when it hits the zombie. (so the bullet won't "fly" above/through the zombie.)

I know about the method of using bullet.removeMovieClip(); but it only works, if there's only one bullet on the screen, and the problem is that i want to have more weapons, some of them with a fast shoot-rate, so this way wouldn't work.

I also tought about scripting the bullet movieclip itself, but then it would be annoying, because i would need to set an instance name to all the enemies, and script it for all of them, so this isnt the right way either.

Here's the shooter button's code:

on(press)
{
_root.player.body.wep.weapon.play()
var bullet:MovieClip = _root.attachMovie("bullet", "bullet_"+_root.getNextHighestDepth(), _root.getNextHighestDepth());
var point = {x:player.bullet_spawn._x, y:player.bullet_spawn._y};
player.localToGlobal(point);

// now use point.x and point.y as coordinates for bullet in conjunction with the rotation you just gave it.
bullet._x = point.x;
bullet._y = point.y;
bullet._rotation = player._rotation
bullet.onEnterFrame = function() {
this._x += Math.cos(this._rotation*Math.PI/180)*_root.bulletSpeed;
this._y += Math.sin(this._rotation*Math.PI/180)*_root.bulletSpeed;
if (_root.wall.hitTest(this._x, this._y, true)) {
this.removeMovieClip();
}
}
}Any ideas how i could do it?

Dancso
July 17th, 2007, 07:54 PM
Yes, i'd like an example fla, please.

Charleh
July 18th, 2007, 07:37 AM
I'd say if you want the game to have a few types of bad guys and weapons - use classes to define your objects and you should have no problems

Dancso
July 28th, 2007, 06:47 AM
I'm more a begginer than you might think... I actually have no idea how to do any of those you guys said... If you could explain me an example way on how to do it please?

Charleh
July 28th, 2007, 08:06 AM
I'll do a tut later - right now I've got to go to the shops to exchange a faulty phone and fix my bed as me and my gf broke it :P (probably not in the way you'd expect though..)

Dancso
July 28th, 2007, 09:14 AM
I'll do a tut later - right now I've got to go to the shops to exchange a faulty phone and fix my bed as me and my gf broke it :P (probably not in the way you'd expect though..)Rofl!:D uhh.. sorry :D

Btw is there any way to completely remove the right-click menu, so i could make use of the right mouse button in my game?

Charleh
July 28th, 2007, 09:30 AM
Rofl!:D uhh.. sorry :D

Btw is there any way to completely remove the right-click menu, so i could make use of the right mouse button in my game?

Don't think so - gonna do a quick tut for you :P

Charleh
July 28th, 2007, 10:25 AM
Don't think so - gonna do a quick tut for you :P

Ok it's not really a 'shooter' tutorial, more of a demonstration of a very simple game engine and an example of classes and inheritance - open it up and I'll explain what it all does

You've got a FLA file and a few classes -

Here's a quick description of the classes

Vector.as:

Provides a 2D vector handling class (not much in here for this example, but you can add a lot to it as you can do a lot with vectors :P)

BaseObject.as:

The basic game object which provides a simple set of functions for physics, rendering and behaviour - as well as automatically handling addition and removal of objects to the game loop list

ObjectList.as:

Provides a list of all active objects in the game and handles the main loop for updating game objects. Also handles removal of game objects and movieclips after each frame

Player.as:

A quick class which inherits from BaseObject which provides some key handling behaviour - press space to drop a jiggler

Jiggler.as:

Another quick class to show different behaviour from the same base code - it explodes in a shower of particles after a short time

Particle.as:

Rudimentary particle handling based on BaseObject

----------------------------------------

Have a look at the classes and try and work out what's going on - I'll explain if you get stuck

Notice: the only code is in these classes and on frame 1 of the root timeline... tiny isn't it?

Dancso
July 28th, 2007, 11:12 AM
I kinda understand it now... so.. this makes it so if i spawn more than one bullet, not only the last will work, but all of them even if spawned at same/almost same time?

olliecozza
July 28th, 2007, 12:00 PM
Don't think so - gonna do a quick tut for you :P

You can change the right click menu to show different things... for example, right click > switch to burst fire, etc...

if you want to know how just ask me.

Charleh
July 28th, 2007, 12:04 PM
I kinda understand it now... so.. this makes it so if i spawn more than one bullet, not only the last will work, but all of them even if spawned at same/almost same time?

Well that amongst other things - have a look at how it's all built - look at the code of Jiggler

Notice that there's hardly any code in Jiggler.as - how does Jiggler know how to move and draw and update?

Basically Jiggler and other classes 'extend' BaseObject.

When you extend a class the new class gets all the properties and behaviour of the class it's extending - then you can write code on top. So if you wanted to change all your objects to be twice the size, all you'd need to do is go into 'BaseObject' and set the _yscale and _xscale to 200 - this would affect all classes based on BaseObject - this is called inheritance

You can also override BaseObject - so if you wanted the new object to Render in a different way you could simply write a new Render function in the new object and this function would be run for that object instead of the BaseObject's original render function

Look in Jiggler.as - you can see that Behaviour() function is declared in there - nothing else, but Jiggler still has all the properties of BaseObject - except for Behaviour() which is different from BaseObjects Behaviour()

Notice that I call super.Behaviour() in Jigglers Behaviour function. This runs the Behaviour function from BaseObject - so you can still have all the behaviour from your parent objects and build on it in subclasses.

It means you can concentrate on adding behaviour instead of recoding bits of the game again and again for different objects.

The 'game' basically runs on a loop every frame. It uses the class ObjectList (oList) in the _root timeline to hold a list of game objects, and it runs through this list by calling oList.HandleObjects(); and oList.CleanUp(); every frame.

The HandleObjects function loops through each object in the list and runs it's Run() function and the CleanUp function just deletes objects which have been marked for removal (you don't want to remove objects during a frame update as it takes the object list out of order and some things may get missed in the update loop).

Each object must be based on BaseObject for this to work - so all your classes must extend this. It's possible to have seperate lists for particles/walls/objects/etc but most Flash games don't get anywhere near complicated enough to warrant this. Most of the slowdown in my game is due to rendering and lack of optimisation (I've got a lot of curves on some objects so I'm converting most of them to bitmaps).

Charleh
July 29th, 2007, 05:39 PM
You can change the right click menu to show different things... for example, right click > switch to burst fire, etc...

if you want to know how just ask me.

Yeah you can customise the menu but you can't disable the thing entirely as far as I know :P

Charleh
July 31st, 2007, 05:53 PM
Yeah you can customise the menu but you can't disable the thing entirely as far as I know :P

Heres a bit more on this tutorial style thing - now you can shoot at the mouse position with space and hit the Jigglers and make them blow up :P

Check out the class files :)

Debug is on - it will draw the bounding boxes for each object - you can turn it off by uncommenting the line with oList.Debug = false; in it

I also added gravity to the particles to show how thats done