A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: Stage Boundaries

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Location
    Cornfolk
    Posts
    4

    Stage Boundaries

    Hi Guys, Im am trying to make boundaries on my stage for my MC box1_mc, there are no errors but it doesnt seem to work.. Here is the code.. Any help would be great


    function limitStageBorder(box1_mc) {

    var box1_x:Number = stage.stageWidth - box1_mc.width;
    var box1_y:Number = stage.stageHeight - box1_mc.height;


    if (box1_mc.x + box1_x > stage.stageWidth) {
    box1_mc.x = stage.stageWidth - box1_x;
    }
    else if (box1_mc.x - box1_x < 0) {
    box1_mc.x = 0 + box1_x;
    }

    if (box1_mc.y - box1_y < 0) {
    box1_mc.y = 0 + box1_y;
    }
    else if (box1_mc.y + box1_y > stage.stageHeight) {
    box1_mc.y = stage.stageHeight - box1_y;
    }
    }

  2. #2
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Code:
    function limitStageBorder(box) {
    
    	var regOffset = box.getBounds(stage);
    	regOffset.x -=  box.x;
    	regOffset.y -=  box.y;
    	if (box.x + regOffset.x + box.width > stage.stageWidth) {
    		box.x = stage.stageWidth - box.width - regOffset.x;
    	} else if (box.x + regOffset.x <0) {
    		box.x = 0 - regOffset.x;
    	}
    
    	if (box.y + regOffset.y + box.height > stage.stageHeight) {
    		box.y = stage.stageHeight - box.height - regOffset.y;
    	} else if (box.y + regOffset.y <0) {
    		box.y = 0 - regOffset.y;
    	}
    
    }
    limitStageBorder(box1_mc);
    Last edited by dawsonk; 12-16-2010 at 07:21 PM. Reason: Typos

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Location
    Cornfolk
    Posts
    4

    Smile

    Appreciate the reply, but the MC still moves off the stage? No errors, just doesnt seem to be working..Hmmm...

    thanks for reply again..

  4. #4
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Weird. This is what I tried and it works.

    Movie clip on the stage with an instance name of 'box1_mc'.
    Code on main timeline.
    Code:
    import flash.display.Stage;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    
    var swfStage:Stage = this.stage;
    swfStage.scaleMode = StageScaleMode.NO_SCALE;
    swfStage.align = StageAlign.TOP_LEFT;
    
    function limitStageBorder(box) {
    
    	var regOffset = box.getBounds(stage);
    	regOffset.x -=  box.x;
    	regOffset.y -=  box.y;
    	if (box.x + regOffset.x + box.width > stage.stageWidth) {
    		box.x = stage.stageWidth - box.width - regOffset.x;
    	} else if (box.x + regOffset.x <0) {
    		box.x = 0 - regOffset.x;
    	}
    
    	if (box.y + regOffset.y + box.height > stage.stageHeight) {
    		box.y = stage.stageHeight - box.height - regOffset.y;
    	} else if (box.y + regOffset.y <0) {
    		box.y = 0 - regOffset.y;
    	}
    
    }
    limitStageBorder(box1_mc);
    
    swfStage.addEventListener(Event.RESIZE, resizeDisplay);
    function resizeDisplay(e:Event):void {
    	limitStageBorder(box1_mc);
    }

  5. #5
    Junior Member
    Join Date
    Dec 2010
    Location
    Cornfolk
    Posts
    4
    Hi, thanks again for the replys, much appreciated. Just tried it there and the mc still moves off stage. Surely there must be an easier way of giving the stage boundaries. Im just testing at the minute with a box, the app is a man that is controlled by the arrow keys and I want to prevent him from going off stage when controlled. Is there a simpler method of subtracting MC properties from the measurements of the stage?

  6. #6
    :
    Join Date
    Dec 2002
    Posts
    3,518
    How are you implementing the function call?
    Code:
    import flash.events.*;
    
    function limitStageBorder(box) {
    
    	var regOffset = box.getBounds(stage);
    	regOffset.x -=  box.x;
    	regOffset.y -=  box.y;
    	if (box.x + regOffset.x + box.width > stage.stageWidth) {
    		box.x = stage.stageWidth - box.width - regOffset.x;
    	} else if (box.x + regOffset.x <0) {
    		box.x = 0 - regOffset.x;
    	}
    
    	if (box.y + regOffset.y + box.height > stage.stageHeight) {
    		box.y = stage.stageHeight - box.height - regOffset.y;
    	} else if (box.y + regOffset.y <0) {
    		box.y = 0 - regOffset.y;
    	}
    
    }
    
    function moveGuy(e:KeyboardEvent):void {
    	switch (e.keyCode) {
    		case Keyboard.LEFT :
    			guyMC.x -=  5;
    			limitStageBorder(guyMC);
    			break;
    		case Keyboard.RIGHT :
    			guyMC.x +=  5;
    			limitStageBorder(guyMC);
    			break;
    		case Keyboard.DOWN :
    			guyMC.y +=  5;
    			limitStageBorder(guyMC);
    			break;
    		case Keyboard.UP :
    			guyMC.y +=  5;
    			limitStageBorder(guyMC);
    			break;
    		default :
    			break;
    	}
    }
    
    stage.addEventListener(KeyboardEvent.KEY_DOWN, moveGuy);

  7. #7
    Junior Member
    Join Date
    Dec 2010
    Location
    Cornfolk
    Posts
    4
    Yeah just tried it there, worked fine.

    Thank you so much. It must have been the way I coded the moving function. I wanted to move the man smoothly but the way it is now will do. I am creating a mini game for my website for the customers to play.
    Thank you so much again for both your time and effort.

  8. #8
    :
    Join Date
    Dec 2002
    Posts
    3,518

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center