To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here


A Flash Developer Resource Site

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Reply
 
Thread Tools Search this Thread Display Modes
Old 08-23-2007, 07:22 PM   #1
Thomas D.Garrod
Instructional Designer
 
Join Date: Feb 2005
Location: Whidbey Island: north of Seattle, east of Victoria, south of Vancouver
Posts: 297
[F8] Why does the variable lose definition?

I done tried and tried to fix this thing, but for some reason, activating my navigation renders my reference variables (goalX and goalY) “undefined.” I would post these files, but they have confidential data. If necessary, I can replace the bmps with garbage. Hopefully, if I give the code and the debug output, someone can crack this puzzle.

There are four containers holding SWFs in this movie. One container (nav) holds the navigation, another (dataFile) holds an Excel spreadsheet bmp (this SWF delivers the values for the x and y arrays), another (head) holds the column headings, and a forth container holds tab buttons. The nav container includes navigation with four buttons, one for each direction. An _root array for x and another for y holds the various positions for head and dataFile (loaded from the dataFile container) .

Here is the _root code:
Code:
var i:Number = 0;
var u:Number = 0;
var goalY:Number = 150;
var goalX:Number = 0;
var myXArray:Array = new Array;
myXArray=[u];
var myYArray:Array = new Array;
myYArray=[i];
nav._alpha=35;
nav.onRollOver=function(){
	nav._alpha=100;
};
nav.onRollOut=function(){
	nav._alpha=25;
};
//Loading the first container++++++++++++++++++++++++++++++++++++++++++++++++++
this.createEmptyMovieClip("dataFile", this.getNextHighestDepth());
var mc1Listener:Object = new Object();
mc1Listener.onLoadInit = function(_loadedMC1:MovieClip) {
	if (_loadedMC1 == _root.dataFile){ 
		_loadedMC1._x = 0;
		_loadedMC1._y = 150;
		trace(dataFile._y);
		}else{
		trace("I'm afraid _loadedMC1 didn't load, or is not defined!" + _loadedMC1);
	}
};
var MCdata:MovieClipLoader = new MovieClipLoader();
MCdata.addListener(mc1Listener);
MCdata.loadClip("Summary.swf",this.dataFile);
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//Loading the second container++++++++++++++++++++++++++++++++++++=
this.createEmptyMovieClip("head", this.getNextHighestDepth());
var mc2Listener:Object = new Object();
mc2Listener.onLoadInit = function(_loadedMC2:MovieClip) {
	if (_loadedMC2 == _root.head){ 
		_loadedMC2._x = 0;
		_loadedMC2._y = 0;
		}else{
			trace("I'm afraid _loadedMC2 didn't load, or is not defined!" + _loadedMC2);
	}
};
var mcHead:MovieClipLoader = new MovieClipLoader();
mcHead.addListener(mc2Listener);
mcHead.loadClip("SummaryHead.swf",this.head);
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//Loading the third container++++++++++++++++++++++++++++++++++++=
this.createEmptyMovieClip("tabs", this.getNextHighestDepth());
var mc3Listener:Object = new Object();
mc3Listener.onLoadInit = function(_loadedMC3:MovieClip) {
	if (_loadedMC3 == _root.tabs){ 
		_loadedMC3._x = 0;
		_loadedMC3._y = 514;
		}else{
			trace("I'm afraid _loadedMC3 didn't load, or is not defined!" + _loadedMC3);
	}
};
var mcTabs:MovieClipLoader = new MovieClipLoader();
mcTabs.addListener(mc3Listener);
mcTabs.loadClip("TabControl.swf",this.tabs);
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//Loading the forth container++++++++++++++++++++++++++++++++++++=
this.createEmptyMovieClip("nav", this.getNextHighestDepth());
var mc4Listener:Object = new Object();
mc4Listener.onLoadInit = function(_loadedMC4:MovieClip) {
	if (_loadedMC4 == _root.nav){ 
		_loadedMC4._x = 300;
		_loadedMC4._y = 20;
		//_loadedMC4.swapDepths(_root.getNextHighestDepth());
	}else{
		trace("I'm afraid _loadedMC4 didn't load, or is not defined!" + _loadedMC4);
	}
};
var mcNav:MovieClipLoader = new MovieClipLoader();
mcNav.addListener(mc4Listener);
mcNav.loadClip("NavController.swf",this.nav);
Here is the code loaded from the dataFile SWF:
Code:
Stage.align="TL";
_root.myXArray=(0, -34, -81, -128, -176, -162, -223, -273, -323);
_root.myYArray=(150, -264);
Here is the code loaded from the nav container’s SWF:
Code:
//This file loads to one of four containers on the main timeline. This file allows the user 
//to move the dataFile container holding the spreadsheet data rows. The onEnterFrame function
// references a root an array on a file loaded to the dataFile container. Each time the user
//click an arrow, the variable for array position changes the goalY or goalX variable on the main timeline.
//These two variables control the scroll functionality for both the dataFile and the head containers.

