PDA

View Full Version : bullet-target interaction



xytor
February 11th, 2007, 05:17 PM
I'm wondering if there is a more efficient way of creating bullets and their interaction with targets than the example below.
Bullet is created from here:

onClipEvent (enterFrame) {
if (Key.isDown(Key.SPACE)) {
var b = _root.attachMovie("Bullet", "Bullet", _root.getNextHighestDepth(), {_x:this._x, _y:this._y});
b.onEnterFrame = function() {
this._y -= 40;
};
}
}


The target deals with the bullet like this:

onClipEvent(enterFrame){
if(this.hitTest(_root.Bullet)){
removeMovieClip(_root.Bullet);
}
}



This sample code seems simple, but when you get into multiple targets, and explosions on impact, the bullets start to go through targets, especially if the target is dynamically created... among other problems.

There must be a better way.

DangerousDan
February 11th, 2007, 05:32 PM
I'm pretty sure on the target hitTest you're only looking at one bullet, you'd have to save the depth of the bullet in a variable and do something like check _root["Bullet"+depth] or something, been a while since I worked with Actionscript :shifty:

xytor
February 11th, 2007, 06:50 PM
Yes, but lets say you have 5 different types of bullets and 5 different types of targets.

Do you tell every bullet about the 5 different types of targets, or do you tell every target about the 5 different types of bullets?

Is there something like polymorphism in flash which i can make a "bullet" class that is the parent to all the types of bullet, and in the target i can say, if you are hit by ANY type of the bullet class, do something?

NiñoScript
February 11th, 2007, 10:55 PM
I guess you can do that polimorphism thing with classes.

bombsledder
February 11th, 2007, 11:32 PM
heres an example



class A{
function A(){}
}




class B extends A{
function B(){}
}




class C extends A{
function C(){}
}




class D extends B{
function D(){}
}




var classA = new A()
var classB = new B()
var classC = new C()
trace (classA instanceof A) //true
trace (classB instanceof A) //true
trace (classC instanceof A) //true
trace (classC instanceof B) //false class C and B are in the same hierarchy <- or ever how you spell it and are instances of Classes A, and of its own class
trace (classD instanceof B) // true
trace (classD instanceof A) // true



dont feel like explaining =P

xytor
February 12th, 2007, 02:14 PM
i understand the concept of polymorphism and classes perfectly, bombsledder. But how do i transform those classes into actual mc's? How do I do hit tests with classes.
Do I say something like


if(this.hitTest(instanceof A)){
do something;
}

that doesn't really make sense...

I guess I am confused about how to use classes in a flash program, and integrate them with actual mc's.

xytor
February 12th, 2007, 04:28 PM
disregard my previous message, it is stupid.
But I do have another problem.
I started making the game engine, and I ran into a big problem. Here is the code on the main frame:

var pProjectiles:Array;
var eProjectiles:Array;
var enemies:Array;
class Projectile{
var Name:String;
var damage:Number;
var depth:Number;
var X:Number;
var Y:Number;
public function Projectile(n,dam,X,Y){
Name=n;
damage=dam;
depth=_root.getNextHighestDepth();
this.X=X;
this.Y=Y;
P=_root.attachMovie(Name,Name+"_"+depth,depth,{_x:X,_y:Y});
P.onEnterFrame=function(){
this._y+=15;
}

}
}
class Enemy{
var life:Number;
function shoot(projectile){

}
}

now when I went to test this, it told me that I had to define classes somewhere outside. What does this mean? where do i define the classes?