PDA

View Full Version : functions not working together



ballerina
October 19th, 2004, 05:19 PM
I have these two functions which both happen on an onEnterFrame event;"basichittest" and "photo". They each work by themselves, but not together

I attached the fla file
I commented out lines 6-10 and line 58.

I want the red dot "photodetector" to get brighter as the mirror slides to the right. I also want the beams of light to bounce back and forth when the drag happens.

If I uncomment the offending code, the beams no longer bounce, but the dot behaves the way I want.

Can I only have one function on enter frame?
I'm pretty bad at actionscripting so I need help. please.
thanks!

stringy
October 19th, 2004, 07:16 PM
I have these two functions which both happen on an onEnterFrame event;"basichittest" and "photo". They each work by themselves, but not together

I attached the fla file
I commented out lines 6-10 and line 58.

I want the red dot "photodetector" to get brighter as the mirror slides to the right. I also want the beams of light to bounce back and forth when the drag happens.

If I uncomment the offending code, the beams no longer bounce, but the dot behaves the way I want.

Can I only have one function on enter frame?
I'm pretty bad at actionscripting so I need help. please.
thanks!

why not put everything inside one function
function basicHitTest() {
brightness = mirror._x;
photodetector._alpha = brightness-330;
if (mirror.hitTest(mcShape2)) {
move = laserbeam._x;
movea = laserbeam2._y;
if (numberb == 1) {
laserbeam._x = move-20;
}
if (numberb == 2) {
laserbeam._x = move+20;
}
if (numbera == 3) {
laserbeam2._y = movea-20;
}
if (numbera == 4) {
laserbeam2._y = movea+20;
}
}
if (laserbeam2.hitTest(bottommirror)) {
numbera = 3;
}
if (laserbeam2.hitTest(topmirror)) {
numbera = 4;
}
if (laserbeam.hitTest(mirror2)) {
numberb = 2;
}
if (laserbeam.hitTest(mirror)) {
numberb = 1;
}
}
this.onEnterFrame = basicHitTest;

ballerina
October 20th, 2004, 12:03 PM
Oh, that's so obvious. I must've been really stupid yesterday.
Thanks for your help stringy. Works great now.

For future reference, what was technically wrong about writing it as two separate functions? Well, besides the fact that it doesn't work.

lostinbeta
October 20th, 2004, 12:27 PM
If you are calling 2 functions on enterframe you have to call them in the same onEnterFrame handler (unless you want to set up 2 individual onEnterFrame handlers, which would be ineffecient)...

this.onEnterFrame = function(){
basicHitTest();
photo();
}


When you do this.onEnterFrame = basicHitTest;
this.onEnterFrame = photo; The photo one overwrites the basicHitTest one because you redefine what function should be running onEnterFrame for 'this'.