Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 08-06-2012, 11:16 PM   #1
Pedz101
Registered User
 
Join Date: Aug 2012
Posts: 9
Red face How to add a boundary to an object

So I created a action script object on the stage. Named the stage document class as movement. The object can move with the keyboard, however, I don't want it to move off stage. The object instance name is square. My question is how do I add a boundary to the object? I've tried the hitTestObject but it doesn't work very well. The stage is 900 by 575.

Note: I am still a newbie so if you can explain to me in details that would be great. thx

Pedz101 is offline   Reply With Quote
Old 08-07-2012, 01:08 AM   #2
[afz]snickelfitz
Senior Member
 
[afz]snickelfitz's Avatar
 
Join Date: Dec 2011
Location: Tucson, AZ
Posts: 2,398
Default

Since you already have the keyboard movement handled, this example uses a draggable object.

ActionScript Code:
import flash.events.*; import flash.display.*; import flash.geom.*; // movieclip on the stage named "object" var object:MovieClip; object.addEventListener(MouseEvent.MOUSE_DOWN, drag); stage.addEventListener(MouseEvent.MOUSE_UP, drop); // track the clicked movieclip var target:MovieClip; // define the draggable boundaries; stroke the boundary var bounds:Rectangle = new Rectangle(100, 100, stage.stageWidth-200, stage.stageHeight-200); var g:Graphics = this.graphics; g.lineStyle(0); g.drawRect(bounds.left, bounds.top, bounds.width, bounds.height); function loop(e:Event):void {     if (target.x > bounds.right - target.width)     {         target.x = bounds.right - target.width;     }     if (target.x < bounds.left)     {         target.x = bounds.left;     }     if (target.y > bounds.bottom - target.height)     {         target.y = bounds.bottom - target.height;     }     if (target.y < bounds.top)     {         target.y = bounds.top;     } } function drag(e:MouseEvent):void {     // define target     target = e.currentTarget as MovieClip;     target.startDrag();         // check target coordinates on each enter frame event     addEventListener(Event.ENTER_FRAME, loop); } function drop(e:MouseEvent):void {     target.stopDrag(); }
[afz]snickelfitz is offline   Reply With Quote
Old 08-07-2012, 03:50 AM   #3
Pedz101
Registered User
 
Join Date: Aug 2012
Posts: 9
Default

Thanks for giving an example for drag and drop but then how do I use this for creating the stage a boundary? Also how I use the x and y for the square to make sure it doesn't leave the stage?
Pedz101 is offline   Reply With Quote
Old 08-07-2012, 05:12 AM   #4
[afz]snickelfitz
Senior Member
 
[afz]snickelfitz's Avatar
 
Join Date: Dec 2011
Location: Tucson, AZ
Posts: 2,398
Default

Just make the Rectangle instance the same dimensions as the stage.
ie: var bounds:Rectangle = new Rectangle(0,0,stage.stageWidth,stage.stageHeight);

Everything else is essentially the same.
The rectangle is included here to illustrate the usefulness of the Rectangle class for doing stuff like this. ie: Rectangle has convenient left, right, bottom, top properties as well as a host of useful methods.

BTW, notice that there are virtually no hard-coded numeric properties in this code, which makes it more flexible and universal than using the numbers 900 and 575 to describe the stage boundaries. For all intents and purposes, the actual dimensions of the stage are irrelevant.

One other thing: be sure your concept of the stage is correct.
There is only one Stage instance. The stage dimensions are set in the FLA in the properties panel, along with the stage background color and document frameRate.

Flash Player instance
--Stage instance
----Document Class instance
------stuff you create in Flash

Last edited by [afz]snickelfitz; 08-07-2012 at 05:18 AM.
[afz]snickelfitz is offline   Reply With Quote
Old 08-07-2012, 06:08 AM   #5
Pedz101
Registered User
 
Join Date: Aug 2012
Posts: 9
Default

Thanks it work =]
Pedz101 is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 08:31 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.