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


Name:
Comment:
Confirm the image code:confirm image

 
Robert Alan writes:
27.11.2024 02:13
Do you want to lose weight and keep it off? Our clinically proven weight loss program guarantees results with 2 proven techniques, and personalized 1-on-1 coaching, support, and motivation. Get more info and start losing weight during a free 10-minute phone consultation with Robert Alan, a Certified Practitioner, Master Weight Loss Coach, and Emotional Eating Specialist with over 25 years of experience. His unique 1-on-1 weight loss program has been featured on National TV, Radio, Magazines, and Newspapers. Book a free 10-minute phone consultation to get more info and start losing weight. Click here on our calendar https://calendly.com/fbfb/click-here Here’s what others have said about the10-minute phone consultation: “I realized that his approach to losing weight is better than anything I've tried before." (Judy N. - March 2023). “During the call, Robert showed me a technique to help me change my relationship with food and naturally reduce my cravings, and I lost 28 pounds so far.” (Sue B. - Jan 2022). This is a completely different approach to losing weight because it's easier than dieting and exercise. Discover 2 new ways to lose weight. Book a free 10-minute phone consultation to get more info and start losing weight. Click here on our calendar https://calendly.com/fbfb/click-here Regards, Robert Alan NLP MC, Certified Practitioner, Master Weight Loss Coach Phone: toll-free 1-800-458-1469 Text: (304) 564-8406 RobertalanWL@gmail.com