PDA

View Full Version : Object reference not stored in object member


m1cky
01-06-2005, 04:30 PM
Hello,
i have a problem with object references.
In my concrete case i create 1 object (with new), this object creates 3 another objects and passes each of them 'this' as reference to the parent for further callbacks. Here comes the ****, in the body of constructor that reference is valid, i store it in a member-variable (doesnt matter which type, never worked) as far as good. If the constructor is left and i try to access the parent object over the stored reference it does not work. If i trace the content of the member it says "undefined" !
What is the mistake ?? Pls help


Here the code :

import FHW_MenuGroup;

class FHW_Menu implements IFHW_Menu
{
var MenuGroups:Array = new Array();

function FHW_Menu()
{
InsertGroup(new FHW_MenuGroup("Button1", GetNumberOfGroups() + 1, this));
InsertGroup(new FHW_MenuGroup("Button2", GetNumberOfGroups() + 1, this));
InsertGroup(new FHW_MenuGroup("Button3", GetNumberOfGroups() + 1, this));
RearrangeGroups();
}

function InsertGroup(Group:FHW_MenuGroup)
{
MenuGroups.push(Group);
}

function RearrangeGroups()
{
var GroupIndex:Number = 0;
var NextGroupYcoo:Number = 0;
trace(GetNumberOfGroups() + " items total. Rearranging...")
for(; GroupIndex < MenuGroups.length; GroupIndex++)
{
trace("Item " + GroupIndex + " moving to 0, " + NextGroupYcoo);
MenuGroups[GroupIndex].MoveTo(0, NextGroupYcoo);
NextGroupYcoo += MenuGroups[GroupIndex].GetVerticalSize();
}
}

function GetNumberOfGroups()
{
return MenuGroups.length;
}
}


//#include "IFHW_Menu.as"

class FHW_MenuGroup
{
function FHW_MenuGroup(MovieRessourceId:String, Depth:Number, m:Object)
{
trace ("FHW_MenuGroup instantiated on depth " + Depth);
clip = _root.createEmptyMovieClip("button", Depth);
clip.attachMovie(MovieRessourceId, "Bt1", 10);
clip.onRollOver = this.ExpandMenu;
clip.onRollOut = this.ContractMenu;
menux = m;
}

function AddMenuItem(Label:String, Address:String)
{
}

function ExpandMenu()
{
trace("rolling over");
CurrentExpandSize = 30;
//undefined !!!
trace(this.menux);
trace(CurrentExpandSize);
menux.RearrangeGroups();
}

function ContractMenu()
{
trace("rolling out");
CurrentExpandSize = 0;
}

function GetVerticalSize()
{
return clip._height + CurrentExpandSize;
}

function MoveTo(x:Number, y:Number)
{
trace("Moving to: " + x + ", " + y);
clip._x = x;
clip._y = y;
}

var Items:Array = new Array();
var CurrentExpandSize:Number = 0;
var clip:MovieClip;
var menux:Object;
}

tdoublea
01-06-2005, 09:06 PM
hello and welcom m1ckey!

the reason is because it is losing scope.
even though you assign a function to the onRollOver and onRollOut methods of the member clip in FHW_MenuGroup, when within those functions(ExpandMenu, CollapseMenu) the refence is still "clip" (or _level0.button): trace(this) inside those and you will see an output of the clip instance.

I would suggest using the Delegate class if you are using 7.2.

import mx.utils.Delegate into FHW_MenuGroup,
and when you declare clip's methods:

clip.onRollOver = Delegate.create(this, ExpandMenu);
clip.onRollOut = Delegate.create(this, CollapseMenu);

now just trace(menux) instead of trace(this.menux)
b4, this.menux was trying to reference _levlel0.button.menux
now it's just referencing the member of the class

let me know if that works out.
hope it helps

-t

m1cky
01-07-2005, 01:38 PM
hello and thanks for your answer, i almost gave up looking herein :)

well im using 7.0. you mean it is impossible to save a reference to an object in a member variable ??
This sucks i would say, if this is true actionscript may not be called as object oriented !

tdoublea
01-07-2005, 02:48 PM
m1ckey,

yes, there are some quirks, but strides are being made. this is only the first delivery of AS 2, so i think there is great potential.

it is a free upgrade though to 7.2:
http://www.macromedia.com/software/flash/special/7_2updater/

i tried a couple different ideas, but i couldn't get to menux without the Delegate...

anyone else have any ideas?

-t

m1cky
01-07-2005, 04:18 PM
well, there is no way to implement back-references between objects ?? One of the standard programming patterns.

I will try Delegate and tell you if i had success, thanks :)

