在flash中设置边界

时间:2014-05-17 21:26:04

标签: actionscript-3 flash actionscript

我想使用as3在flash中为我的影片剪辑设置边界,以便影片剪辑无法移出舞台。我怎样才能做到这一点?这是我现在的代码。我现在如何设定界限?

    bij_mc.links_mc.play();
bij_mc.rechts_mc.play();

stage.addEventListener(KeyboardEvent.KEY_DOWN,beweeg);
stage.addEventListener(KeyboardEvent.KEY_UP,stopbeweeg);

function beweeg(evt:KeyboardEvent):void {
    if (evt.keyCode==Keyboard.LEFT) {
        bij_mc.links_mc.x -=10;
        bij_mc.rechts_mc.x -=10;
        bij_mc.lichaam_mc.x -=10;
        bij_mc.links_mc.stop();
        bij_mc.rechts_mc.play();
        bij_mc.rotation = -5;
    } else if (evt.keyCode==Keyboard.RIGHT) {
        bij_mc.links_mc.x +=10;
        bij_mc.rechts_mc.x +=10;
        bij_mc.lichaam_mc.x += 10;
        bij_mc.links_mc.play();
        bij_mc.rechts_mc.stop();
        bij_mc.rotation = 5;
    } else if (evt.keyCode==Keyboard.UP) {
        bij_mc.links_mc.y -=10;
        bij_mc.rechts_mc.y -= 10;
        bij_mc.lichaam_mc.y -= 10;
        bij_mc.links_mc.play();
        bij_mc.rechts_mc.play();
        bij_mc.rotation = 0;
    } else if (evt.keyCode==Keyboard.DOWN) {
        bij_mc.links_mc.y +=10;
        bij_mc.rechts_mc.y += 10;
        bij_mc.lichaam_mc.y += 10;
        bij_mc.links_mc.stop();
        bij_mc.rechts_mc.stop();
        bij_mc.rotation = 0;
    }
}

function stopbeweeg(evt:KeyboardEvent):void {
    bij_mc.links_mc.play();
    bij_mc.rechts_mc.play();
    bij_mc.rotation = 0;
}

1 个答案:

答案 0 :(得分:0)

stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
private function onEnterFrame(event:Event):void
{
   // update beweeg
   checkBoundaries()
}

private function checkBoundaries():void
{
   // left
   if (bij_mc.x < 0)
          bij_mc.x = 0;
   // right
   else if (bij_mc.x > stage.stageWidth)
          bij_mc.x = stage.stageWidth - bij_mc.width;
   // top
   if (bij_mc.y < 0)
          bij_mc.y = 0;
   // bottom
   else if (bij_mc.y > stage.stageHeight)
          bij_mc.y = stage.stageHeight - bij_mc.height;
}

相应地更新每个影片剪辑。