Skip to main content
sergey_landar
Inspiring
January 8, 2010
Answered

Creating area where movie clip can moving but can not go away from this area

  • January 8, 2010
  • 2 replies
  • 461 views

I want make somethin line first movie clip must moving in second movie clip, but can not go away from second movie clip.

stage.addEventListener(KeyboardEvent.KEY_DOWN, starMoving);
function starMoving(event:KeyboardEvent):void
{
     switch (event.keyCode)
     {
          case Keyboard.UP : star_mc.y-=10;
          break;
          case Keyboard.DOWN : star_mc.y+=10;
          break;
          case Keyboard.LEFT : star_mc.x=star_mc.x-10;
          break;
          case Keyboard.RIGHT : star_mc.x+=10;
          break;
          default:
          break;
     }
}

So this is the code for manipulate first movie clip.. i can go everywhere on the stage...

How i can make then first movie clip moved just in second?

Thank you.

This topic has been closed for replies.
Correct answer

Whenever you move, you need to test the extents to see if you're still in bounds. I'll use right for an example, you can do the rest.

case Keyboard.RIGHT : 
     star_mc.x+=10;
     if(star_mc.x + star_mc.width > containerClip.x + containerClip.width){
          star_mc.x =
containerClip.x + containerClip.width - star_mc.width;
     }
     break;

2 replies

Correct answer
January 8, 2010

Whenever you move, you need to test the extents to see if you're still in bounds. I'll use right for an example, you can do the rest.

case Keyboard.RIGHT : 
     star_mc.x+=10;
     if(star_mc.x + star_mc.width > containerClip.x + containerClip.width){
          star_mc.x =
containerClip.x + containerClip.width - star_mc.width;
     }
     break;
sergey_landar
Inspiring
January 9, 2010

Thank you dmennenoh

All looks easy:) tomorrow will study cause now 3o'clock in the night:)

kglad
Community Expert
Community Expert
January 8, 2010

you can check the pixelBounds property of each of the movieclip's transform property to see if one is completely within the other.