# Constrain

**URL:** <https://forum.kirupa.com/t/constrain/18569>\
**Category:** flash\
**Created:** [April 18, 2003, 3:45am UTC](https://forum.kirupa.com/t/constrain/18569 "2003-04-18T03:45:59Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![mystee](https://avatars.discourse-cdn.com/v4/letter/m/f14d63/32.png) [@mystee](https://forum.kirupa.com/u/mystee)\
**Post date:** [April 18, 2003, 3:45am UTC](https://forum.kirupa.com/t/constrain/18569/1 "2003-04-18T03:45:59Z")

</div>

How can make a moving object in flash constrain within the window/stage?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 18, 2003, 4:02am UTC](https://forum.kirupa.com/t/constrain/18569/2 "2003-04-18T04:02:06Z")

</div>

Do you mean keep its size without stretching or shrinking?  
If so…

```php

Stage.scaleMode = "noscale"

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 18, 2003, 4:06am UTC](https://forum.kirupa.com/t/constrain/18569/3 "2003-04-18T04:06:36Z")

</div>

what i meant was…

for example…i have a racing car in the window…and i can move it using key press…but i do not wish it to go out of the window. So it means that the car can only move within the window…

How can dat be done?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 18, 2003, 4:58am UTC](https://forum.kirupa.com/t/constrain/18569/4 "2003-04-18T04:58:23Z")

</div>

Check its \_x and \_y properties to see if they exceed 0 or your window height or width.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 18, 2003, 6:36am UTC](https://forum.kirupa.com/t/constrain/18569/5 "2003-04-18T06:36:41Z")

</div>

erm…check the MC \_x and \_y properties? how can it be done?

Btw, how can I get an animation to play when the car is on top of a button or movieclip?

i’m still new to this…=|

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 18, 2003, 6:45am UTC](https://forum.kirupa.com/t/constrain/18569/6 "2003-04-18T06:45:15Z")

</div>

we would need to know what method you’re using to move the car before that could be answered. Can you provide an FLA for us to look at?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 18, 2003, 6:55am UTC](https://forum.kirupa.com/t/constrain/18569/7 "2003-04-18T06:55:01Z")

</div>

ok…car is just an example…

Anyway, the object in this .fla is just a dummy…

In this case…I do not wish my moving object to go out of the window…and when its on top of the grey button…a pop up window will appear…

How can this be done with AS?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 18, 2003, 7:02am UTC](https://forum.kirupa.com/t/constrain/18569/8 "2003-04-18T07:02:04Z")

</div>

sorry… I got to check it in the morning. No Flash here at work. 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 18, 2003, 7:13am UTC](https://forum.kirupa.com/t/constrain/18569/9 "2003-04-18T07:13:20Z")

</div>

its ok…=)

meanwhile…any other help?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 18, 2003, 7:39am UTC](https://forum.kirupa.com/t/constrain/18569/10 "2003-04-18T07:39:02Z")

</div>

well, if the car were moved by say buttons, where you held down the right arrow key and it moved to the right… then you would have code something like

onKeyPress(RIGHTARROW){  
if(\_root.car.\_x\>0||\_root.car.\_x\<600){  
\_root.car.\_x+=10;  
}  
}

Keep in mind… this is just from my head, so it probebly wont work the exact way I’m thinking… but it shows in what way “if” statements can be used to prevent an object from going somewhere.

The idea of a movie clip playing while the car is over it would be done with a hitTest method. Again though… it will take me looking at it before I can tell exactly what the paths are to various objects in the movie, but it might look something like.

onClipEvent(enterFrame){  
for(obj in \_root){  
if(\_root[obj].hitTest(this,true){  
\_root[obj].play();  
}  
}  
}

it probebly looks like greek to you at this point but I’ll explain both methods once I see how your movie is set up.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 18, 2003, 7:49am UTC](https://forum.kirupa.com/t/constrain/18569/11 "2003-04-18T07:49:19Z")

</div>

I tried the hit test method…didn’t work…=|

I’m so messed up…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 18, 2003, 7:59am UTC](https://forum.kirupa.com/t/constrain/18569/12 "2003-04-18T07:59:02Z")

</div>

yeah… it is very dependent upon the paths to various objects. The other thing is, the way I wrote it, it would basicaly be testing 17+ times a second, on every object on the main timeline. Though it might have worked it’s hardly efficient. Really, it’s just an example of what a hit test looks like, in a “for in” loop. I can actually explain relatively well what happens in the script I wrote.

```auto

//this would go on the car movieClip. onEnterframe fires it's code whenever the object's play head enters a new frame.
onClipEvent(enterFrame){
  //a for loop says, look in here, for objects, and temporarily call each object you find, "obj". Perform the contained code on each object you find.
  for(obj in _root){
    //if the obj you found in root is intersecting with this object's registration point, exicute the script in the brackets.
    if(_root[obj].hitTest(this,true){
      //test was true, make this object you found play
      _root[obj].play();
    }
  }
}

```

It’s not too tough to perform these hitTests, but like I said… not very efficient the way I wrote it.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 18, 2003, 11:42am UTC](https://forum.kirupa.com/t/constrain/18569/13 "2003-04-18T11:42:13Z")

</div>

heres some constrain code:  
[http://proto.layer51.com/d.aspx?f=660](http://proto.layer51.com/d.aspx?f=660)

just put that in the first frame of your movie so its defined and understood by flash, then, use whatever it is to move your car and AFTER that, use that function ie

car.constrainTo(screenClip);

^ now you can either constrain it to a movieclip (like a movieclip that makes up the ground) or hardcode in the dimensions of your screen like

car.constrainTo({xmin:0,xmax:550,ymin:0,ymax:400});

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 19, 2003, 12:27am UTC](https://forum.kirupa.com/t/constrain/18569/14 "2003-04-19T00:27:59Z")

</div>

I copied this to the 1st frame…

MovieClip.prototype.constrainTo = function(cb){  
if (typeof cb == “movieclip”) cb = cb.getBounds(this.\_parent);  
var tb = this.getBounds();  
if (this.\_x + tb.xmin \< cb.xmin) this.\_x = cb.xmin + tb.xmax;  
if (this.\_x + tb.xmax \> cb.xmax) this.\_x = cb.xmax + tb.xmin;  
if (this.\_y + tb.ymin \< cb.ymin) this.\_y = cb.ymin + tb.ymax;  
if (this.\_y + tb.ymax \> cb.ymax) this.\_y = cb.ymax + tb.ymin;   
}

and I put in the function

car.constrainTo  
({xmin:0,xmax:550,ymin:0,ymax:400});

into my moving object…

but it still go out of the window…why?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 19, 2003, 6:45am UTC](https://forum.kirupa.com/t/constrain/18569/15 "2003-04-19T06:45:57Z")

</div>

ah…it works…Thanks!
