0
votes

Im trying to call a function(s) from different places. Dont have any luck so far. This is the basic function. When i press a button it do something:

button.addEventListener(MouseEvent.CLICK, myFunction1);
function myFunction1(event:MouseEvent):void 
{
   ///dosomeThing1
}

I have more than one button (myFunction2..3...4... etc)

but i wanted to exactly the same (myfunction1..2 etc) happen when i get a specific random number. For example if random number = 1 >>> do myfunction1 if random number = 2 >>> do myfunction2

So i tried this: (this code is at the top of the timelineframe)

function randomRange(minNum:Number, maxNum:Number):Number 
{
return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum);
}
var myrandomNumber = (randomRange(1, 6))
trace(myrandomNumber); //A number between 1 and 6
if (myrandomNumber == 1) {
    myFunction1(null); // i tried this (null) stuff
}
if (myrandomNumber == 2) {
    myFunction2();   // i tried also with just braces
}

etc.

the random number generation is alright, but i get errors all the time when i want to call a function (if i give a simple >> smthng.gotoAndStop(x) command (instead of a function) it is fine)

the whole code is in the timeline frame (no separate file)

the if statements are not inside in any function, just as you see here, it is ~kind of an "onLoad" stuff

The function works if i click on the button so it cant be the problem i guess, just is dont know how to call the same function from somewhere else?!? :-)

anybody any ideas? Really appreciate it!

Please leave examples! (Rookie programmer here! :-)

thanks Ben

Updates:

after Neals advice (thanks)i tried this one:

function randomRange(minNum:Number, maxNum:Number):Number 
{
return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum);
}
var myrandom = (randomRange(1, 6))
trace(myrandom); //A number between 10 and 20
if (myrandom == 1) {

wbtungsten():void{
color2.redOffset   = 70;
color2.greenOffset = 0;
color2.blueOffset  = -100;
szin_wb_cam.szin_wb.transform.colorTransform = color2;
}
}
if (myrandom == 2) {
 // another something
}

and couple lines below:

nav_mc.navtungsten.addEventListener(MouseEvent.CLICK, wbtungsten1);
private function wbtungsten1(e:MouseEvent)
{
wbtungsten();
}

In that case i get this errors: Scene 1, Layer 'AS3', Frame 2, Line 23 1084: Syntax error: expecting rightbrace before semicolon. Scene 1, Layer 'AS3', Frame 2, Line 21 1078: Label must be a simple identifier. Scene 1, Layer 'AS3', Frame 2, Line 22 1084: Syntax error: expecting colon before dot.

2
What errors do you get? Where do have the if statements? Inside a function? Is this all in the timeline or different .as files? Please update your question with this info instead of replying to this comment. - Neal Davis
Since replacing the function with a different command works, the problem is not probably in any of what you have posted, but rather in the functions themselves. Can you post an example of these functions? - Neal Davis
okay thanks!!! i updated the question :-) - Ben the Photo
Please edit your question and include the error message you get. - null
okay everything is updated! - Ben the Photo

2 Answers

1
votes

(1)
Regarding : "The function works if i click on the button" but somehow you cannot call it using myFunction1(null); or just myFunction1();. This tells me your function looks something like this

function myFunction1(event:MouseEvent):void

because it is associated with mouse clicks it can only run via a "click" event being noticed. To be able able to run the function regardless of clicks or direct calling (eg: myFunction1();) then define as...

function myFunction1(event:Event = null):void

(2)
I haven't tested your code but I would guess this section would cause a problem.

if (myrandom == 1) {

wbtungsten():void{
color2.redOffset   = 70;
color2.greenOffset = 0;
color2.blueOffset  = -100;
szin_wb_cam.szin_wb.transform.colorTransform = color2;
}

especially this line wbtungsten():void{ ... what are you trying to achieve with it exactly? Creating a new function inside an If statement? Stop doing that, it doesn't work as you can see.

Possible fix : Something like below should work.

if (myrandom == 1) 
{
    color2.redOffset   = 70;
    color2.greenOffset = 0;
    color2.blueOffset  = -100;
    szin_wb_cam.szin_wb.transform.colorTransform = color2;

    wbtungsten(); //call the function called "wbtungsten"
}

(3)
Regarding this quote:

"...but I wanted to exactly the same (myfunction1..2 etc) happen when I get a specific random number. For example if random number = 1 >>> do myfunction1 if random number = 2 >>> do myfunction2"

I'm a little confused about the link between these myfunction1 and wbtungsten1 and wbtungsten(); etc etc. If what you're trying to achieve is that each random number affects color2's red, green and blues offsets then you should do something like below :

if (myrandom == 1) 
{
    change_Color(70, 0, -100);
}   

if (myrandom == 2) 
{
    change_Color(10, 25, 5); //some other numbers for testing
}   

function change_Color(redNum:Number, greenNum:Number, blueNum:Number) : void
{
    color2.redOffset   = redNum;
    color2.greenOffset = greenNum;
    color2.blueOffset  = blueNum;

    szin_wb_cam.szin_wb.transform.colorTransform = color2;
}

What's happening above is you call the same function from each random number but you give the function different parameters (settings) to work with. Maybe the above function change_Color achieves what your wbtungsten():void{ was trying to fulfill but I can't be sure.

...

Edit : Regarding "It is pretty simple code now... .i dont get it why it isnt working"

The problem is the structure of your code. Remember Flash Player processes code line by line downwards unless you make it "jump" to a later line. You have if statements that jumps straight into a function. Since the function references a variable that does not exist you will get a null. I can see you declared var color2:ColorTransform = new ColorTransform(); but the "jump" skipped over it so color2.redOffset = 70; means nothing to the compiler.

Solution :
Put this line (shown below) before any If or function stuff. In your FLA, it becomes very top/first line of code (now everything else later knows what you mean by color2 & no more null issues)...

var color2:ColorTransform = new ColorTransform();

Declare all your variables first before any other code that either adjusts those variables or goes to functions that use the variables. This way you force compiler to read (& understand) them before any further usage.

Optimal code structure should be :

1) imports - Any Imports are declared first at top of code
2) variables - Declare your variables. (if you think of a new one later just scroll back here to add it)
3) main code - Instruction code involving variables & functions...
4) functions - Main code will reference or jump to here whenever need before returning to do next known line of instructions in main code at (3)..

0
votes

It's because your function is expecting an event.

Do this instead:

myFunction1():void{
    //do stuff
}

addEventListener(MouseEvent.CLICK,doSomethingHelper);

private function doSomethingHelper(e:MouseEvent){
    myFunction1();
}

update

I see in your updated code you are creating a function inside of an if block. That makes it inaccessible to the rest of your code. Don't do that. It's your wbtungsten function. Create it elsewhere, then call it inside the if block like wbtungsten();