Question

I'm making a few game tests on flash recently, mostly with the purpose of improving my as3 skills.

Well, I've made a mouse-controlled character, a circle, which moves towards the direction the mouse is pointing when clicking. The character is fixed in the middle of the screen and it is the other environment that moves, like a kind of scrolling but with gravity.

Anyway, everything works fine except for the collision detection between the character and the walls, which kill him. The wall is a rectangle with tunnels inside (mostly a "maze") and the hero is in these tunnels, inside the walls' bounding box, so the traditional hitTest with bounding boxes won't work. With every method flash provides, the hitTest detection is constant, so the hero keeps dying again and again, resseting the values and making impossible to move around. So I used the Carey O'Neil CDK library, but the result is also a failure: whith any method I keep getting the same result.

I've debugged the code and the function works like it should, so I think it is only an issue of hitTesting. I haven't found anything even though I've looked for an answer for several hours, except for hitTest detection in nested movieclips and similar things which don't help very much.

Here's a fraction of my code in the Circle_mc class (the hero):

package{

    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.display.Sprite;
    import flash.ui.Keyboard;
    import flash.events.KeyboardEvent;
    import flash.display.Stage;

    import com.greensock.*;
    import com.greensock.easing.*;

    import com.coreyoneil.collision.CollisionList;

    public class Circle_mc extends MovieClip{

        /*Various variables declarations*/

        var myCollisionList:CollisionList;
        var collisions:Array;

        public function Circle_mc(){

            /*Various variables value assingment*/

            addEventListener(Event.ENTER_FRAME, action);
            addEventListener(Event.ADDED_TO_STAGE, init);


        }
        function init(e:Event){
            myCollisionList = new CollisionList(this);
            myCollisionList.addItem(Game.walls1);

            collisions = myCollisionList.checkCollisions();

            mySprite = new Sprite;
        }
        public function action(e:Event){

            this.rotation += Level_walls.x_vel_a;

            trailRestraint++;
            if(trailRestraint > 5 && ((Level_walls.x_vel_a > 1.1 || Level_walls.x_vel_a < -1.1) || (Level_walls.y_vel_a > 1.1 || Level_walls.y_vel_a < -1.1))){
                rotation_now = this.rotation;
                duplicateTrail();
            }
            if(collisions.length > 0){
                die();
            }
        }
        function duplicateTrail() {
            var xPosition:Number = this.x;
            var yPosition:Number = this.y;
            var scaleFactor:Number=1;
            var trail_unit:Trail = new Trail();
            stage.addChildAt(trail_unit, numChildren - 1);
            trail_unit.x=xPosition;
            trail_unit.y=yPosition;
            trail_unit.scaleX=scaleFactor;
            trail_unit.scaleY=scaleFactor;
            trail_unit.rotation = rotation_now;
        }
        function die(){
            Level_walls.x_vel_a = 0;
            Level_walls.y_vel_a = 0;
            Game.walls1.x = stage.stageWidth /2;
            Game.walls1.y = stage.stageHeight /2;
        }
    }

}

Well, Game.as is the Document Class, where I call most of the addChilds in the flash, so walls1 and circle or others are just instance names.

So, has anyone com across a similar problem? Any help/hint/piece of information would be very appreciated.

Thanks in advance!

Was it helpful?

Solution 2

Ok, I've solved it! I tested the CDK Method using CollisionList(), the hitTestObject Method, and the hitTestPoint Method with a regular shape (a rectangle in this case), and the results are all right with all of them (maybe CDK works a little better).

So, the problem had to be with the code. It was in fact a quite obvious error, but didn't realize of it until now. I've made some modifications in the code, and now everything works like a charm. In the action function I've added:

collisions = myCollisionList.checkCollisions();

if(collisions.length > 0){
    collisions = 0 as Array;
    die();
}

Thanks for the help.

OTHER TIPS

Another way off the top of my head is taking the maze and turning it into a grid of blocks programmatically in memory. Then, while you could hit test programmatically for each block, an even faster way might be to calculate only which blocks you need to check against based on the player's location within the maze, potentially reducing the number of tests down to a small fraction of the blocks needed.

I am also making an assumption that your maze will fit neatly in to a grid of some sort (ie: if you overlay a grid of 'n' square size on top, you won't get any half filled squares). If this is not true, it could still work, just that the grid would be made up of rectangles instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top