1

I'm trying to make a character able to move within the "world" movieclip, but not go through the walls. So here is the character class, pretty basic. The moving variable dictates whether the character will move or not. The issue is, when I move the character left or right against the "world" the character will move upwards or downwards as well.

// Code to move character
package 
{
    import flash.display.*;
    import flash.events.*;
    import flash.ui.*;

    public class Character extends MovieClip
    {
        public var Moving:Boolean = true;
        public var maxSpeed = 10;
        public var dx = 0;
        public var dy = 0;
        public var rot = 0;
        public var rof = 30;
        private var keysDown:Array = new Array  ;
        // Declare variables:
        //var bullet:Bullet;
        var reload:int = 10;
        private var bullets:Array;


        public function Character()
        {
            //bullets = bulletList;
            // Initialize variables:

            addEventListener(Event.ADDED_TO_STAGE,init);
        }


        function init(e:Event):void
        {
            stage.addEventListener(KeyboardEvent.KEY_DOWN,keyPressed);
            stage.addEventListener(KeyboardEvent.KEY_UP,keyReleased);
            this.addEventListener(Event.ENTER_FRAME,processInput);
            this.addEventListener(Event.ENTER_FRAME,moveMe);
            removeEventListener(Event.ADDED_TO_STAGE,init);
        }

        public function removeT():void
        {
            stage.removeEventListener(KeyboardEvent.KEY_DOWN,keyPressed);
            stage.removeEventListener(KeyboardEvent.KEY_UP,keyReleased);
            this.removeEventListener(Event.ENTER_FRAME,processInput);
            this.removeEventListener(Event.ENTER_FRAME,moveMe);
        }






        private function keyPressed(e:KeyboardEvent):void
        {
            keysDown[e.keyCode] = true;
            // Handle keys pressed:
        }

        private function keyReleased(e:KeyboardEvent):void
        {
            keysDown[e.keyCode] = false;
        }

        private function processInput(e:Event)
        {
            // Handle keys held:
            dx = 0;
            dy = 0;

            if (keysDown[Keyboard.LEFT])
            {

                dx =  -  maxSpeed;
                dy = 0;
                rot = -180;
                this.gotoAndStop(4);
            }
            if (keysDown[Keyboard.RIGHT])
            {
                dx = maxSpeed;
                dy = 0;
                rot = 0;
                this.gotoAndStop(1);
            }
            if (keysDown[Keyboard.UP])
            {
                dx = 0;
                dy =  -  maxSpeed;
                rot = -90;
                this.gotoAndStop(3);
            }
            if (keysDown[Keyboard.DOWN])

            {
                this.gotoAndStop(2);
                dx = 0;
                dy = maxSpeed;
                rot = 90;
            }

        }

        private function moveMe(e:Event)
        {
            if(Moving){
            this.x +=  dx;
            this.y +=  dy;
            this.rotation = rot;
            }

             //stop if it tries to go off the screen
            if (this.x < 0)
            {
                this.x = 0;
            }
            if (this.x > stage.stageWidth)
            {
                this.x = stage.stageWidth;
            }
            if (this.y < 0)
            {
                this.y = 0;
            }
            if (this.y > stage.stageHeight)
            {
                this.y = stage.stageHeight;
            }
        }
    }
}

Here is the code for the collision checking of the object and the world. I use the functions checkFrom and checkFromX to check the points around the movieclip. So when one of those functions are checked, it iterates through all the points on that side of the movieclip character picture of the movieclip character if anyone is confused. http://prntscr.com/5fqu69

function checkFrom(x1:int, x2:int, y:int )
{
    if (x2<x1) {
        trace("x2<x1");
        return false;
    }
    for (var i=x1; i<=x2; i++)
    {
        if (world.hitTestPoint(i,y,true))
        {
            return true;
        }
    }
    return false;
}

//made this to check for right and left
function checkFromX(y1:int, y2:int, x:int )
{//y1 at top
    if (y2<y1) {
        trace("y2<y1");
        return false;
    }
    for (var a=y1; a<=y2; a++)
    {
        if (world.hitTestPoint(x,a,true))
        {
            return true;
        }
    }
    return false;
}







function moveDude(e:Event):void
{

    var obj:Object = e.target;

    if (obj.hitTestObject(world.lv1))
    {
        cleanup();
        gotoAndStop("donkeyKong");
    }
    else
    {



        obj.Moving = true;
        while (checkFrom(obj.x - obj.width / 2 + obj.width / 8,obj.x + obj.width / 2 - obj.width / 8,obj.y + obj.height / 2 - obj.height / 9))
        {//bot
            obj.y--;
            obj.Moving = false;

        }
        while (checkFrom(obj.x - obj.width / 2 + obj.width / 8,obj.x + obj.width / 2 - obj.width / 8,obj.y - obj.height / 2 + obj.height / 9))
        {

            obj.y++;
            obj.Moving = false;

        }
        while (checkFromX(obj.y - obj.height / 2 + obj.height / 7,obj.y - obj.height / 7 + obj.height / 2,obj.x - obj.width / 2 + obj.width / 9))
        {//left
            obj.x++;
            obj.Moving = false;

        }
        while (checkFromX(obj.y - obj.height / 2 + obj.height / 7,obj.y + obj.height / 2 - obj.height / 7,obj.x + obj.width / 2 - obj.width / 9))
        {
            obj.x--;
            obj.Moving = false;
        }
Trows
  • 391
  • 2
  • 12
  • Buddy, why not using simple hitTest? You can easily create objects for boundaries and use *single* hitTest instead of all that for loops.. And I didn't understand the 'go left goes up' thingy - please describe it better. – Andrey Popov Dec 12 '14 at 12:50
  • @AndreyPopov I'm kind of obligated to use this code, since this is a school project. The issue I'm having is when the character hits the object "world" when going up or down, the character stops because the hitTestPoint function iterates through all the points and detects a collision. However, when I collide with the right or left side of the "world" the character stops moving right or left, but instead will start moving up or down. – Trows Dec 12 '14 at 12:59
  • Well you haven't showed how you've linked the collision checker with the movement logic.. We cannot know why this happens :) – Andrey Popov Dec 12 '14 at 14:52

0 Answers0