This Blog contains About Action script 3.0 Variables, Data types, Access specifies, Keywords, Operators, Conditional statements, Control statements, Arrays (Indexed, Associative, Multidimensional, Cloning), Functions, Symbols, Movie clips (Transformation point, Registration point, Loading images), Buttons, Events (Key board, Mouse, Timer, XML)Pre- defined function :- Random, Hit test object, Drag and drop, Sort, Loader, Tween,URL Loader, Video components, Audio Components, Sound Channel.
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'
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment