I'm almost entirely new to OOP, and I just don't understand how to call a method in one class from another. I'm trying to call a method in the Main.as from a custom class, but it always comes up as the "Call to possibly undefined method" error, and I don't know a way around it.
Main.as code:
package {
import flash.display.MovieClip;
public class Main extends MovieClip {
public var titleScreen:TitleScreen = new TitleScreen();
public var feedMeShibe:FeedMeShibe = new FeedMeShibe; //These are exported
public var milkbone:MilkboneItem = new MilkboneItem; //actionscript symbols
public var openMouthArea:OpenMouthArea = new OpenMouthArea; //in the library
public function Main() {
titleScreen.x = 350;
titleScreen.y = 250;
addChild(titleScreen);
}
public function addShibe(shibeX:Number, shibeY:Number, shibeSize:Number):void {
feedMeShibe.x = shibeX;
feedMeShibe.y = shibeY;
feedMeShibe.width = feedMeShibe.width*shibeSize;
feedMeShibe.height = feedMeShibe.height*shibeSize;
addChild(feedMeShibe);
}
public function addMilkbone(milkboneX:Number, milkboneY:Number, milkboneSize:Number):void {
milkbone.x = milkboneX;
milkbone.y = milkboneY;
milkbone.width = milkbone.width*milkboneSize;
milkbone.height = milkbone.height*milkboneSize;
addChild(milkbone);
}
public function addOpenMouthArea(OMAX:Number, OMAY:Number, OMAW:Number, OMAH:Number):void {
openMouthArea.x = OMAX;
openMouthArea.y = OMAY;
openMouthArea.width = OMAW;
openMouthArea.height = OMAH;
addChild(openMouthArea);
}
}
}
TitleScreen.as code:
package {
import flash.display.MovieClip;
import flash.events.Event;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
public class TitleScreen extends MovieClip {
public function TitleScreen() {
createListeners();
}
private function createListeners():void {
this.addEventListener(Event.ENTER_FRAME, stopFrame);
}
public function stopFrame(e:Event):void {
if (this.currentFrame == 510){
this.removeEventListener(Event.ENTER_FRAME, stopFrame);
this.stop();
addShibe(100, 100, 0.5); //How do I
addMilkbone(50, 50, 0.6); //access the Main class
addOpenMouthArea(200, 200, 1, 1); //with these?
}
}
}
}
I don't normally ask questions online, but I'm at the end of my rope here. I'm completely stumped, and browsing tutorials and previous questions online is only further confusing me. Any help would be greatly appreciated.
If I'm not mistaken all you have to do is
Main.addShibe(100, 100, 0.5); //How do I Main.addMilkbone(50, 50, 0.6); //access the Main class Main.addOpenMouthArea(200, 200, 1, 1); //with these?
The most simple way to do that is to have one class to reference another class. Let's say you have a switch and a bulb. The switch can tell the bulb to turn lights on and off.
Here's a minimalistic example of how it could look like:
now you have a bulb referenced in your switch class. Inside from it you can call public methods and access public variables of the Bulb class. Don't forget to make a public variable bulbReference in your LightSwitch class.
There are many ways to form dependencies between elements of your code to break strong dependencies and make your code: scalable, reusable and maintainable. To learn about it look for Design Patterns. There is a great book written about this topic: http://shop.oreilly.com/product/9780596528461.do
But there's a lot of material on the topic free on the Internet.