Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

Encapsulation, Getter and Setter methods

This is a discussion on Encapsulation, Getter and Setter methods within the Tutorials forums, part of the Flash English category; Citing Actionscript 3.0 hence the Object Oriented Programming, I can"t and don"t want to bypass the encapsulation ...


Go Back   Forum Flash CS3 Flash CS4 > Flash CS3 Flash CS4 > Flash English > Tutorials

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 09-10-07, 19:47
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
Encapsulation, Getter and Setter methods

Citing Actionscript 3.0 hence the Object Oriented Programming, I can"t and don"t want to bypass the encapsulation and the Getter and Setter methods.
The encapsulation is a technique to protect/hide the properties of our Class.
This is because we could find ourselves in the situation of having to create a Class to then pass it to a customer or a developer, but not necessarily giving direct access to the properties of it.
For those new to programming, this could sound complex, but let"s play this feeling down. I"ll explain better, we"ve seen how to define properties in our classes:
Code:
package
{
	import flash.display.MovieClip;
	
	public class Filippo extends MovieClip
	{
		private var mano:MovieClip;
		private var misura:Number;
		
		public function Filippo()
		{
			
		}
	}
}
In the Filippo class, mani and misura are 2 properties. So far we only have access to these properties the common way, i.e. directly:
Code:
package
{
	import flash.display.MovieClip;
	import flash.display.Shape;
	
	public class Filippo extends MovieClip
	{
		private var mano:Shape;
		private var misura:Number=100;
		
		public function Filippo()
		{
                        misura=400;
			mano=new Shape();
			mano.graphics.beginFill(0xFFCC00);
			mano.graphics.drawRect(0,0,misura,misura);
			mano.graphics.endFill();
			addChild(mano);
		}
	}
}
If we initialized the class from the Timeline this way:
Var filo:Filippo=new Filippo();
To give access to the mano property, we"d have to change its definition from private to public, which isn"t good going.
If we wanted give access to the properties, but indirectly ( as they are private ) we have to define methods that return those properties. Here we have 2 ways:
Code:
package
{
	import flash.display.MovieClip;
	import flash.display.Shape;
	
	public class Filippo extends MovieClip
	{
		private var mano:Shape;
		private var misura:Number=100;
		
		public function Filippo()
		{
			mano=new Shape();
			mano.graphics.beginFill(0xFFCC00);
			mano.graphics.drawRect(0,0,misura,misura);
			mano.graphics.endFill();
			addChild(mano);
		}
		
		public function dammiLaMisura():Number
		{
			return misura;
		}
		public function tieniLaMisura(n:Number):void
		{
			misura=n;
		}
	}
}
in this case we"d access the misura property from the Timeline this way:
var filippo:Filippo=new Filippo();
filippo.tieniLaMisura(400);
trace(filippo.dammiLaMisura());

or the Setter and Getter:
Code:
package
{
	import flash.display.MovieClip;
	import flash.display.Shape;
	
	public class Filippo extends MovieClip
	{
		private var mano:Shape;
		private var _misura:Number=100;
		
		public function Filippo()
		{
			mano=new Shape();
			mano.graphics.beginFill(0xFFCC00);
			mano.graphics.drawRect(0,0,misura,misura);
			mano.graphics.endFill();
			addChild(mano);
		}
		
		public function set misura(n:Number):void
		{
			_misura=n;
		}
		public function get misura():Number
		{
			return _misura;
		}
	}
}
in this case we"d access the misura property this way:
var filippo:Filippo=new Filippo();
filippo.misura=400;
trace(filippo.misura);

In both cases we"ve implemented public methods to assign or receive a value from the private properties.
Some programmers ( myself included :P ) consider the first example a little uncomfortable, as it"s simpler:
filo.misura=400; (getter&setter)
instead of
filo.tieniLaMisura(400);

Stay tuned !
__________________

 


I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !

Last edited by Flep; 28-08-08 at 06:40..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #2 (permalink)  
Old 26-10-07, 14:29
Junior Member
 
Join Date: Oct 2007
Posts: 16
Rep Power: 0
Abdul is on a distinguished road
Re: Encapsulation, Getter and Setter methods

Hi there Flep,

One question, where exactly i place the call in the timeline?

I created a *.fla file and one keyframe holding the following action:

Code:
var newObj:Abdul = new Abdul();
trace(newObj.getMisura());
newObj.setMisura(50);
trace(newObj.getMisura());
but when run, i get the square shape with an output:

Code:
Error: Error #2136: The SWF file file:///C|/Documents%20and%20Settings/Abdul/My%20Documents/tutorial/flash/abdul.swf contains invalid data.
    at Abdul/::frame1()
what's wrong?!

Many thanks,
Abdul
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 26-10-07, 14:38
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
Re: Encapsulation, Getter and Setter methods

Hi Abdul,
lemme see the code of Abdul.as
Anyway, i think this is the error:
trace(newObj.getMisura());
if you are using a getter, you must call it:
trace(newObj.getMisura);
__________________

 


