0
votes

I'm creating simple flash game. I've added collisions for character with walls, stages, etc. But as I see for now collisions are with all objects which I add to stage. When I add background - character can't move (because is collision with background). How to avoid It?

Here is myCollisionList with character (Hero):

        var myCollisionList:CollisionList = new CollisionList(Hero);
        myCollisionList.addItem(stage1);
        myCollisionList.addItem(stage2);
        myCollisionList.addItem(stage3);

Here is part of code for moving character to left side. When added background I always get trace("Touching wall!"). How to avoid It?

     if(left){
                                Hero.x_speed = -walkspeed;
                                setDirection(1);

                                if(myCollisionList.checkCollisions().length > 0) { // checking if is anything in collision list

// I think here is problem, but don't know how to fix It?
             if (hitTestPoint(char.x - 26, char.y+20, true)){ //checking if character touching any object (have collision with anything)

                                    trace("Touching wall!");
                                    Hero.x_speed = 0;
                                }
                                else {
                                    Hero.x_speed = 8;   
                                }}

I've tried to use HitTestObject too (but if possible I need to use HitTestPoint)

if (Hero.HitTestObject(stage1 || stage2 || stage3)){
.....
}

But It works only with first stage1, for other 2 doesn't work.

1

1 Answers

0
votes

I guess your main class is a DisplayObject, otherwise

hitTestPoint(char.x - 26, char.y+20, true)

would throw an error, as it is testing the point against itself for hits. Since i guess your background is a children of this object, the pixels of the background collide with the point. Try calling

stage1.hitTestPoint(char.x - 26, char.y+20, true)

etc...

also

if (Hero.HitTestObject(stage1 || stage2 || stage3))

is incorrect, i think you are looking for

if (Hero.HitTestObject(stage1) || 
    Hero.HitTestObject(stage2) || 
    Hero.HitTestObject(stage3))