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?