Wednesday 19 December 2012

character moves using stage clicks in as3




var iswalking = false;
var goX = char.x;
var goY = char.y;
var movespeed = 5;
var dir = "down";
stage.addEventListener(Event.ENTER_FRAME, loop);
function loop(Event)
{// animation handling
if (iswalking == true)
{
char.w.play();
}
else
{
char.w.gotoAndStop(1);
}// direction handling
char.gotoAndStop(dir);// movement handling
if ((goY-movespeed)>char.y)
{
char.y +=  movespeed;
dir = "down";
iswalking = true;
}
else if ((goY+movespeed)<char.y)
{
char.y -=  movespeed;
dir = "up";
iswalking = true;
}
else if ((goX-movespeed)>char.x)
{
char.x +=  movespeed;
dir = "right";
iswalking = true;
}
else if ((goX+movespeed)<char.x)
{
char.x -=  movespeed;
dir = "left";
iswalking = true;
}
else
{
iswalking = false;
}
}
bg.addEventListener(MouseEvent.CLICK, setposition);
//bg.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
//bg.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);


function setposition(event:MouseEvent):void
{
goX = mouseX;
goY = mouseY;
}

function mouseDownHandler(event:MouseEvent):void
{
bg.startDrag(true);
bg.x=mouseX;
    bg.y=mouseY;
}
function mouseUpHandler(event:MouseEvent):void
{
bg.stopDrag();
//bg.removeEventListener(MouseEvent.CLICK, setposition);
}

Animation : -

1. create character frames front, back, left and right.
2. create a background with the instance name of 'bg'

No comments:

Post a Comment