This website uses cookies to improve user experience. By using our website you consent to all cookies in accordance with our Cookie Policy. X

Creating a subclass with predefined graphics [basic]

Some time ago I have described how to use Interfaces to create a system of popups - here to be exact. I have mentioned then that similar system could be crafted with the use of subclasses, but in case of Flash this might bring unexpected hurdles to overcome. To understand what those hurdles are exactly, it is best to start from the very end by explaining how to deal with them - there are two solutions:

1. One base class to control all subclasses.
Lets say we have two popups, one with a text and other with an image, but also two shared elements: background and "OK" button. In this case it would be best to create three classes: MyPopup which will serve as a base class containing the logic for the background (named e.g. "mcBackground") and for the button ("mcButton"), followed by MyPopupImage responsible for image popup and MyPopupText responsible for the text popup. Unfortunately Flash will refuse to compile this setup, stating that there is a conflict with background in MyPopup and backgrounds in both MyPopupImage and MyPopupText. Of course you could just use different names, but this kind of defeats the purpose - the correct answer is to … delete the subclasses (their files to be exact), allowing Flash to create them on export.

Quite frankly I have no idea how it works, but one thing is for sure, we now can create as many popups as we want using the variables from the base class for background, button and whatever else we want. The one downfall of the solution is of course the need to create all the logic (for every popup) in one place.

2. Using the GET functions.
Here the situation is exactly of opposite of the one described in the previous point, we will be creating separate classes for each popup even it will require zero lines of actual execution code.
Back to the example of 3 classes: MyPopup, MyPopupImage and MyPopupText - we need to build them in such way that no variable conflicts with ones defined in the base class:

//MyPopup class
public class Popup extends MovieClip {
	public function Popup() {
	}
}
//MyPopupImage
public class MyPopupImage extends MyPopup {
	public var mcBackground:MovieClip;
	public var mcButton:MovieClip;
		
	public function MyPopupImage() {	
	}
}
//MyPopupText
public class MyPopupText extends MyPopup {
	public var mcBackground:MovieClip;
	public var mcButton:MovieClip;
		
	public function MyPopupText() {	
	}
}
Okay, but doesn't it make all pointless? From looks of it now the base class doesn't have the access to both "mcBackground" and "mcButton", meaning we wont be able to write single, shared logic for them in MyPopup. This is where the GET functions come into action! Lets added them to MyPopup in following way:
public class MyPopup extends MovieClip {
	public function MyPopup() {
	}
	protected function get popupBackground():MovieClip {
		return null;
	}
	protected function get popupButton():MovieClip {
		return null;
	}
}
And then rebuild MyPopupImage and MyPopupText as so:
//MyPopupImage
public class MyPopupImage extends MyPopup {
	public var mcBackground:MovieClip;
	public var mcButton:MyButton;
	
	public function MyPopupImage() {	
	}
	
	override protected function get popupBackground():MovieClip {
		return mcBackground;
	}
	override protected function get popupButton():MovieClip {
		return mcButton;
	}
}
//MyPopupText
public class MyPopupText extends MyPopup {
	public var mcBackground:MovieClip;
	public var mcButton:MyButton;
	
	public function MyPopupText() {	
	}
	
	override protected function get popupBackground():MovieClip {
		return mcBackground;
	}
	override protected function get popupButton():MovieClip {
		return mcButton;
	}
}
Finished, now base class MyPopup can access backgrounds and buttons from subclasses:
public function MyPopup() {
	super();
	trace(popupBackground!=null); //true
}

Check it out for yourself: subclass.zip