Reputation: 49
I've got different functions in actionscript 3, one function generates random numbers each time there's a button click. But with another button I want to call the random number which is generated at that moment. I have to call the random number from another function then, but these are two different functions so it just considers it as an undefined property.
I hope you understand what I'm trying to describe here. Thanks in advanced!
EDIT: a piece of code
I have an
var randomnummer1:Number = Math.floor(Math.random() * 6) +1;
This piece of code is inside the function
function bijMuisKnop(e:MouseEvent):void{
}
At another function I want to call the randomnummer1 at that moment.
function welkNummerKnop(e:MouseEvent):void{
NummerOpDatMoment = randomnummer1;
}
But I can't call randomnummer1 because it's inside another function. So I get the property undefined error.
Upvotes: 0
Views: 151
Reputation: 15570
Rather than declaring the variable for the random number inside the handler function you should declare it once outside, so that the variable scope is available to both functions:
var randomnummer1:Number;
function bijMuisKnop(e:MouseEvent):void
{
randomnummer1 = Math.floor(Math.random() * 6) +1;
}
function welkNummerKnop(e:MouseEvent):void
{
NummerOpDatMoment = randomnummer1;
}
Upvotes: 0
Reputation: 25489
Have a class level variable (or a global variable)
private var randomNumber:Number;
then,
function bijMuisKnop(e:MouseEvent):void{
var randomnummer1:Number = Math.floor(Math.random() * 6) +1;
randomNumber=randomnummer1; //Assign to global variable
}
function welkNummerKnop(e:MouseEvent):void{
NummerOpDatMoment = randomNumber;
}
Upvotes: 1