#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

    GetProperty setProperty help


    Hi. i have an as2 file and i need to modify something so i need a little help.
    I post the file and i explain what i wanna do.


    Code:
    class userProfile {
    	private var _profile:Object;
    	private var _webcamViewers:Array;
    	private var _details:Array = new Array("picture", "gender", "dj", "country", "state", "city", "marital", "about", "pstatus", "year", "display_cam", "enable_sound", "allow_private");
    	function userProfile() {
    		_profile = new Object();
    		_webcamViewers = new Array();
    	}
    	
    	public function getProperty(property:String) {
    		if (_profile[property] == undefined) {
    			return "";
    		}
    		return _profile[property];		
    
    	}
    	public function setProperty(property:String, value:String) {
    		_profile[property] = value;
    	}
    	
    
    	public function reset() {
    		_profile = new Object();
    		_webcamViewers = new Array();
    	}
    }
    the setProperty - getProperty returns the values when i call them only from the user.

    That i wanna do is to give him another username or id and return the getProperty from that user and setProperty in that user.

    example:
    Code:
    getProperty(username, property) {
      return  _profile[property];
    }
    to return the property from the username i send.
    the same for the setProperty:
    Code:
    setProperty(username, property, value) {
      _profile[property] = value; 
    }
     to set the property in the username i send.
    Thanks a lot for any help!

    Regards.
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Location
    Pennsylvania, USA
    Posts
    35
    Rep Power
    1
    First of all I'd like to ask if you are aware of
    where to place the .as file and how to include it in
    the code? and only need to learn how to write the
    class code correctly.

    Thanks.

    Shawn - robowhiz.info

    p.s. I'll be logging off soon, but I'll try to post some generic code later (within a day) to accomplish what you are desiring. The comment I have right now is that the _details variable is not used by your code. My code is going to be in ActionScript 3. So, if you are looking for AS2 code, you may have to modify it.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Location
    Pennsylvania, USA
    Posts
    35
    Rep Power
    1
    OP,

    As mentioned above, here is the code you can use to accomplish what you want. The class below is written for AS3; so, you may need to modify it slightly to make it work for AS2. Also, you may add a user-delete function, which will involve shifting the _webcamViewers array.

    Here is the class:

    Code:
    package com.dynamicflashhtml5.test {
        public dynamic class userProfiles extends Object {
           private var _webcamViewers:Array;
           // Intended fields: "picture", "gender", "dj", "country", "state", "city", "marital", "about", "pstatus", "year", "display_cam", "enable_sound", "allow_private"
    
    	   function userProfiles() {
           // Constructor and initializer
    		   _webcamViewers = new Array();
            }
    
           public function getProperty(userid:String, property:String) : String {
            var curUser:Object = getProfile(userid);
            if(curUser == null)
            {
              return "Error: No Object found for username: " + userid + ".";
            }
            if (curUser[property] == undefined) {
              return "";
            }
            return curUser[property];		
           } // end getProperty()
    
           public function setProperty(userid:String, property:String, value:String) : void {
              var curUser:Object = getProfile(userid); // user doesn't exist, he/she is added
              if(curUser != null)
                 curUser[property] = value;
    	   } // end setProperty()
    
           protected function getProfile(userkey:String) : Object {
           // searches _wecamViewers array for user; returns user's Object or adds new
           // user object if user is not found.  property must be unique.
           // return null;
             var wcvSize:Number = _webcamViewers.length;
             var curObj:Object = null;
             for(var i:int=0; i < wcvSize; i++)
             {
               curObj  = _webcamViewers[i];
               if(curObj != null)
               { 
                  if(curObj['username'] == userkey)
                  return curObj; // We found a match, so return it.
               }
             } // end for loop()
              // If we reach this point in the code, no matching user was found so append'
              // to array
             var newProfile:Object = new Object();
             newProfile['username'] = userkey;
             _webcamViewers.push(newProfile); // Add to global array of profiles
             return newProfile; // return newly created Object
           } // end getProfile()
    
           public function reset() : void {
             _webcamViewers = new Array();
           } // end reset()
        } // end class userProfile
    } // end package
    Here is the test code to demonstrate that the class works:

    Code:
      import com.dynamicflashhtml5.test.userProfiles;
    
      var testData:userProfiles = new userProfiles();
    
      testData.setProperty("user1", "prop1", "val1");
      testData.setProperty("user1", "prop2", "val2");
      testData.setProperty("user1", "prop3", "val3");
      testData.setProperty("user1", "prop4", "val4");
    
      testData.setProperty("user2", "prop1", "valA");
      testData.setProperty("user2", "prop2", "valB");
      testData.setProperty("user2", "prop3", "valC");
      testData.setProperty("user2", "prop4", "valD");
    
    
      trace("get user 1, prop1:" + testData.getProperty("user1", "prop1"));
      trace("get user 1, prop2:" + testData.getProperty("user1", "prop2"));
      trace("get user 1, prop3:" + testData.getProperty("user1", "prop3"));
      trace("get user 1, prop4:" + testData.getProperty("user1", "prop4"));
    
      trace("get user 2, prop1:" + testData.getProperty("user2", "prop1"));
      trace("get user 2, prop2:" + testData.getProperty("user2", "prop2"));
      trace("get user 2, prop3:" + testData.getProperty("user2", "prop3"));
      trace("get user 2, prop4:" + testData.getProperty("user2", "prop4"));
    And finally, here is the output from the test code:

    get user 1, prop1:val1
    get user 1, prop2:val2
    get user 1, prop3:val3
    get user 1, prop4:val4
    get user 2, prop1:valA
    get user 2, prop2:valB
    get user 2, prop3:valC
    get user 2, prop4:valD
    This is just a barebones example, that you can modify as
    desired adding more functions that you need.

    Regards - Shawn - robowhiz.info
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0
    Thank you for the quick reply and sorry for the delay i was in work..

    the all code is this:


    Code:
    import com.factory;
    class com.userProfile {
    	private var _profile:Object;
    	private var _webcamViewers:Array;
    	private var _details:Array = new Array("picture", "gender", "dj", "country", "state", "city", "marital", "about", "pstatus", "year", "display_cam", "enable_sound", "allow_private");
    	function userProfile() {
    		_profile = new Object();
    		_webcamViewers = new Array();
    	}
    	
    	public function getProperty(property:String) {
    		if (_profile[property] == undefined) {
    			return "";
    		}
    		return _profile[property];		
    
    	}
    	public function setProperty(property:String, value:String) {
    		_profile[property] = value;
    	}
    	
    	public function getBoolean(property:String) {
    		if (_profile[property] == "n" || _profile[property] == "no" || _profile[property] == "0") {
    			return false;
    		}
    		return true;
    	}
    	// who is watching webcam
    	public function addViewer(username:String) {
    		if (!isWatching(username)) {
    			_webcamViewers.push(username);
    		}
    	}
    	public function delViewer(username:String) {
    		for (var i = 0; i<_webcamViewers.length; i++) {
    			if (_webcamViewers[i] == username) {
    				_webcamViewers.splice(i, 1);
    			}
    		}
    	}
    	public function isWatching(username:String) {
    		for (var i = 0; i<_webcamViewers.length; i++) {
    			if (_webcamViewers[i] == username) {
    				return true;
    			}
    		}
    		return false;
    	}
    	public function isAdult() {
    		var year:String = this.getProperty("year");
    		var access:Boolean = true;
    		if (year == undefined || year == "" || isNaN(year)) {
    			return false;
    		} else {
    			var Year:Number = Number(year);
    			var myDate:Date = new Date();
    			if (myDate.getFullYear()<Year+18) {
    				return false;
    			}
    		}
    		return true;
    	}
    	public function reset() {
    		_profile = new Object();
    		_webcamViewers = new Array();
    	}
    }
    and the factory code is this:

    Code:
    import com.*;
    import com.components.*;
    import com.clips.*;
    class com.factory {
    	private static var _instances:Object = new Object();
    	private static var _newInstances:Object = new Object();
    	static function getClass(classname:String) {
    		if (_instances[classname] == undefined) {
    			switch (classname) {
    			case 'adapter' :
    				_instances[classname] = new adapter();
    				break;
    			case 'connector' :
    				_instances[classname] = new connector();
    				break;
    			case 'characterMap' :
    				_instances[classname] = new characterMap();
    				break;
    			case 'config' :
    				_instances[classname] = new config();
    				break;
    			case 'core' :
    				_instances[classname] = new core();
    				break;
    			case 'crypt' :
    				_instances[classname] = new crypt();
    				break;
    			case 'layout' :
    				_instances[classname] = new layout();
    				break;
    			case 'sound' :
    				_instances[classname] = new sound();
    				break;
    			case 'styles' :
    				_instances[classname] = new styles();
    				break;
    			case 'init_device' :
    				_instances[classname] = new init_device();
    				break;
    			case 'modules' :
    				_instances[classname] = new modules();
    				break;
    			case 'obj_encoder' :
    				_instances[classname] = new obj_encoder();
    				break;
    			case 'preloader' :
    				_instances[classname] = new preloader();
    				break;
    			case 'userProfile' :
    				_instances[classname] = new userProfile();
    				break;
    			case 'error' :
    				_instances[classname] = new error();
    				break;
    			case 'banners' :
    				_instances[classname] = banners.getInstance();
    				break;
    			case 'tooltip' :
    				_instances[classname] = new tooltip(0xFF0000, 0xFFCC00);
    				break;
    			case 'userinfo' :
    				_instances[classname] = userinfo.getInstance();
    				break;
    			}
    		}
    		return (_instances[classname]);
    	}
    }

    i call the propertys like this from the connector.as :
    Code:
    	// ----------------------------------- //
    	private function sendProfile()
    	{
    		this.setProperty("pic",factory.getClass("userProfile").getProperty("picture"),"no");
    		this.setProperty("gender",factory.getClass("userProfile").getProperty("gender"),"no");
    	this.setProperty("country",factory.getClass("userProfile").getProperty("country"),"no");
    		this.setProperty("state",factory.getClass("userProfile").getProperty("state"),"no");
    		this.setProperty("city",factory.getClass("userProfile").getProperty("city"),"no");
    		this.setProperty("marital",factory.getClass("userProfile").getProperty("marital"),"no");
    		this.setProperty("about",factory.getClass("userProfile").getProperty("about"),"no");
    	}
    	/*
    	*
    	*/
    PS: i only want to return the getProperty - setProperty - getBoolean for the curUser not the rest isWatching , addViewer, isAdult etc ftom the profile code
    I will test asap your code!!!

IMN logo majestic logo threadwatch logo seochat tools logo