0

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.

2
  • Hi, any code you can post here will clarify the question, it is VERY unclear now Commented Oct 16, 2011 at 13:47
  • Do you need the random number that is generated during the click only or is it alright if you generate a new one? Commented Oct 16, 2011 at 14:26

2 Answers 2

1

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;
}
Sign up to request clarification or add additional context in comments.

3 Comments

So what you are doing is this: Click on 1 button generates a random number. Clicking on another button runs welkNummerKnop. Am I right?
Yes, I click on a button and every time I click, the generated number changes. But when I click on welkNummerKnop, it displays your current number on another dynamic textfield.
This would be the way to do it then. If NummerOpDatMoment is the name of the text field then you need to do NummerOpDatMoment.text
0

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;
}

1 Comment

This also returns nothing, because every time I hit the button, the number changes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.