red penguin
01-07-2005, 05:08 PM
Not sure if this helps at all, but...some code to peruse....*maybe* it will shed some light...*maybe* not, but what the hell...

This comes directly from EAS2.0....I've used it in some small projects to handle events...

class com.foobar.EventProxy {

private var receiverObj:Object;
private var funcRef:Function;


//------------------------------------------------------------------
// receiverObj The object on which funcRef will be called
// funcRef A reference to the functio to call in response
// to the event
//------------------------------------------------------------------
function EventProxy (receiverObj:Object, funcRef:Function)
{
this.receiverObj = receiverObj;
this.funcRef = funcRef;
}


//------------------------------------------------------------------
// Invoked before the registered event is broadcast by the component
// Proxies the event call out to the receiverObj object's method
//------------------------------------------------------------------
private function handleEvent (eventObj:Object):Void
{
// If no function name has been defined...
if(funcRef == undefined){
// ... pass the call to the event name method
receiverObj[eventObj.type](eventObj);
}else{
// ... otherwise, pass the call to the specified method using
// Function.call()
funcRef.call(receiverObj, eventObj);
}
}
}

Import this from your class file...

import mx.controls.*;
import com.foobar.EventProxy;

// class code...in the Constructor
// saveData is a method of this class
// myButton is a Component
myButton.addEventListener("click", new EventProxy(this, saveData));

m1cky
01-07-2005, 05:48 PM
this works ???? I guess no because the bottom 2 lines dont work !

this.receiverObj = receiverObj;
receiverObj[eventObj.type](eventObj);

This is exactly my problem, I get an "undefined" in attempt to access an object which reference was stored in a member variable !

@tdoublea

the method ExpandMenu and CollapseMenu is called what dont work is teh call to the parent object FHW_Menu.RearrangeGroups because i cant store the back-reference to it in my FHW_MenuGroup object.

deadbeat
01-07-2005, 08:30 PM
As mentioned abovet is a matter of scope - using Delegate will probably get you around this, alternately you can store a reference to the MenuGroup object in the clip when you create it, then use the reference to get back to the menux var...


function FHW_MenuGroup(MovieRessourceId:String, Depth:Number, m:Object)
{
trace ("FHW_MenuGroup instantiated on depth " + Depth);
clip = _root.createEmptyMovieClip("button", Depth);
clip.attachMovie(MovieRessourceId, "Bt1", 10);
clip.owner=this; // create the reference
clip.onRollOver = this.ExpandMenu;
clip.onRollOut = this.ContractMenu;
menux = m;
}


Then use the reference to access menux:

function ExpandMenu()
{
trace("rolling over");
CurrentExpandSize = 30;
trace(owner.menux);
trace(CurrentExpandSize);
owner.menux.RearrangeGroups();
}


For more details on scoping & callbacks, here's a helpful link:

http://www.gskinner.com/blog/archives/000069.html

This refers to xml handlers, but the issue is the same...

HTH,

K.

m1cky
01-07-2005, 09:23 PM
ActionScript is a big piece of ****, macromedia should never have called it object oriented and relate its syntax to C++/Java because NOTHING works the way any C++ or java-programmer would expect ! Even PHP shows more authentic OO-behaviour than AS ! :mad:

Why dont that goddamn member variables keep the values that were assigned to them in the proper constructor method ??? This crap can not have anything to do with some scope they must keep the own values at instance-SCOPE !

var CurrentExpandSize:Number = 0; <---- this one has ALWAYS the value of 0 except for the method where a dirrefent value was assigned ! :confused:

m1cky
01-07-2005, 10:07 PM
ahhhhhhhhhhhhhhhhhhhhhhhh i found the bug !!!

BEWARE OF USING this !!

It contains sometimes other things than the this-object even if the name suggests this !

this is not always this, i shouldnt relate my c++/java-knowledge to this ****ing script language which just adapts alien syntax for different things !
:mad: :mad: :mad: :mad: :mad:

after deleting this. everywhere my code works as expected.. Thanks everyone who tried to help

madgett
01-07-2005, 10:49 PM
This can be a very evil thing...lol.

CyanBlue
01-07-2005, 10:57 PM
Howdy, m1cky...

Do us all favor and watch out for what you are saying in the public place... It's one thing that you don't like ActionScript... That's fine... But it is not okay to use those words in the forum where there could be many people reading the threads...

red penguin
01-10-2005, 05:53 PM
@m1cky: It's not a bug, is it? I believe this is stated in EAS 2.0.

20 Ton Squirrel
01-10-2005, 06:05 PM
It's not a bug... it's a feature.

*dodges rotten fruit being tossed on stage and scampers off*

red penguin
01-10-2005, 08:26 PM
Ha!