function slideY() {
	_root.dataFile.onEnterFrame = function() {
		_root.dataFile.diff = Math.abs(_root.dataFile._y-_root.goalY);
		if (_root.dataFile.diff<1) {
			_root.dataFile._y = _root.goalY;
			_root.head._y = _root.goalY;
			delete _root.dataFile.onEnterFrame;
		} else {
			_root.dataFile._y += (_root.goalY-_root.dataFile._y)*.3;
			_root.head._y += (_root.goalY-_root.head._y)*.3;
		}
	}
};

function slideX() {
	_root.dataFile.onEnterFrame = function() {
		_root.dataFile.diff = Math.abs(_root.dataFile._x-_root.goalX);
		if (_root.dataFile.diff<1) {
			_root.dataFile._x = _root.goalX;
			_root.head._x = _root.goalX;
			delete _root.dataFile.onEnterFrame;
		} else {
			_root.dataFile._x += (_root.goalX-_root.dataFile._x)*.3;
			_root.head._x += (_root.goalX-_root.head._x)*.3;
		}
	}
};
//These functions set the goal variables and activates scrolling (above)
downM.onRelease = function() {
	_root.myYArray[i++];
	_root.goalY = myYArray;
	slideY();
};
upM.onRelease = function(){
	_root.myYArray[i--];
	_root.goalY = myYArray;
	slideY();
};
rightM.onRelease = function() {
	_root.goalX = _root.myXArray[u++];
	slideX();
};
leftM.onRelease = function(){
	_root.goalX = _root.myXArray[u--];
	slideX();
};
//These functions animate the arrows to give the user notice of function
//I initially nested this code with the arrows symbol, but there was some 
//conflict preventing that from working.  Instead of figuring it out, just moved 
//them to this timeline. The negative: 8 functions instead of 2. The positive: this works.

leftM.onRollOver=function(){
	leftM.play();
}
leftM.onRollOut=function(){
	leftM.gotoAndStop("home");
}
rightM.onRollOver=function(){
	rightM.play();
}
rightM.onRollOut=function(){
	rightM.gotoAndStop("home");
}
upM.onRollOver=function(){
	upM.play();
}
upM.onRollOut=function(){
	upM.gotoAndStop("home");
}
downM.onRollOver=function(){
	downM.play();
}
downM.onRollOut=function(){
	downM.gotoAndStop("home");
}
Head carried no script.


I have attached the debug output after I’ve clicked on the buttons. Obviously, the containers do not move as intended. But why?
__________________
Tomas

Last edited by Thomas D.Garrod; 10-21-2007 at 01:34 PM.
Thomas D.Garrod is offline   Reply With Quote
Old 08-24-2007, 12:11 AM   #2
a_modified_dog
FK'n_dog
 
a_modified_dog's Avatar
 
