Skip to main content
Participating Frequently
August 13, 2009
Question

change the pic of instance

  • August 13, 2009
  • 1 reply
  • 769 views

hi

i make this code that move instance in 8 direction

but i used rotation to change the direction of instance

but now i went to change the pic like this pic in attach

or

http://img43.imageshack.us/img43/5909/alexchar.png

and the code

var up:Boolean = false;
var down:Boolean = false;
var left:Boolean = false;
var right:Boolean = false;


//create the function main.
function Main(event:Event){
    check_move();
    change_opg();
}

function prease_key (e:KeyboardEvent)
{
    if(e.keyCode==37)
    {
        left = true;
    }
    if(e.keyCode==39)
    {
        right = true;
    }
    if(e.keyCode==38)
    {
        up = true;
    }
    if(e.keyCode==40)
    {
        down = true;
    }
}

function resave_key (e:KeyboardEvent)
{
    if(e.keyCode==37)
    {
        left = false;
    }
    if(e.keyCode==39)
    {
        right = false;
    }
    if(e.keyCode==38)
    {
        up = false;
    }
    if(e.keyCode==40)
    {
        down = false;
    }
}
function check_move()
{
    if(left)
    {
        m1.x -=5;
    }
    if(right)
    {
        m1.x +=5;
    }
    if(up)
    {
        m1.y -=5;
    }
    if(down)
    {
        m1.y +=5;
    }
}
function change_opg()
{
    if(left)
    {
        if(up)
        {
            m1.rotation =315;
        }
        else if(down)
        {
            m1.rotation =225;
        }
        else
        {
            m1.rotation =270;
        }
       
    }   
    if(right)
    {
        if(up)
        {
            m1.rotation =45;
        }
        else if(down)
        {
            m1.rotation =135;
        }
        else
        {
            m1.rotation =90;
        }
       
    }
    if(up&&!left&&!right)
    {
        m1.rotation =0;
    }
    if(down&&!left&&!right)
    {
        m1.rotation =180;
    }
   
}
//Lets the function main play every frame.
addEventListener(Event.ENTER_FRAME,Main);

stage.addEventListener(KeyboardEvent.KEY_DOWN, prease_key);

stage.addEventListener(KeyboardEvent.KEY_UP, resave_key);

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
August 13, 2009

what's your question?

Participating Frequently
August 13, 2009

in this code i put a simple circle whan i click left  > the circle rotation 270 >>>>

now i went to replace this circle with man

whan i click left the pic of instance change to other pic

ex of man (with state  left right up down ..)

http://img43.imageshack.us/img43/5909/alexchar.png

do u understand now?

kglad
Community Expert
Community Expert
August 13, 2009

instead of using m1 in your code, declare:

var mc:MovieClip;

and in your code use mc instead of m1.

then when you want to rotate your circle, use:

mc=m1;

and when you want to rotate a different displayobject, say mc2, use:

mc=m2;