I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 29-10-07, 18:39
Junior Member
 
Join Date: Oct 2007
Posts: 16
Rep Power: 0
Abdul is on a distinguished road
Re: Encapsulation, Getter and Setter methods

Hello Flep,

Here's the Abdul.as code:
Code:
package
{
    import flash.display.MovieClip;
    import flash.display.Shape;
    
    public class Abdul extends MovieClip
    {
        private var mano:Shape;
        private var misura:Number=100;
        
        public function Abdul()
        {
                        misura=400;
            mano=new Shape();
            mano.graphics.beginFill(0xFFCC00);
            mano.graphics.drawRect(0,0,misura,misura);
            mano.graphics.endFill();
            addChild(mano);
        }
        
        public function getMisura():Number
        {
            return misura;
        }
        
        public function setMisura(n:Number):void
        {
            misura=n;
        }
    }
}
I am not using getter nor setter. I was trying the first method explained in the lesson to retrieve the property.

Best Regards,
Abdul
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 29-10-07, 22:59
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
Re: Encapsulation, Getter and Setter methods

Hi Abdul,
the code you are using:
Code:
var newObj:Abdul = new Abdul();
trace(newObj.getMisura());
newObj.setMisura(50);
trace(newObj.getMisura());
is fine.
__________________

 


I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 08-02-08, 09:07
Junior Member
 
Join Date: Feb 2008
Posts: 2
Rep Power: 0
Greg767 is on a distinguished road
Re: Encapsulation, Getter and Setter methods

Hi Flep,

You said the code he is using is fine, but then where does his problem come? Because I am having the same problem, that is when I try to create an instance of a class in another one to be able to access its methods, I get that same error.
Could you explain why?
Thanks
Greg
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 08-02-08, 12:34
Junior Member
 
Join Date: Feb 2008
Posts: 2
Rep Power: 0
Greg767 is on a distinguished road
Re: Encapsulation, Getter and Setter methods

Hi Flep,

I could resolve this problem by creating a third class where the setters and getters are, and also the private variable I am accessing to.

BUT

I have another problem.
It seems that the instance I am creating from this class makes not available to change this private variable through its public getter.

So I have 3 classes, lets name it A B C. In A I have a click event listener and when clicked it should change the variable in B from 1 to 0 or the other way.

In C I have a condition that should trigger 2 different actions following the state of the variable in B.

So I have my private variable in B and public getter and setter.

I make an instance of B in C.
Since A extends from C I can use that instance of B in A.

So when clicked, in A I call the instanceB.setter to change the variable and if I trace it after it changed.

Then in C I use the same instanceB.getter in the condition to check whats the value of the variable and here instead of getting the value that was just set, I get the value of the variable how it was initialized.

So if it was initialized to 0 I get 0, etc.


Please explain me what do I do wrong.

Thanks a lot.
Greg

Tha class that I called A:
Code:
package app{
    
    import flash.events.TUIO;
    import app.core.canvas.*;
    import app.core.loader.*;
    import flash.display.MovieClip;
    import flash.events.*;
    import app.core.action.RotatableScalable;
    import Draw;
    import Hold;
    import Poi;
    
    public class MapPoint extends RotatableScalable{
         var hold_button:Hold;
         var draw_button:Draw;
         var poi_button:Poi;
         
         
         var parentScaleCanvas = new ScaleCanvas();
         var childMoveCanvas = new MediaCanvas(' ');
         var buttonLayer = new MovieClip ();
         var holdLayer = new MovieClip ();
        
        

         
        public function MapPoint(){
            
            hold_button = new Hold ();
            draw_button = new Draw ();
            poi_button = new Poi ();
            
                        
            hold_button.x = 520;
            hold_button.y = -350;
            draw_button.x = 520;
            draw_button.y = -300;
            poi_button.x = 520;
            poi_button.y = -250;
            
            parentScaleCanvas.x = 400;
            parentScaleCanvas.y = 300;
            
            
            this.addChild(parentScaleCanvas);
            parentScaleCanvas.addChild(childMoveCanvas);
            parentScaleCanvas.addChild(holdLayer);
            parentScaleCanvas.addChild(buttonLayer);
            buttonLayer.addChild(hold_button);
            buttonLayer.addChild(draw_button);
            buttonLayer.addChild(poi_button);
                        
            var flickrLoader = new Flickr(childMoveCanvas,1);
            
            hold_button.addEventListener(MouseEvent.CLICK, holdClicked);
            
            

            
            
            
            //TUIO.init( this, 'localhost', 3000, '', true );
            
        }
        
        
        
        private function holdClicked(someVar:String):void{
            
            buttonState.holdButtonSet();
            buttonState.readState();

            
        }
        
    }
    
}
The class that I called B:
Code:
package {
    
    import flash.display.MovieClip;
    
    
    public class MapPointer extends MovieClip{
        
        private var holdButtonState:Number = 1;
                
        public function MapPointer(){
            
                    
        }
        
        public function holdButtonSet ():void{
            
            if (holdButtonState == 0){
                holdButtonState = 1;
            }else{
                holdButtonState = 0;
            }
            trace("MapPointer : Hold button set: " + holdButtonState);
        }
        
        public function holdButtonGet ():Number{
            
            trace("MapPointer: holdButtonGet fonction: " +holdButtonState);
            return holdButtonState;
        }
        public function readState():void{
            trace("MapPointer: This button was set from the stage: " +holdButtonGet());
        }
            
        
    }
    
}
The class C is quite big so here is the beginning and the condition part:

