A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Flex > New Actionscript Project - Structure

  1. #1
    Member
    Join Date
    May 2007
    Posts
    46

    Flex > New Actionscript Project - Structure

    Hi all i am havijng some difficulty with a certain topic.

    Say you have a class that centres a sprite. Thats is job. When a resize event occurs, the class repositions itself.

    //In this line i added an stage.addEventListener(resize) event listener INSIDE the CustomClass class so it behaves as expected but is hard coded

    var centre:CustomClass = new CustomClass();

    However is this wrong?

    //You can also do this:

    var centre:CustomClass = new CustomClass();
    stage.addEventListener(Event.Resize, onCentreResize);

    function onCenterResize(e:Event): void
    {
    centre.x = stage.stageWidth/2 - centre.width/2;
    }



    //and this
    var centre:CustomClass = new CustomClass();
    stage.addEventListener(Event.Resize, centre.onCentreResize);



    Whos job is it to provide the functionality of the centreSprite?
    Last edited by bellend; 04-12-2008 at 09:04 AM.

  2. #2
    trace("AKA: Biro Barna");
    Join Date
    Oct 2007
    Location
    RO.Timişoara
    Posts
    1,403
    You need to call the method from withing your class that does the resizing. Don't forget that the method must be public in order to access / call it. Something like: center.resizeNow(); or you could call the constructor of you class with super();



    | Windows MSN: birobarna [at] hotmail [dot] com | Skype: barna.biro |
    WebLog: http://blog.wisebisoft.com/ |
    | Software Developer / Flash & Flex Developer | Student ( Computer Science ) | Interested in: Sharing Knowledge |
    |
    Romanian Adobe Flash, Flex, AIR Forum: http://www.flashforum.ro/
    | By perseverance the snail reached the ark. |


  3. #3
    Member
    Join Date
    May 2007
    Posts
    46
    Thanks for the quick reply man.

    I hope you have some patience as i still dont understand. Ill try to be thorough.

    The following code is the main application class, the CentreSprite class is the only other class. So its basic. The whole purpose of the application is to centre the CentreSprite class on screen when a browser resize occurs. (it is purely the theory i am after).

    Here is the code.

    MAIN APPLICATION CLASS


    Code:
    package {
    	import flash.display.*;
    
    	public class Example extends Sprite
    	{
    		public function Example()
    		{
    			stage.scaleMode = StageScaleMode.NO_SCALE;
    			stage.align = StageAlign.TOP_LEFT;
    			
    			var cs:CenterSprite = new CenterSprite();
    			addChild(cs);
    		}
    	}
    }
    CENTERSPRITE CLASS

    Code:
    package
    {
    	import flash.display.Sprite;
    
    	public class CenterSprite extends Sprite
    	{
    		public function CenterSprite()
    		{
    			super();
    			this.graphics.beginFill(0x000000);
    			this.graphics.drawRect(0, 0, 100, 100);
    			this.graphics.endFill();
    		}
    		
    	}
    }

    Running this code will give you a simple square on the top left of the screen.

    When it comes to our positioning, i have 2 options. I can get the stage to take care of it without changing the CentreSprite at all.


    MAIN APPLICATION CLASS


    Code:
    package {
    	import flash.display.*;
    	import flash.events.Event;
    
    	public class Example extends Sprite
    	{
    		private var cs:CenterSprite;
    		
    		public function Example()
    		{
    			stage.scaleMode = StageScaleMode.NO_SCALE;
    			stage.align = StageAlign.TOP_LEFT;
    			
    			cs = new CenterSprite();
    			addChild(cs);
    			
    			stage.addEventListener(Event.RESIZE, onResize);
    		}
    		
    		private function onResize(e:Event): void
    		{
    			cs.x = stage.stageWidth/2 - cs.width/2;
    			cs.y = stage.stageHeight/2 - cs.height/2;
    		}
    	}
    }
    Or vice versa, i could leave the main application class alone and instead modify the CentreSprite class.

    CENTRESPRITE CLASS

    Code:
    package
    {
    	import flash.display.Sprite;
    	import flash.events.*;
    
    	public class CenterSprite extends Sprite
    	{
    		public function CenterSprite()
    		{
    			super();			
    			addEventListener(Event.ADDED_TO_STAGE, init);
    		}
    		
    		private function init (e:Event): void
    		{
    			this.graphics.beginFill(0x000000);
    			this.graphics.drawRect(0, 0, 100, 100);
    			this.graphics.endFill();
    			
    			stage.addEventListener(Event.RESIZE, onResize);
    		}
    		
    		private function onResize(e:Event): void
    		{
    			this.x = stage.stageWidth/2 - this.width/2;
    			this.y = stage.stageHeight/2 - this.height/2;
    		}
    		
    	}
    }
    Both are the same effect. However, with re-usable class objects, where does one put the code? .. This is as basic an example as i can think of. I know there is another way as well. I could get the main application class to call a public function cs.onResize() as well and it would work.

    I do not know what is the correct way to look at this and its a major hurdle in my understanding of the subject.

    Any Takers?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center