all groups > flash actionscript > april 2005 >
You're in the

flash actionscript

group:

Why doesn't this simple class not work?!


Why doesn't this simple class not work?! svh9633
4/26/2005 12:00:00 AM
flash actionscript: (Grrrr, this server needs new hardware or something because I have to write
everything a second time, no reponse ro server :( )

Hi!
I have a class which have the private variable called cmd. I then have two
public methods. From the first I set a new value to cmd. And from the second I
send that new value. But this doesnt work.

Here is the class:

class InterfaceToVB {
// ********************************************************** //
// This class handles the communication and interface between
// the flash application and the VB platform. //
// ********************************************************** //
//
// PROPERTIES
// Progress from VB
private var cmd:String;
//
// CONSTRUCTOR
function InterfaceToVB() {
this.cmd = "sam";
}
//
// PUBLIC METHODS

//Function/Method to take care of incoming commands
public function onIncomingCommand(prop, oldVal, newVal, userData):String {
trace("onIncomingCommand");
trace(String(newVal));
this.cmd = String(newVal);
trace(this.cmd);
return ""; // watch function must have something back.
}

public function get lastCommand():String {
trace(this.cmd);
return this.cmd;
}

}

Here is the layer script:

//TopDown execution

//create interface Object.
var vbInterface:InterfaceToVB = new InterfaceToVB;

//Incoming Commands that needs to be rested in order to notice changes.
_root.command = "";

test1_btn.onPress = function() {
_root.command = "command";
}

test2_btn.onPress = function() {
watchLabel_txt.text = vbInterface.lastCommand;
}

//the watch to create the event of incoming commands.
_root.watch("command", vbInterface.onIncomingCommand);

It is so basic but the cmd value wont change
I am going nuts, please please help me
Thanks
/Sam
Re: Why doesn't this simple class not work?! svh9633
4/26/2005 12:00:00 AM
Thanks for reply.
No, because _root is an object?!
see: http://www.macromedia.com/devnet/mx/flash/articles/stock_history03.html

Anyway, it works and it seems that the watch function is not the problem. The
watch event is triggered. It is the class that doesn't work.

/Sam
Re: Why doesn't this simple class not work?! mpetty
4/26/2005 12:00:00 AM
function onIncomingCommand(prop, oldVal, newVal, userData):String {
...
return ""; // watch function must have something back.


Your problem most likely rests with the return value. When you assign a
variable to use this callback, you are essentially obliterating that variable
because it returns an empty string. Every example I've seen using object.watch
either returns the new value, or conditionalizes the new vs the old but always
returns one of the two in order to maintain state.
Re: Why doesn't this simple class not work?! David Stiller
4/26/2005 10:28:41 AM
svh9633,

[quoted text, click to view]

I'm not sure what you expect watch() to do. Watch is a method of the
Object class, not of the _root. In order to use Object.watch(), you must
reference an object instance.


David
stiller (at) quip (dot) net
"Luck is the residue of good design."

Re: Why doesn't this simple class not work?! David Stiller
4/26/2005 11:04:23 AM
Sam,

[quoted text, click to view]

Ah. Yes. Well, I'm properly embarrassed. ;)

[quoted text, click to view]

I've read your class a few times now. It's probably that I cannot
envision your context, but I just don't see what *should* be happening
versus what *is* happening (which, I guess, is nothing).

Have you been able to trigger a callback with a normal, old object?


David
stiller (at) quip (dot) net
"Luck is the residue of good design."

Re: Why doesn't this simple class not work?! svh9633
4/27/2005 12:00:00 AM
Thanks for all reponse!
I am really pleased that peope out there are trying to help me.

Ok lets go back to basics. I am telling you that it is not the watch
functionality that is the obsticle here. Just to show you I made a new flash
movie with only two layers (on for components and one for action) On this scene
I put two buttons (get_btn and set_btn) and two labels (set_txt & get_txt) I
then create my object Test which only contains a pair of set and get methods.
But the Set method doesn't work. This is BASIC action script and this should
work.

The code for the flase movle:

//top-down compiling.

//create object
var myTest:Test = new Test ();

set_btn.onPress = function () {
trace("set");
myTest.inComing(set_txt.text);
}

get_btn.onPress = function () {
trace("get");
get_txt.text = myTest.outGoing;
}

The code for the class :

class Test {
// PROPERTIES
// Progress from VB
private var cmd:String;
//
// CONSTRUCTOR
function Test() {
this.cmd = "sam";
trace("Object created");
}
//
// PUBLIC METHODS

//this method doesn't work!! Why??
public function set inComing(str:String):Void {
trace("incoming");
this.cmd = str;
trace("this.cmd");
}

//this method works but sends the constructor value.
public function get outGoing():String {
trace("outGoing");
trace(this.cmd);
return this.cmd;
}
}

Why do not the set mtehod work??
Thanks for all you help
/Sam
Re: Why doesn't this simple class not work?! mpetty
4/27/2005 12:00:00 AM
set_btn.onPress = function () {
trace("set");
myTest.inComing(set_txt.text); <<<<
}

You used the setter method incorrectly. Setters do not recognize parenthasized
function parameters. Setters / Getters are psuedo variable members. Hence the
reason why they are called "set / get" because you directly access the method
name.

ie mytest.incoming ="some new text" or trace(mytest.incoming)
Re: Why doesn't this simple class not work?! svh9633
4/27/2005 12:00:00 AM
mpetty thanks alot!

AddThis Social Bookmark Button