Code:
age app.core.action{
    import flash.events.*;

    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.filters.DropShadowFilter;
    import flash.geom.Point;
    import flash.utils.getTimer;
    import MapPointer;
    
    
    public dynamic class RotatableScalable extends MovieClip
    {
        public var blobs:Array;        // blobs we are currently interacting with
        private var GRAD_PI:Number = 180.0 / 3.14159;
        public var state:String;
        private var curScale:Number;
        private var curAngle:Number;
        private var curPosition:Point = new Point(0,0);

        private var basePos1:Point = new Point(0,0);        
        private var basePos2:Point = new Point(0,0);                
        
        public var blob1:Object;
        public var blob2:Object;        
        public var bringToFront:Boolean = true;
        public var noScale = false;
        public var noRotate = false;        
        public var noMove = false;
        public var mouseSelection = false;
        
        public var dX:Number;
        public var dY:Number;        
        public var dAng:Number;
        public var dcoef:Number = 0.5;
        
        //DoubleTap variables
        public var xdist:Number;
        public var ydist:Number;
        public var distance:Number;
        private var oldX:Number = 0;
        private var oldY:Number = 0;        
        public var doubleclickDuration:Number = 500;
        public var clickRadius:Number = 50;        
        public var lastClick:Number = 0;
        public var buttonState:MapPointer;
            
        public function RotatableScalable()
        {
            state = "none";
            
            blobs = new Array();
            this.addEventListener(TouchEvent.MOUSE_MOVE, this.moveHandler, false, 0, true);            
            this.addEventListener(TouchEvent.MOUSE_DOWN, this.downEvent, false, 0, true);                        
            this.addEventListener(TouchEvent.MOUSE_UP, this.upEvent, false, 0, true);                                    
            this.addEventListener(TouchEvent.MOUSE_OVER, this.rollOverHandler, false, 0, true);                                    
            this.addEventListener(TouchEvent.MOUSE_OUT, this.rollOutHandler, false, 0, true);        
        
            this.addEventListener(MouseEvent.MOUSE_MOVE, this.mouseMoveHandler);                                    
            this.addEventListener(MouseEvent.MOUSE_DOWN, this.mouseDownEvent);                                                            
            this.addEventListener(MouseEvent.MOUSE_UP, this.mouseUpEvent);    
            //this.addEventListener(MouseEvent.MOUSE_OVER, this.mouseRollOverHandler);
            this.addEventListener(MouseEvent.MOUSE_OUT, this.mouseUpEvent);    
            
            this.addEventListener(MouseEvent.DOUBLE_CLICK, this.doubleTap);                                        
            
            this.addEventListener(Event.ENTER_FRAME, this.update, false, 0, true);        
            buttonState = new MapPointer();
        
            dX = 0;
            dY = 0;
            
            dAng = 0;
        }
The Condition:

Code:
public function mouseDownEvent(e:MouseEvent)
        {
                if(e.stageX == 0 && e.stageY == 0)
                return;
            if(buttonState.holdButtonGet() == 0){
                trace("RotatableScalable: This button state seen from RotatableScalabe, image can move: " +buttonState.holdButtonGet());
                noMove = false;
            }else{
                noMove = true;
                trace("RotatableScalable: This button state seen from RotatableScalabe, image can't move: " +buttonState.holdButtonGet());
    
            }
            if(!noMove)
                {    this.startDrag();    
                //this.addBlob(1, e.localX, e.localY);
                }
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Bookmarks

Thread Tools
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 On
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads

Thread Thread Starter Forum Replies Last Post
Tutorial 3 - the methods Flep Object Oriented Programming - tutorials 6 27-07-08 02:21
Object Oriented Programming - lezione 6 - Getter & Setter Flep Programmazione Orientata agli Oggetti - tutorials 1 14-06-08 18:36
Incapsulamento e metodi Getter & Setter Flep Articoli e tutorials 15 14-11-07 13:58
Tutorial 6 - Getter & Setter Flep Object Oriented Programming - tutorials 0 23-10-07 07:00
Tutorial 5 - attribute of the methods Flep Object Oriented Programming - tutorials 0 15-10-07 06:43


All times are GMT. The time now is 15:01.

Powered by vBulletin version 3.7.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC4
Forum SiteMap