PDA

View Full Version : compress code....


jennys26
01-18-2006, 10:24 PM
I have a tab with a slide out menu. The slide out menu is coded so that when you roll over it and it starts to slide out, if you take your mouse off before its finished sliding out, it will begin to slide back in at that point. It wont finish the whole sliding out animation.

So because of that I've ended up with all these hit tests and if else tests to get the buttons working. The code below works, but I've got 8 tabs and its ridiculous to write this out 8 times. Any ideas on how to compress this code??



tab_1.onRollOver = function () {

if(_currentFrame = 1){

tab_1.onEnterFrame = function () {

this.nextFrame();
if (this._currentframe >= 25) {
this.onEnterFrame=undefined
}
else if (tab_1.pmp_mc.hitTest(_xmouse,_ymouse)){
this.pmp_mc.gotoAndStop(2);
this.pap_mc.gotoAndStop(1);
this.ci_mc.gotoAndStop(1);
this.burns_mc.gotoAndStop(1);
}

else if (tab_1.pap_mc.hitTest(_xmouse,_ymouse)){
this.pap_mc.gotoAndStop(2);
this.pmp_mc.gotoAndStop(1);
this.ci_mc.gotoAndStop(1);
this.burns_mc.gotoAndStop(1);
}
else if (tab_1.ci_mc.hitTest(_xmouse,_ymouse)){
this.pap_mc.gotoAndStop(1);
this.pmp_mc.gotoAndStop(1);
this.ci_mc.gotoAndStop(2);
this.burns_mc.gotoAndStop(1);
}

else if (tab_1.burns_mc.hitTest(_xmouse,_ymouse)){
this.pap_mc.gotoAndStop(1);
this.pmp_mc.gotoAndStop(1);
this.ci_mc.gotoAndStop(1);
this.burns_mc.gotoAndStop(2);
}
else if (tab_1.blank_mc.hitTest(_xmouse,_ymouse)){
this.pap_mc.gotoAndStop(1);
this.pmp_mc.gotoAndStop(1);
this.ci_mc.gotoAndStop(1);
this.burns_mc.gotoAndStop(1);
}

}
}
}

tab_1.onRollOut = button.onDragOut = function () {

tab_1.onEnterFrame = function () {

this.prevFrame();
if (this._currentframe == 1) {
this.onEnterFrame=undefined}
}
}


tab_1.onPress = function () {
if (tab_1.pmp_mc.hitTest(_xmouse,_ymouse)){
this._parent.gotoAndStop("pmp");
}
if (tab_1.pap_mc.hitTest(_xmouse,_ymouse)){
trace("pap_mc onPress");
}
if (tab_1.ci_mc.hitTest(_xmouse,_ymouse)){
trace("ci_mc onPress");
}
if (tab_1.burns_mc.hitTest(_xmouse,_ymouse)){
trace("burns_mc onPress");
}
}

Cota
01-18-2006, 10:28 PM
Your first If statement is incorrect. missing ==. Are you attaching this code dynamically or is all precoded?

jennys26
01-18-2006, 10:36 PM
I see what you mean about the missing =, but it works, somehow. Its written in the first frame of my fla.

Cota
01-18-2006, 10:38 PM
If its on the first frame, and the buttons are there too..you can make it dynamic and not have ti re-write 8 times. If thats the case, I'll show you how.

jennys26
01-18-2006, 10:42 PM
OK....do you mean create the 8 tabs dynamically and then apply the code to them?

Cota
01-18-2006, 10:44 PM
THe tabs already exist correct? on the first frame of the main FLA? If thats the case then we can apply that code to all the tabs and only write it once.

jennys26
01-18-2006, 10:46 PM
righto, how do I do that?

Cota
01-18-2006, 10:52 PM
Lets wrap it in a for loop.

for (i=1; i<0; i++) {
//All the other code will go inside here.
};

Then replace all "tab_1" with this
_root["tab_" + i].

Sorry, I would do it, but there is a copy and paste bug in the forums..I would spend the next 20 minutes formatting it..Does this make sense?

jennys26
01-18-2006, 11:26 PM
It's not quite working, but lets go way back to basic's. If I've got 8 tabs named tab_1 through to tab_8 all on the first frame of my movie. The tabs are instances of a movieclip with 2 frames, one up, one over.

Then I set up a loop so that when you roll over the tab it goes to the over state.

This is what I've written and it doesn't work. WHY??


for (i=1; i<0; i++) {
_root["tab_" + i].onRollOver = function(){
this.gotoAndStop(2);
}
}

I can't trace anything. And if you rollover the tabs the arrow doesn't change to the hand. It doesn't register anything.....

jennys26
01-18-2006, 11:28 PM
don't worry changed the i<0 to i<8 and its doing something. Will keep trying to make it work. Thanks for your help so far. might have more questions later, if you still there.

ta

flashead
01-18-2006, 11:38 PM
here's a shorter version:
// loop that'll set up yer buttons
for ( var i=0; i<=8; i++ )
{
var btn = this["tab_" + i];

btn.hitArray = [ "pmp_mc", "pap_mc", "ci_mc", "burns_mc" ];
btn.lastMC = null;
btn.curMC = null;
btn.hit = false;

btn.onRollOver = btnOver;
btn.onRollOut = btnOut;
btn.onPress = btnPress;
btn.onRelease = btn.onReleaseOutside = btnRelease;
}

function btnOver()
{
if ( this._currentframe == 1 )
{
var l = this.hitArray.length;

this.onEnterFrame = function ()
{
this.nextFrame();
var mc = this.hitArray[i];

// so instead of physically checking the hitTest on each mc,
// this uses a for loop with an array of instance names.
// if a hitTest returns true, the mc it hit is passed to the toggleMC function.
if ( this._currentframe >= 25 ) delete this.onEnterFrame;
else for ( var i=0; i<l; i++ ) if ( mc.hitTest( _xmouse, _ymouse ) && !this.hit ) toggleMC( mc );
}
}
}

function btnOut()
{
this.onEnterFrame = function ()
{
this.prevFrame();
if ( this._currentframe == 1 ) delete this.onEnterFrame;
}
}

function btnPress()
{
// pretty much left this alone for now. not sure where you're going with it.
if ( this.pmp_mc.hitTest( _xmouse, _ymouse ) ) this._parent.gotoAndStop( "pmp" );
if ( this.pap_mc.hitTest( _xmouse, _ymouse ) ) trace( "pap_mc onPress" );
if ( this.ci_mc.hitTest( _xmouse, _ymouse ) ) trace( "ci_mc onPress" );
if ( this.burns_mc.hitTest( _xmouse, _ymouse ) ) trace( "burns_mc onPress" );
}

function btnRelease()
{
trace( "btnRelease()" );
}

function toggleMC( mc )
{
this.lastMC = this.curMC;
this.curMC = mc;

this.curMC.gotoAndStop( 2 );
this.lastMC.gotoAndStop( 1 );
}


managed to streamline your rollOver function too ;)

flashead
01-19-2006, 02:26 PM
Lets wrap it in a for loop.

for (i=1; i<0; i++) {
//All the other code will go inside here.
};

Then replace all "tab_1" with this
_root["tab_" + i].

Sorry, I would do it, but there is a copy and paste bug in the forums..I would spend the next 20 minutes formatting it..Does this make sense?

About that copy/paste bug. Seems to be just for IE. When I use FF or Safari at home it's all good.

Cota
01-19-2006, 06:02 PM
I figured as much..I've had a few other copy and paste issues with IE...usually passing it to notepad keeps the formatting, just doesnt do it here.