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.