Is there anyway to dynamically change a buttons hit area?
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
…just a small example of sequentially changing the hitArea for a MC called myButton to 3 other MC’s named hitArea1 hitArea2 hitArea3 etc…
//use onEnterFrame to reset the hitArea
this.onEnterFrame = function()
{
newHitArea = _root["hitArea" + count];
myButton.hitArea = newHitArea;
}
Hope this helps
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.
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 :-\
thanks, ilyas. =)
minor update…
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);
Nice Kax!
Thanks for that
SHO
no problem, eki. :beam:
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. :-\
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);
you so crazy kax :thumb:
yeah… i know.