Join Date: Apr 2003
Location: "aaarf"
Posts: 9,176
in you root code you declare your array -
PHP Code:
var myXArray:Array = new Array;
trace(typeof myXArray); // an array is an object
Variable _level0.myXArray = [object #1, class 'Array'] []
in your output text, myXArray is shown to compile as -
PHP Code:
Variable _level0.myXArray = -323
trace
(typeof myXArray); // number, not an array
your problem [edit - at least one of them] lies in the datafile swf -
PHP Code:
//_root.myXArray=(0, -34, -81, -128, -176, -162, -223, -273, -323);
//_root.myYArray=(150, -264);

/* using () creates-
Variable _level0.XArray = -323
Variable _level0.YArray = -264
*/

_root.myXArray=[0, -34, -81, -128, -176, -162, -223, -273, -323];
_root.myYArray=[150, -264];

/* whereas using [] creates-
Variable _level0.myXArray = [object #1, class 'Array'] [
    0:0,
    1:-34,
    2:-81,
    3:-128,
    4:-176,
    5:-162,
    6:-223,
    7:-273,
    8:-323
  ]
Variable _level0.myYArray = [object #2, class 'Array'] [
    0:150,
    1:-264
  ]
*/
I have used a simple fla as a tester -
PHP Code:
myXArray = [0];
myYArray = [0];

this.createEmptyMovieClip("mc", 1);
var
mc1:Object = new Object();
mc1.onLoadInit = function() {
trace(myXArray[4]);  trace(myYArray[0]); //-176 150
};

var
MCL:MovieClipLoader = new MovieClipLoader();
MCL.addListener(mc1);
MCL.loadClip("datafile.swf",mc);
hth

Last edited by a_modified_dog; 08-24-2007 at 12:25 AM. Reason: see edit :)
a_modified_dog is offline   Reply With Quote
Old 08-24-2007, 10:58 AM   #3
Thomas D.Garrod
Instructional Designer
 
Join Date: Feb 2005
Location: Whidbey Island: north of Seattle, east of Victoria, south of Vancouver
Posts: 297
Yes. You are right on with that one. I was surprised that typecasting didn't catch my problem. It seems like a throw back to 1.0 where you could change the casting by just making the variable equal some new data type.

