Groups | Blog | Home
all groups > flash actionscript > july 2006 >

flash actionscript : Question --Function.call--


sobaka11
7/2/2006 10:52:26 PM
I have a class:

class My {
var keyListener:Object = new Object();
function My() {
keyListener.onKeyDown = function() {
lala();
};
keyListener.onKeyUp = function() {
};
Key.addListener(keyListener);
}
function lala() {
trace("my");
}
}

I want that, when user will press a key the function lala() will be called.
But the function doesn't run and I don't see message "my" in Output Window.
I have heard that I must use function Function.call() to fix this problem.
Can you advise me how to resolve this problem?
LuigiL
7/3/2006 8:04:48 AM
Why create a new object (keyListener) when you already have an object (My) that
can listen for key events?

class My {

public function My() {
Key.addListener(this);
}

private function onKeyDown():Void{
lala();
}

private function lala():Void {
trace("my");
}
}
Jeckyl
7/3/2006 11:27:23 AM
That's correct behaviour .. as written the function 'lala' needs to be a
method of the keyListener object to be called like that. And its not .. its
a member of your class 'My'.

It has nothing to do with Function.call()

Instead, your keyListenter need to have a reference to the My object that
owns it so it can call the lalal() method of that My object.

eg

class My {
var keyListener:Object = new Object();
function My() {
keyListener.my = this;
keyListener.onKeyDown = function() {
my.lala();
};
keyListener.onKeyUp = function() {
};
Key.addListener(keyListener);
}
function lala() {
trace("my");
}
}
--
Jeckyl

sobaka11
7/3/2006 5:57:51 PM
sobaka11
7/4/2006 12:25:38 PM
One more question.
I have .fla file:
clip=createEmptyMovieClip("newroot", _root.getNextHighestDepth());
var a:loader=new loader(clip);

And class:

class loader {
//????? ????????? ?????????? ?? xml-?????
var xmlNode:XMLNode;
var xmlData:XML;
var n:Number, m:Number;
var k:Object;
var clipname:MovieClip;
function loader(clip:MovieClip) {
clipname = clip;
trace(clipname); //here I see the name of the clip
xmlData = new XML();
xmlData.ignoreWhite = true;
xmlData.onLoad = loadXML;
xmlData.load("doc.xml");
}
function loadXML(loaded) {
if (loaded) {
trace(clipname); //here the name is 'undefined'
k = this;
xmlNode = k.firstChild;
n = Number(xmlNode.childNodes[0].firstChild.nodeValue);
m = Number(xmlNode.childNodes[1].firstChild.nodeValue);
//var main_obj:main=new main(n,m,clipname);
}
}
}

When I load XML data I can't see the parameter from .fla file( clipname ).
What should I do to see the parameter 'clipname' in function loadXML() ?

LuigiL
7/4/2006 6:51:25 PM
Inside callback handlers class members go out of scope.
//class file
import mx.utils.Delegate;

class loader {

var xmlNode:XMLNode;
var xmlData:XML;
var n:Number, m:Number;
var k:Object;
var clipname:MovieClip;

function loader(clip:MovieClip) {
clipname = clip;
trace(clipname); //here I see the name of the clip
xmlData = new XML();
xmlData.ignoreWhite = true;
xmlData.onLoad = Delegate.create(this,loadXML);
xmlData.load("doc.xml");
}

function loadXML(loaded) {
if (loaded) {
trace(clipname); //here the name is 'undefined'
k = this;
xmlNode = k.firstChild;
n = Number(xmlNode.childNodes[0].firstChild.nodeValue);
m = Number(xmlNode.childNodes[1].firstChild.nodeValue);
//var main_obj:main=new main(n,m,clipname);
}
}
}
//end class file
AddThis Social Bookmark Button