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;
}