PDA

View Full Version : trying to grasp applying generic function to mc onEnterFrame's



RASMedia
December 17th, 2004, 01:26 AM
Ok, so first of all thank you for reading this, extra thanks for taking the time to reply.

So, I'm a beginning to intermediate Flash and AS guy grappling with some programming concepts.

I find that when I create my own functions from scratch, especially to define a generic function to different movie clips, in this case to their onEnterFrame functions, I get unexpected results.

The bottom is some code from a new simple website I'm trying to code. My problem is that I can't get the function working in a generic way. I know I may be taking the completely wrong approach and totally missing something, but I am continuously coming up with problems (seems to be a misunderstading of scope at a deeper level, not simple variable timeline scope, but inherant scope within functions, etc..).

So, I will just copy paste the code here and try and comment as much as possible. For claritie's sake I will remove button codes except option1 and option2.

Ok, it's late and my brain is fried. Here we go. Flame away, I need to get this!!


//initialization of a few global and one timeline variable. These should
//be self explanatory in the code so I wont go into to much detail
//my probs arent in the detail but in the overall relationship of the
//code blocks
//
_global.INIT_MOVIE_POS = 1230;
_global.TARGET_X = 5;
_global.SPEED = 5;
var currentMovie = "0";
//
//
//LoadNewMovie function unloads current movie and loads new movie
//My main problem here is that this function seems to assign itself to
//every movie clip onEnterFrame that is subscribed to it, even though
//the assignment of the MC's onEnterFrame is governed by a onRelease
//event of the associated button
//
//
LoadNewMovie = function () {
if (currentMovie != "0") {
currentMovie.UnloadMovie();
}
this._visible = true;
if (this._x>TARGET_X) {
this._x -= (TARGET_X+this._x)/5;
} else {
this._x = TARGET_X;
currentMovie = this;
delete this.onEnterFrame;
}
};
//
//
//UnloadMovie unloads the current movie, called by LoadNewMovie
//
UnloadMovie = function() {
currentMovie._visible = false;
currentMovie._x = INIT_MOVIE_POS;
};
//prototype shiver controls roll over and roll out movement of buttons
//
MovieClip.prototype.shiver = function(xScale, yScale, strength, weight) {
var xScaleStep = 0;
var yScaleStep = 0;
this.onEnterFrame = function() {
xScaleStep = (xScale-this._xscale)*strength+xScaleStep*weight;
yScaleStep = (yScale-this._yscale)*strength+yScaleStep*weight;
this._xscale += xScaleStep;
this._yscale += yScaleStep;
};
};
option1.onRollOver = function() {
this.shiver(110, 110, 0.3, 0.8);
this._alpha = 100;
};
option1.onRollOut = function() {
this.shiver(100, 100, 0.3, 0.8);
this._alpha = 70;
};
option1.onMouseDown = function() {
_root.accolade_mc.onEnterFrame = LoadNewMovie;
};
option2.onRollOver = function() {
this.shiver(110, 110, 0.3, 0.8);
this._alpha = 100;
};
option2.onRollOut = function() {
this.shiver(100, 100, 0.3, 0.8);
this._alpha = 70;
};
option2.onMouseDown = function() {
dummy2_mc.onEnterFrame = LoadNewMovie;
};

Any and all help is appreciate here. Sometimes advanced RIA developers will see a problem like this and send me a 5 page RIA app that explains all the problems, but is SO over my head, it doesn't really help. What I do need to understand is, from a procedural approach to my simple problem, what am I missing, OR if my problem naturally points to an OOP approach of solving it, please let me know what the area of my fundamental misunderstanding or lack of knowledge is so I can specifically look to address it.

I am prepairing to study OOP structure, but feel that it won't benefit me until I have a strong procedural background. + I'm in between jobs right now and have to think about immediate visual effects, nor RIA which I am still a bit far from to say the least!

Anybody who read this far, you have my serious appreciation, respect, and thanks!!

David

:?)

glkngs
December 17th, 2004, 03:05 AM
I've tried out your script and it seems to work fine!?
What exactly is the problem? The onEnterFrame ran and ended like it should and it didn't interfere with any other mc.. maby i just didn't get it at all :sigh:

RASMedia
December 17th, 2004, 03:39 AM
my prob is that when I run:

option1.onRollOver = function() {
this.shiver(110, 110, 0.3, 0.8);
this._alpha = 100;
};
option1.onRollOut = function() {
this.shiver(100, 100, 0.3, 0.8);
this._alpha = 70;
};
option1.onMouseDown = function() {
_root.accolade_mc.onEnterFrame = LoadNewMovie;
};
option2.onRollOver = function() {
this.shiver(110, 110, 0.3, 0.8);
this._alpha = 100;
};
option2.onRollOut = function() {
this.shiver(100, 100, 0.3, 0.8);
this._alpha = 70;
};
option2.onMouseDown = function() {
dummy2_mc.onEnterFrame = LoadNewMovie;
};

I literally get both MC's, accolade_mc and dummy2_mc acting on both onMouseDown's as if they were one. In other words, with this exact script worded the exact way, When I click "option1" both mc's act on top of each other at the same time (I lower the dummy2_mc alpha to 70% to verify this behaviour).

At the same time, if I comment out either a piece of the code, or various pieces of the code, I can observe it function as 'seemingly' normal, but the second I tune all pieces of code back in, the operations dont function correctly. Not just the buttons, but other significant parts of the code as well.

I know I'm not just tripping on this problem because when I comment sections of code out, the changes are predictable and repeatable. I guess the many combinations have kept me from spending a whole day charting the problems, but they do exist and they are repeatable.

But I KNOW there is something fundamental I am missing here.

Thanks for the effort on testing the code. I know its got fundamental structural problems, I just don't know what they are!!

icio
December 17th, 2004, 11:31 AM
mouse downs are global, it's not like a onPress where the mouse has to be over the movieclip.

either use onPress = function()... or try something like this:


mc.onMouseDown = function() {
if (this.hitTest(_root._xmouse, _root._ymouse)) {
// user has click on this moveclip
}
}

hope this helps :thumb:

RASMedia
December 17th, 2004, 11:52 AM
ahhh. That was indeed a big part of my problem. Thank you kindly sir!!

Sometimes I feel like I wish there was some more down and dirty nuts and bolts low level documentation on some of this stuff. For instance, all over I see and use curly braces, some with semi colons, some without, but even in the AS dictionary I see no mention as to why and when they are used.

I'm sure myself, and others, have some sense of when to put them, but it still seems like belief rather than logic. When I look up curly braces in the AS dictionary it uses them with no semi colons, and no mention of semi colons. Are they completely arbitrary??

Anyhow, thanks for the help, again!! I still have a coulpe issues with the code, but my big one is solved.

=) =) =)

icio
December 17th, 2004, 11:56 AM
the only time i can think of this is when you do something like:

mc.onEnterFrame = function() {
};
is this the sort of thing you mean?

as far as i can tell, you use them when you specify an event function out with the movieclip that you are specifying it for.

personally, i never put ';'s after '}'s, i just click the format code button and flash does it for me :)

RASMedia
December 17th, 2004, 12:51 PM
yeah, that's what I meant. That's a good suggestion though. I will write various blocks of code without the ";" and then auto format and observe where Flash puts it!

Thanks again ~

d

icio
December 17th, 2004, 01:14 PM
=) you're welcome