As3: Do not move through objects? How is the logic behind this?

advertisements

I have a function where I can control my character.

I also have a movieclip on the stage called assassin_table.

I want to make it so that the character can't move in the table, a.k.a make the table work like a wall.

I have this code:

            if(!this.hitTestObject(_root.assassin_table))
            {
                if(upKeyDown)
                {
                    gotoAndStop(4);
                    y-=Math.cos(rotation/-180*Math.PI)*(mainSpeed +7);
                    x-=Math.sin(rotation/-180*Math.PI)*(mainSpeed +7);

                }
                if(!upKeyDown)
                {
                    gotoAndStop(3);
                }
             }

However, if I touch the table, then I can't move at all.

I know it's because if(!this.hitTestObject(_root.assassin_table)), but I don't understand the logic behind NOT moving through objects. I'd much rather have a near pixel-perfect collision detection system, but since it's so hard to find any good info online which isn't confusing, I'll stick with hitTestObject for now :)

Thank you in advance!

EDIT: Tried something, didn't really work that well.

    if(!_root.assassinDead && !teleporting && this.currentFrame != 5)
        {
            if(this.hitbox.hitTestObject(_root.assassin_table))
            {
                _root.assassin_table.gotoAndStop(2);

                if(this.x > _root.assassin_table.x)
                {
                    trace("Can't move right");
                    canMoveRight = false;
                }

                if(this.x <_root.assassin_table.x)
                {
                    trace("Can't move left");
                    canMoveLeft = false;
                }

                if(this.y > _root.assassin_table.y)
                {
                    trace("Can't move up");
                    canMoveUp = false;
                }

                if(this.y < _root.assassin_table.y)
                {
                    trace("Can't move down");
                    canMoveDown = false;
                }
            }
            else
            {
                canMoveRight = true;
                canMoveLeft = true;
                canMoveUp = true;
                canMoveDown = true;
            }
        }

This causes me to sometimes be able to walk through the table. I figure it's because my character can move in essentially every possible angle (since he's always facing the mouse and there are no tiles/grids).

How would I make it so that it would work with the advanced movement I have?

Moving up runs this:

                    y-=Math.cos(rotation/-180*Math.PI)*(mainSpeed +7);
                    x-=Math.sin(rotation/-180*Math.PI)*(mainSpeed +7);

And the rotation is decided by this:

            this.rotation = Math.atan2((stage.mouseY - this.y), (stage.mouseX - this.x)) * 180/ Math.PI + 90;


You should seperate your hittest functions for four different moving directions. I mean, you shouldn't use this "hitTestObject" stuff, that only returns a boolean value "true" or "false", and that's not going to work for you.

You need a function like "testPoint(player.x, player.y);" and returns the object at the given position, so you can implement it for your game like that

   if (upKeyDown && testPoint(player.x, player.y - mainSpeed +7) == null) y-=Math.cos(rotation/-180*Math.PI)*(mainSpeed +7);

player.y - mainSpeed +7 // that checks for plus mainSpeed+7 because of trying to stop your player before it get stack inside object