• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 926
  • Last Modified:

Actionscript 2 Variable Listener

This is a rather simple concept, Im not sure how to generate the proper code.

I simply need a variable listener for my variable (_root.openimage)

and a switch/case statement for the conditions.

So incase the variable changes, the listener will know and prepare the appropriate function.

Help! This is rather urgent.
0
webrhp
Asked:
webrhp
  • 2
1 Solution
 
webrhpAuthor Commented:
My rookie attempt code is below, help!
// Guide ///
// _root.galleryName = name of the gallery selected to determine pages
// _root.openimage = name of the image loaded when thumbnail clicked
 
// Hide Arrows Initially //
_root.scenage.work_content.work_panels.arrow_left._visible = false;
_root.scenage.work_content.work_panels.arrow_right._visible = false;
 
 
var imageCheck:Object = new Object();
 
imageCheck = function():Void {
 
if(_root.galleryName == "bedelicious") {
	
	switch (_root.openimage) {
		
		case "" :
			_root.scenage.work_content.work_panels.arrow_left._visible = false;
			_root.scenage.work_content.work_panels.arrow_right._visible = true;
			// Hide Page 01
			_root.scenage.work_content.work_panels.work_donnakaranfragrances_pg01._visible = true;
			// Show Page 02
			_root.scenage.work_content.work_panels.work_donnakaranfragrances_pg02._visible = false;
			// Fade in Page 02
			new mx.transitions.Tween(_root.scenage.work_content.work_panels.work_donnakaranfragrances_pg01, "_alpha", mx.transitions.easing.Strong.easeOut, 0, 100, 2, true);
		break;
		
		case "img/bedelicious_01.jpg" :
			// Left
			_root.scenage.work_content.work_panels.arrow_left.onPress = function() {
				_root.scenage.work_content.work_panels.arrow_left._visible = false;
				_root.scenage.work_content.work_panels.arrow_right._visible = true;
			}
			// Right
			_root.scenage.work_content.work_panels.arrow_right.onPress = function() {
				_root.scenage.work_content.work_panels.arrow_left._visible = true;
				_root.scenage.work_content.work_panels.arrow_right._visible = true;
				_root.openimage = "img/bedelicious_02.jpg";
				_root.scenage.work_content.view_image.gotoAndPlay(11);
			}
		break;
		case "img/bedelicious_02.jpg" :
			// Left
			_root.scenage.work_content.work_panels.arrow_left.onPress = function() {
				_root.scenage.work_content.work_panels.arrow_left._visible = true;
				_root.scenage.work_content.work_panels.arrow_right._visible = true;
				_root.openimage = "img/bedelicious_01.jpg";
				_root.scenage.work_content.view_image.gotoAndPlay(11);
			}
			// Right
			_root.scenage.work_content.work_panels.arrow_right.onPress = function() {
				_root.scenage.work_content.work_panels.arrow_left._visible = true;
				_root.scenage.work_content.work_panels.arrow_right._visible = true;
				_root.openimage = "img/bedelicious_03.jpg";
				_root.scenage.work_content.view_image.gotoAndPlay(11);
			}
		break;
		case "img/bedelicious_03.jpg" :
			// Left
			_root.scenage.work_content.work_panels.arrow_left.onPress = function() {
				_root.scenage.work_content.work_panels.arrow_left._visible = true;
				_root.scenage.work_content.work_panels.arrow_right._visible = true;
				_root.openimage = "img/bedelicious_02.jpg";
				_root.scenage.work_content.view_image.gotoAndPlay(11);
			}
			// Right
			_root.scenage.work_content.work_panels.arrow_right.onPress = function() {
				_root.scenage.work_content.work_panels.arrow_left._visible = true;
				_root.scenage.work_content.work_panels.arrow_right._visible = true;
				_root.openimage = "img/bedelicious_04.jpg";
				_root.scenage.work_content.view_image.gotoAndPlay(11);
			}
		break;
		case "img/bedelicious_04.jpg" :
			// Left
			_root.scenage.work_content.work_panels.arrow_left.onPress = function() {
				_root.scenage.work_content.work_panels.arrow_left._visible = true;
				_root.scenage.work_content.work_panels.arrow_right._visible = true;
				_root.openimage = "img/bedelicious_03.jpg";
				_root.scenage.work_content.view_image.gotoAndPlay(11);
			}
			// Right
			_root.scenage.work_content.work_panels.arrow_right.onPress = function() {
				_root.scenage.work_content.work_panels.arrow_left._visible = true;
				_root.scenage.work_content.work_panels.arrow_right._visible = true;
				_root.openimage = "img/bedelicious_05.jpg";
				_root.scenage.work_content.view_image.gotoAndPlay(11);
			}
		break;
		case "img/bedelicious_05.jpg" :
			// Left
			_root.scenage.work_content.work_panels.arrow_left.onPress = function() {
				_root.scenage.work_content.work_panels.arrow_left._visible = true;
				_root.scenage.work_content.work_panels.arrow_right._visible = true;
				_root.openimage = "img/bedelicious_04.jpg";
				_root.scenage.work_content.view_image.gotoAndPlay(11);
			}
			// Right
			_root.scenage.work_content.work_panels.arrow_right.onPress = function() {
				_root.scenage.work_content.work_panels.arrow_left._visible = true;
				_root.scenage.work_content.work_panels.arrow_right._visible = false;
			}
		break;
 
	} // switch _root.openimage 
	
} // if _root.galleryName
 
}
 
