View Full Version : Passing values between classes
anomite
10-24-2008, 03:59 AM
It seems like this is a common, easy question but I can't find quite the right answer.
I have two classes - myBox, which extends Sprite and myBoxButton, which extends SimpleButton. I also have a text field with a value in it inside myBox.
RadioButton and TextField are instantiated by Radio. When the instance of RadioButton is pressed, I want it to modify the value inside the TextField. Basically I just have a little box with a button on it. When the button is pressed it increments the value in the TextField by 1. But I want the button to be a seperate class so I can use it on other boxes.
What is the best way to do this and how to I communicate between the two classes? I don't need specific code, I just need to be pointed in the right direction. Do I use Events? Do I pass values between another class somehow? Thanks so much!
Essientially using getters and setters. Each class should have a public method to set a value within itself and a public method to return that value to the outside world. In its simplest form, its 2 functions.
anomite
10-24-2008, 05:59 PM
Thanks, that's exactly what I needed to know.
anomite
10-26-2008, 10:02 PM
Ok, so I'm trying to do this and I'm confused.
I have a class, Freq, that basically retrieves and modifies a single value, freq.
package
{
public class Freq
{
var freq:uint = 345;
public function getFreq()
{
return freq;
}
public function setFreq(freqSet)
{
freq = freqSet;
}
}
}
If I trace freq inside setFreq it shows the appropriate value that I passed in. I can change it endlessly and everything works great. My question is why when I change the value of freq inside setFreq does it not change the value of freq inside getFreq? Retrieving freq with getFreq always returns the initial value of 345. How do I update the value of freq to what I passed in so that it can be returned? Thanks!
Quick test, this works as it should
package
{
public class Freq
{
private var freq:uint;
public function getFreq() : uint
{
return freq;
}
public function setFreq( freqSet:uint ) : void
{
freq = freqSet;
}
}
}
anomite
10-28-2008, 02:14 AM
I must have a problem somewhere else because it still isn't working for me. Tracing freq in setFreq returns the number I want -- but tracing freq in getFreq will only return the starting value.
I'm calling getFreq() on every ENTER_FRAME with an event listener. Could that be interfering somehow? Maybe that's not the best way to refresh the text field. I really appreciate this help.
Could you show that code?
anomite
10-28-2008, 02:32 PM
Sure, no problem.
Here's the Radio() class that contains the components. My plan was that it would check the value of freq in the Freq() class and update freqField if it was changed with the RadioButton instance. The frame rate is 30.
public function Radio()
{
addEventListener(Event.ENTER_FRAME, refreshFreq);
}
private function refreshFreq(e:Event):void
{
currentFreq = myFreq.getFreq();
freqField.text = currentFreq + " kHz";
}
And then here's the RadioButton() class
public function RadioButton() : void
{
addEventListener(MouseEvent.CLICK, handleClick);
}
private function handleClick(event:MouseEvent):void
{
freqNum++;
myFreq.setFreq(freqNum);
}
Thanks!
Where are the classes declared? They may be referencing two different instances of the class.
anomite
10-28-2008, 03:58 PM
That must be the problem. I'm declaring myFreq in both Radio() and RadioButton(), so they are different instances. So how do I use the Freq() class in both Radio() and RadioButton() without instantiating it twice? Do I declare it in my Main() class and use it somehow throughout?
Declare it your main class and persist it throughout there.
anomite
10-29-2008, 01:27 AM
Sorry, I'm pretty new to this. If I create an instance in my main class, how do I access that in other classes? I've tried creating it at the package level and class level and I can't access it. Seems like I'm either very confused about scope or missing something else really easy. Again, thanks.
anomite
10-29-2008, 04:19 PM
the document class:
package
{
public class Main extends Sprite
{
var myRadio:Radio = new Radio();
var myFreq:Freq = new Freq();
public function Main()
{
addChild(myRadio);
}
}
}
the Freq class
package
{
public class Freq
{
var freq:uint;
public function getFreq() : uint
{
return freq;
}
public function setFreq( freqSet:uint ) : void
{
freq = freqSet;
}
}
}
the radio, which contains the text field
package
{
public class Radio extends Sprite
{
var freqField:TextField = new TextField();
var currentFreq:uint;
var myButton:RadioButton = new RadioButton();
public function Radio()
{
addEventListener(Event.ENTER_FRAME, refreshFreq);
addChild(freqField);
addChild(myButton);
}
private function refreshFreq(e:Event):void
{
// this is wrong and where i'm confused. how do i access the myFreq object
// that was declared in the main class?
currentFreq = myFreq.getFreq();
freqField.text = currentFreq + " kHz";
}
}
}
then the button on the radio
package
{
public class RadioButton extends SimpleButton
{
var freqNum:uint;
public function RadioButton() : void
{
addEventListener(MouseEvent.CLICK, handleClick);
}
private function handleClick(e:MouseEvent):void
{
freqNum++;
//this is wrong. how do I access the myFreq object declared
//in the main class?
myFreq.setFreq(freqNum);
}
}
}
thanks. I'm really trying to get the foundations correct before I jump ahead too far.
This is where things get a little tricky. You'll to start getting into events, maybe even custom events, to trigger and pass this data around. Unfortunately, it isnt something that can be explained in a few sentences.
alcho
11-01-2008, 02:52 AM
Hi anomite,
Have you tried passing the myFreq variable into the Radio through its constructor?
Something like:
myFreq:Freq = new Freq();
myRadio:Radio = new Radio(Freq);
and have the radio constructor save the freq to be used in the refreshFreq method, ie..
private var localFreq;
Radio(myFreq:Freq){
... //previous constructor code
localFreq = myFreq;
}
Or you could just create the myFreq variable from within the Radio class. As its not currently used in the DocumentClass anyway.
You could also do the same constructor passing method for the RadioButton.
This will work if the variables are passed by reference (which I think they are) and so all the myFreq will be the same. Try it and see if it works, post back your results, if not, i'd be glad to show you the EventListner method in which Cota mentioned.
-Alcho
anomite
11-03-2008, 01:12 AM
Thanks so much for the tips. I was getting really confused about instances and scope and references, etc. What I ended up doing was eliminating the Freq() class altogether, as you suggested, and creating one myFreq variable in Radio(). It looks roughly like this now:
package {
public class Radio extends Sprite
{
var currentFreq:uint;
public function Radio()
{
addEventListener(Event.ENTER_FRAME, handleUp);
}
private function handleUp(e:Event):void
{
//increments the value in freqField()
currentFreq++;
//checks the value and triggers events on certain values
checkFreq(currentFreq);
//updates the text field
freqField.text = currentFreq + " kHz";
//formats the text
setFreqStyle();
}
}
}
If I want to share currentFreq's value with another object I'm sort of back where I started, but I think your suggestion of passing through the constructor is right where I'd start. Thanks.
alcho
11-03-2008, 09:44 AM
Another object could have access to it with a get method, ie
public function getCurrentFreq():unit{
return this.currentFreq;
}
As long as it has access to that myRadio object,
myRadio.getCurrentFreq();
mrshappy
11-08-2008, 06:29 AM
If I want to share currentFreq's value with another object I'm sort of back where I started, but I think your suggestion of passing through the constructor is right where I'd start
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.