# \[MX\] Change a buttons hit area?

**URL:** <https://forum.kirupa.com/t/mx-change-a-buttons-hit-area/22496>\
**Category:** flash\
**Created:** [June 4, 2003, 7:20pm UTC](https://forum.kirupa.com/t/mx-change-a-buttons-hit-area/22496 "2003-06-04T19:20:38Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Keith130](https://avatars.discourse-cdn.com/v4/letter/k/b38774/32.png) [@Keith130](https://forum.kirupa.com/u/Keith130)\
**Post date:** [June 4, 2003, 7:20pm UTC](https://forum.kirupa.com/t/mx-change-a-buttons-hit-area/22496/1 "2003-06-04T19:20:38Z")

</div>

Is there anyway to dynamically change a buttons hit area?

---

<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:** [June 5, 2003, 10:18am UTC](https://forum.kirupa.com/t/mx-change-a-buttons-hit-area/22496/2 "2003-06-05T10:18:52Z")

</div>

Hi,

Yes, there is. But you have to use a movieClip symbol rather than a button symbol.(button symbols are bound to disappear, I think)

Make a MC with three frames and place you ‘up’, ‘over’ and ‘down’ state graphics and a stop() on each of them, then label them \_up, \_over, \_down.Now this MC will behave like a button if you use onPress or onRelease command on it  
(not need to use all the commands , one is enough to get all the button states). Then you can declare any MC as the hit area like this:

MyButtonMc.hitArea = MyButtonMc.myHitAreaMc;

(if your hit area is ‘inside’ the buttonMC like in the example (it doesn’t have to) make sure it stretches the whole length of the MC).

You can move, scale or do whatever you want to it , or you can set any other MC at any time as you hitArea.

Cheers

SHO

---

<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:** [June 5, 2003, 11:19am UTC](https://forum.kirupa.com/t/mx-change-a-buttons-hit-area/22496/3 "2003-06-05T11:19:04Z")

</div>

…just a small example of sequentially changing the hitArea for a MC called myButton to 3 other MC’s named hitArea1 hitArea2 hitArea3 etc…

```auto

//use onEnterFrame to reset the hitArea
this.onEnterFrame = function()
{
             newHitArea = _root["hitArea" + count];    
             myButton.hitArea = newHitArea; 
	
}

```

Hope this helps 🙂

---

<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:** [June 6, 2003, 2:26pm UTC](https://forum.kirupa.com/t/mx-change-a-buttons-hit-area/22496/4 "2003-06-06T14:26:27Z")

</div>

```auto
Button.prototype.$area = undefined;
Button.prototype.$evnt = new Object();
Button.prototype.getHitArea = function() {
	return this.$area;
};
Button.prototype.setHitArea = function(area) {
	var rbtn = this;
	for (var prop in this) {
		this.$area[prop] = this.$evnt[prop];
		this.$evnt[prop] = area[prop];
		area[prop] = function () {
			rbtn[prop]();
		};
	}
	this.enabled = (!area || area == this);
	this.$area = area;
};
Button.prototype.addProperty("hitArea", Button.prototype.getHitArea, Button.prototype.setHitArea);
ASSetPropFlags(Button.prototype, null, 1);

```

there you go… now you have a nice hitArea property for your buttons. =) 😛

let me know if you find any bugs. 😉

---

<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:** [June 6, 2003, 3:13pm UTC](https://forum.kirupa.com/t/mx-change-a-buttons-hit-area/22496/5 "2003-06-06T15:13:57Z")

</div>

kax \>\> Nice one 🙂  
mindfriction \>\> Your code doesn’t mean anything. You’re setting the hitArea of a clip to the same value all the time :-\

---

<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:** [June 6, 2003, 3:27pm UTC](https://forum.kirupa.com/t/mx-change-a-buttons-hit-area/22496/6 "2003-06-06T15:27:35Z")

</div>

thanks, ilyas. =)

minor update…

```auto
Button.prototype.$area = undefined;
Button.prototype.$evnt = new Object();
Button.prototype.getHitArea = function() {
	return this.$area;
};
Button.prototype.setHitArea = function(area) {
	var rbtn = this;
	for (var prop in this) {
		this.$evnt[prop] ? this.$area[prop]=this.$evnt[prop] : delete this.$area[prop];
		this.$evnt[prop] = area[prop];
		area[prop] = function () {
			rbtn[prop]();
		};
	}
	this.enabled = (!area || area == this);
	this.$area = area;
};
Button.prototype.addProperty("hitArea", Button.prototype.getHitArea, Button.prototype.setHitArea);
ASSetPropFlags(Button.prototype, null, 1);

```

---

<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:** [June 6, 2003, 5:03pm UTC](https://forum.kirupa.com/t/mx-change-a-buttons-hit-area/22496/7 "2003-06-06T17:03:34Z")

</div>

Nice Kax!

Thanks for that

SHO

---

<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:** [June 6, 2003, 5:06pm UTC](https://forum.kirupa.com/t/mx-change-a-buttons-hit-area/22496/8 "2003-06-06T17:06:51Z")

</div>

😉

no problem, eki. :beam:

---

<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:** [June 6, 2003, 10:38pm UTC](https://forum.kirupa.com/t/mx-change-a-buttons-hit-area/22496/9 "2003-06-06T22:38:23Z")

</div>

ok… i found a bug. 😛

i fixed it and improved a bit some things… but i’m not really happy with this result. maybe i’ll keep working on it later. :-\

```auto
Button.prototype.setHandler = function(evnt, rbtn) {
	this[evnt] = function () {
		rbtn[evnt]();
	};
};
MovieClip.prototype.setHandler = Button.prototype.setHandler;
Button.prototype.$area = undefined;
Button.prototype.$evnt = new Object();
Button.prototype.getHitArea = function() {
	return this.$area;
};
Button.prototype.setHitArea = function(area) {
	for (var prop in this.$area) delete this.$area[prop];
	for (var prop in this.$evnt) this.$area[prop] = this.$evnt[prop], delete this.$evnt[prop];
	for (var prop in area) this.$evnt[prop] = area[prop], delete area[prop];
	for (var prop in this) area.setHandler(prop, this);
	this.enabled = (!area || area == this);
	this.$area = area;
};
Button.prototype.addProperty("hitArea", Button.prototype.getHitArea, Button.prototype.setHitArea);
ASSetPropFlags(Button.prototype, null, 1);
ASSetPropFlags(MovieClip.prototype, null, 1);

```

---

<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:** [June 6, 2003, 10:48pm UTC](https://forum.kirupa.com/t/mx-change-a-buttons-hit-area/22496/10 "2003-06-06T22:48:35Z")

</div>

you so crazy kax 😉 :thumb:

---

<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:** [June 6, 2003, 10:54pm UTC](https://forum.kirupa.com/t/mx-change-a-buttons-hit-area/22496/11 "2003-06-06T22:54:41Z")

</div>

yeah… i know. 😛 😉