Unfortunately, fixing this problem does not change the problem with goalY and goalX. They remain undefined (I've shown the first few lines of output after implementing the correction to the array value assignment).
Quote:
Level #0:
Variable _level0.$version = "WIN 9,0,45,0"
Variable _level0.i = 0
Variable _level0.u = 0
Variable _level0.goalY = undefined
Variable _level0.goalX = undefined
Variable _level0.myXArray = [object #1, class 'Array'] [
0:0,
1:-34,
2:-81,
3:-128,
4:-176,
5:-162,
6:-223,
7:-273,
8:-323
]
Variable _level0.myYArray = [object #2, class 'Array'] [
0:150,
1:-264
]
Variable _level0.mc1Listener = [object #3, class 'Object'] {
onLoadInit:[function 'onLoadInit']
}
This may suggest the solution, I just need to ponder this for a few more minutes. I'll let you know as soon as the light comes on.
__________________
Tomas
Thomas D.Garrod is offline   Reply With Quote
Old 08-24-2007, 11:15 AM   #4
Thomas D.Garrod
Instructional Designer
 
Join Date: Feb 2005
Location: Whidbey Island: north of Seattle, east of Victoria, south of Vancouver
Posts: 297
Thinking out loud

Keep in mind that goalX and goalY are typecast as Number. They are initially set to a value of zero. If I run debug: variables, the output shows the value of each defined as zero. But if I click on a button, and then run the variables, the output shows the affected variable as undefined.

This suggests that the code for the button:

Code:
downM.onRelease = function() {
	_root.goalY = _root.myYArray[i++];//This should set goalY to the value of i++
	slideY();
};
upM.onRelease = function(){
	_root.goalY = _root.myYArray[i--];
	slideY();
};
rightM.onRelease = function() {
	_root.goalX = _root.myXArray[u++];
	slideX();
};
leftM.onRelease = function(){
	_root.goalX = _root.myXArray[u--];
	slideX();
};
Or the code for my slide functions, is causing the problem.

Code:
function slideY() {
	_root.dataFile.onEnterFrame = function() {
		_root.dataFile.diff = Math.abs(_root.dataFile._y-_root.goalY);
		if (_root.dataFile.diff<1) {
			_root.dataFile._y = _root.goalY;
			_root.head._y = _root.goalY;
			delete _root.dataFile.onEnterFrame;
		} else {
			_root.dataFile._y += (_root.goalY-_root.dataFile._y)*.3;
			_root.head._y += (_root.goalY-_root.head._y)*.3;
		}
	}
};

function slideX() {
	_root.dataFile.onEnterFrame = function() {
		_root.dataFile.diff = Math.abs(_root.dataFile._x-_root.goalX);
		if (_root.dataFile.diff<1) {
			_root.dataFile._x = _root.goalX;
			_root.head._x = _root.goalX;
			delete _root.dataFile.onEnterFrame;
		} else {
			_root.dataFile._x += (_root.goalX-_root.dataFile._x)*.3;
			_root.head._x += (_root.goalX-_root.head._x)*.3;
		}
	}
};
__________________
Tomas
Thomas D.Garrod is offline   Reply With Quote
Old 08-24-2007, 11:21 AM   #5
Thomas D.Garrod
Instructional Designer
 
Join Date: Feb 2005
Location: Whidbey Island: north of Seattle, east of Victoria, south of Vancouver
Posts: 297
This was one of those stupidly confident leaps of faith. I declared my arrays and at the same time set them to equal the value of the variable (u, i). I wonder if this is a source of problem? (Probably not, since the array does not show a problem in the debug output.)

Code:
var myXArray:Array = new Array;
myXArray=[u];
var myYArray:Array = new Array;
myYArray=[i];
This seems logical, but I have learned to doubt that basis.
__________________
Tomas
Thomas D.Garrod is offline   Reply With Quote
Old 08-24-2007, 01:11 PM   #6
dawsonk
:
 
Join Date: Dec 2002
Posts: 2,557
Maybe try something like this...

Code:
downM.onRelease = function() {
	_root.goalY = _root.myYArray[_root.i++];
	slideY();
};
upM.onRelease = function(){
	_root.goalY = _root.myYArray[_root.i--];
	slideY();
};
rightM.onRelease = function() {
	_root.goalX = _root.myXArray[_root.u++];
	slideX();
};
leftM.onRelease = function(){
	_root.goalX = _root.myXArray[_root.u--];
	slideX();
};
dawsonk is offline   Reply With Quote
Old 08-24-2007, 02:09 PM   #7
a_modified_dog
FK'n_dog
 
a_modified_dog's Avatar
 
Join Date: Apr 2003
Location: "aaarf"
Posts: 9,176
or break it down even further to check the logic -
PHP Code:
downM.onRelease = function() {
varInc = _root.i++;  trace(varInc);
_root.goalY = _root.myYArray[varInc];  trace(_root.goalY); //
slideY(_root.goalY);
};

function
slideY(num) {
trace(num);
_root.dataFile.onEnterFrame = function() {
_root.dataFile.diff = Math.abs(_root.dataFile._y - num);
trace(_root.dataFile.diff);
....
}
a_modified_dog is offline   Reply With Quote
Old 08-24-2007, 03:03 PM   #8
Thomas D.Garrod
Instructional Designer
 
Join Date: Feb 2005
Location: Whidbey Island: north of Seattle, east of Victoria, south of Vancouver
Posts: 297
dawsonk's tip was right-on. Adding _root to the u and i, made the difference. Now I need to fix the lack of limits. The scrolling worked initially and then erratcally, then not at all. Debug showed that the index was at a negative position.

But this is progress!

Dog, you are quite right to provide debug guidance. These tips are greatly appreciated (I want to catch my own fish).
__________________
Tomas
Thomas D.Garrod is offline   Reply With Quote
Reply

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 08:26 PM.


internet.commerce
Be a Commerce Partner
 »  »  »  »  »  »  »
 »  »  »  »  »  »
 

    

Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.