click.addListener(imageCheck);

Open in new window

0
 
webrhpAuthor Commented:
Anyone out there????
0
 
CraybeCommented:
The below code should do the job.  I did make some additional changes in your code to remove redundancy.

Rather than calling to change the visibility of the left & right buttons and calling the play function I made a function (opentheimage) specifically to do that.  

To place a listener on a variable or rather a Watch on a variable it needs to be an attribute of an Object.  First we create _root.openimage as an Object then we create a function to call when it's value changes (in this case I named it imageWatch) and then we set the watch up.

The code is commented and hopefully will make sense.  If you need further help let me know.
// Guide ///
// _root.galleryName = name of the gallery selected to determine pages
// _root.openimage = name of the image loaded when thumbnail clicked
 
// Hide Arrows Initially //
_root.scenage.work_content.work_panels.arrow_left._visible = false;
_root.scenage.work_content.work_panels.arrow_right._visible = false;
 
// Set up the listener for _root.openimage openimage has to be an object for watch to work
// we will set up the object to be openimage and the value to be stored in openimage.image
 
_root.openimage = new Object()
 
var imageWatch:Function = function(prop, oldVal, newVal){
	trace("your image variable has changed to: "+newVal)
}
_root.openimage.watch("image", imageWatch);
 
 
//this function will take out some of the reduncancy in your case statement below
//this isn't necessary but it will make your life easier if you need to make changes
// Args: 1: OpenImage Value. 2: Left Button On/Off (True/False). 3: Right Button On/Off (True/False)
function opentheimage(openimageval,lefton,righton){
	//set the left arrow to visible or not
	_root.scenage.work_content.work_panels.arrow_left._visible = lefton;
	_root.scenage.work_content.work_panels.arrow_right._visible = righton;
	//change the open image and play
	if(openimageval<>null){
		//as openimage must be an object for the listner we will store it's value in openimage.image
		_root.openimage.image = openimageval;
		_root.scenage.work_content.view_image.gotoAndPlay(11);
	}
}
 
 
var imageCheck:Object = new Object();
 
imageCheck = function():Void {
 
if(_root.galleryName == "bedelicious") {
        
        switch (_root.openimage) {
                
                case "" :
                        _root.scenage.work_content.work_panels.arrow_left._visible = false;
                        _root.scenage.work_content.work_panels.arrow_right._visible = true;
                        // Hide Page 01
                        _root.scenage.work_content.work_panels.work_donnakaranfragrances_pg01._visible = true;
                        // Show Page 02
                        _root.scenage.work_content.work_panels.work_donnakaranfragrances_pg02._visible = false;
                        // Fade in Page 02
                        new mx.transitions.Tween(_root.scenage.work_content.work_panels.work_donnakaranfragrances_pg01, "_alpha", mx.transitions.easing.Strong.easeOut, 0, 100, 2, true);
                break;
                
                case "img/bedelicious_01.jpg" :
                        // Left
                        _root.scenage.work_content.work_panels.arrow_left.onPress = function() {
                                 opentheimage(null,false,true)
                        }
                        // Right
                        _root.scenage.work_content.work_panels.arrow_right.onPress = function() {
                                 opentheimage("img/bedelicious_02.jpg",true,true)
                        }
                break;
                case "img/bedelicious_02.jpg" :
                        // Left
                        _root.scenage.work_content.work_panels.arrow_left.onPress = function() {
                                opentheimage("img/bedelicious_01.jpg",true,true)
                        }
                        // Right
                        _root.scenage.work_content.work_panels.arrow_right.onPress = function() {
                                opentheimage("img/bedelicious_03.jpg",true,true)
                        }
                break;
                case "img/bedelicious_03.jpg" :
                        // Left
                        _root.scenage.work_content.work_panels.arrow_left.onPress = function() {
                                opentheimage("img/bedelicious_02.jpg",true,true)
                        }
                        // Right
                        _root.scenage.work_content.work_panels.arrow_right.onPress = function() {
								opentheimage("img/bedelicious_04.jpg",true,true)
                        }
                break;
                case "img/bedelicious_04.jpg" :
                        // Left
                        _root.scenage.work_content.work_panels.arrow_left.onPress = function() {
                                opentheimage("img/bedelicious_03.jpg",true,true)
                        }
                        // Right
                        _root.scenage.work_content.work_panels.arrow_right.onPress = function() {
                                opentheimage("img/bedelicious_05.jpg",true,true)
                        }
                break;
                case "img/bedelicious_05.jpg" :
                        // Left
                        _root.scenage.work_content.work_panels.arrow_left.onPress = function() {
                                opentheimage("img/bedelicious_04.jpg",true,true)
                        }
                        // Right
                        _root.scenage.work_content.work_panels.arrow_right.onPress = function() {
                                opentheimage(null,true,false)
                        }
                break;
 
        } // switch _root.openimage 
        
} // if _root.galleryName
 
}
 

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now