开发者

Checking for a collision between multiple dynamic instances in AS3

Okay, here's the rub. I'm making a little maze game. In this maze game, I have walls. They link to a class called Wall.as. In order to collision detect with the player, I do this:

MovieClip(parent).checkCollisonWithPlayer(this);

Where (parent) is a manager logic class. The parent does this

public function checkCollisonWithPlayer(wall:MovieClip)
    {

        if(player != null)
        {
            Collision.block(player,wall);
        }
    }

Now here's my problem. I have a class of Bullet.as that does much the same thing:

private function onEnterFrame(event:Event):void
    {
         x+= _vx;
         //Check for collisions with walls and enemies
         MovieClip(parent).checkCollisionWithEnemies(this);
         //Remove if it leads off the stage
        if (x > stage.stageWidth)
        {
            parent.removeChild(this);
        }
    }

What if I wanted to check for a collision between one of these bullets() and one of these walls()? How can I do that? They're never in the same function at the same time and there's almost certainly more than one of each in play at any time. I'm new to AS and this feels like a common issue, but I couldn;t dig up any material that made sense.

Here;s what I need:

开发者_开发问答
public function checkCollisonWithWall(wall:MovieClip,bullet:MovieClip)
{

    if(bullet.hitTestObject(wall))
        Collision.block(bullet,wall);
    }
}

But of course that doesn't work. What are my options?


On a simple level:

1) separate out physics from everything else: it makes things easier and more reusable.

2) have each physics object register itself with, say, a physics manager. things that you need to keep track of are if the object is static or dynamic (moving) and maybe even a shape flag (if you only wanted to collide certain objects with other objects). a shape flag is simply an int that tells you what type it is

3) have one onEnterFrame event, not an enter frame for each object. this will make your life easier

4) on this single onEnterFrame event, call on your physics manager to update the objects and see if any are colliding. You can to check collisions between static and dynamic objects, and (possibly) dynamic objects against other dynamic objects. You can use shape flags to cull out unnecessary tests. If you find a collision, you can dispatch an event/notify your objects/remove them from the game whatever.

something like

private function _init():void
{
    // create 4 walls
    for( var i:int = 0; i < 4, i++ )
    {
        var w:Wall = new Wall;

        // position and add to stage etc

        // add it to the physics manager (they don't need an update)
        this._physicsManager.addStatic( w, ShapeFlags.WALL );
    }

    // create 10 bullets
    for( i = 0; i < 10; i++ )
    {
        var b:Bullet = new Bullet;

        // position and add to stage etc

        // add it to the update and physics manager
        this._updateManager.add( b );
        this._physicsManager.addDynamic( b, ShapeFlags.BULLET ); // ShapeFlags is a static class of different types of objects
    }
}

private function _onEnterFrame( e:Event ):void
{
    // if you're moving your objects based on time, then
    // dt is "delta time", i.e., the amount of time that
    // has passed since the last update

    // you can have an update manager that will move your 
    // different objects according to their speed.
    this._updateManager.update( dt );

    // now your objects have moved to their new position,
    // get the physics manager (though in fairness, if it's
    // only doing collisions, then it should just be a 
    // collisions manager) to check for collisions
    this._physicsManager.update();

    // do whatever else
}

This is a very simple example. Here, we're moving objects, then checking for collisions. You could possible run into problems with objects that are moving too fast and passing through each other, so no collision is registered. Something like this:

// frame one: object a is heading towards object b
// and object b is heading towards object a
(a)->   <-(b)

// frame two: because of their speed, they pass through
// each other, and you don't see a collision
<-(b)   (a)->

This system is the easiest and simplest way to do this and is probably fine for a lot of games. If you want more complicated (as in checking for collisions between frames), then it's a bit more complicated, but you can still build on this.

Collision detection/response is a big topic, and you can find hundreds of code examples around the net. A good code structure can make the world of difference however.


It looks like the parent manager class has permanent references to the player and the enemies that it uses for collision detection. So also give it references to the walls and bullets.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