How can I access a function in one class from another AS3? - actionscript-3

I have a AS3 MP3 player. The player class is called Mp3PlayerFrontEnd and controls the play and pause buttons. The playlist class is called PlaylistBoxItem and loads the track names into a playlist from XML. On each line of the playlist is a download button. When the download button is pressed, I want the player to pause. I thought I could just _player.pause(); from the playlist class but it doesnt work.
So my question is... How do I access the function in the MP3 player class from the playlist class?
I did not make this, I purchased it. I asked the guy who made it for help and he responded back...
"Normally you will just have to get access to the Mp3Player instance in PlaylistListboxItem class and call pause() on it in download function. I guess the easiest way to achieve this is to create a public static instance of a holder class that could be used access the player from wherever in code."
I do not know how to make a public static instance or where to put it.
Would I need to make a new class?
Where would I import it from?
How would I write the function?
Thanks.
Rich

whatever class is in control of everything needs a reference to your player.
So if your play list class is where you want to use play/pause/etc in the constructor add a place for a reference to the mp3player.
public class PlayListBoxItem
{
private var myMp3Player:Mp3Player;
// the class constructor
public function PlayListboxItem(myMp3Player:Mp3Player)
{
this.myMp3Player = myMp3Player;
}
}
now anywhere in the play list class you can access your mp3 player with myMp3Player.start() .stop(), .whatever public functions are there.
Make sure start and stop are public functions.

Related

Action script adding a background

First of all I've just a couple hours experience with Flash and AS3 therefore, I'm sorry if it is a bit easy question.
What I want do to is a simple space war games to learn basics of AS3 and flash. Altough I dont know much things about layers, I think my game should contain two layers, one for background and the second one is for enemies spaceships and our spaceship.
I added a jpeg format file to library to use it as a background. (as you can see from the link :http://prntscr.com/2pe6zb and http://prntscr.com/2pe733 )
And I create a as3 documentFile which is called Arkaplan.as and content of it is:
package{
import flash .display.*;
public class Arkaplan extends MovieClip{
public function Arkaplan(){
var hero:backGround =new backGround();
addChild(hero);
}
}
}
However, I got an error which says that : "1180: Call to a possibly undefined method BackGround."
Is there anyone to help me to solve what is wrong ?
EDIT
When I changed the code above lilke :
package{
import flash .display.*;
public class Arkaplan extends MovieClip{
public function Arkaplan(){
var myBitmap:Bitmap = new Bitmap(new backGround(500, 500));
var myMovieclip:MovieClip=new MovieClip();
myMovieclip.addChild(myBitmap);
addChild(myMovieclip);
trace("deneme 1-2");
}
}
}
Problem is solved but I dont know why it runs correctly know ? To be able to use Bitmap Do I have to add it as a child to movieClip ?
Any time you use the word 'new' you are creating a new object. The word after new will be the name of a Class that Flash will try to locate. You must either have an external .as file that matches that name or have a library item set to 'export for ActionScript' with a Class name that matches that name.
You can access a library item's properties by right-clicking on it in the library and clicking 'properties'. With the 'advanced' option open, check the 'export for ActionScript' box and enter a Class name that matches the one you want to create.

actionscript 3.0 referencing a custom class of a main swf from a loaded swf's custom class

I am writing educational software that utilizes a single "application" swf, which loads external "topic" swf files (containing library items specific to the topic). Each of these external "topic" swf files also utilizes custom classes that solve calculations specific to the topic (ex. trigonometry class for the trigonometry.swf topic file).
I want the trigonometry class of the trigonometry.swf to be able to reference a custom class method that exists in the main "application" swf. If it helps to know the context, I want the user working on trigonometry to be able to type in the trig function the believe to be correct (sin, cos, or tan) and "check" to see if they are right. The "indicate_right_wrong" method exists in a "grader" custom class of the main "application" swf (so that any changes I make to this class will be updated throughout all topics). So...how do I reference a custom class method of a main swf FROM an externally loaded swf file's own custom class?
I DO have the result working when I import the "grader" custom class into each "topic" swf and reference it directly, but I don't want to have to re-publish each topic swf if I make a change to the "grader" custom class. Thus, I'm trying to have each topic capable of referencing the "grader" custom class when it exists within the main "application" swf.
The error I get when I try using variations of parent or parent.parent are property grader_class not found on flash.display.Stage (for parent) and Cannot access a property or method of a null object reference. (when using parent.parent).
Thank you for any help you can provide.
You can't compile code that calls methods not found in the code being compiled. However, you can make runtime calls by evaluating a method. For example:
stage["myfunction"](args);
However, while you could create some sort of pathing logic to reference your methods, as George Profenza indicated, your success will be much higher (and easier to work with) if you simply pass a function listener to a dispatched event or method call.
For example, let's assume you have a custom CHECK_ANSWER event that fires when the user enters an answer. Your gradeAnswer() function (that exists in your "application.swf") would need to register for that event after the child "trigonometry.swf" was loaded. Subsequently, and the data would be passed through.
This, however, may not be ideal as it can cause the child SWF to not unload from memory properly due to a parent registration to a child object (all registrations must be removed first).
Personally, I'd recommend creating a static class which serves as a directory for your application. Each swf loads this class, and lists their pertinent methods as needed. For example:
package com.atriace {
public class Directory {
public static var methods:Object = {
}
}
}
When the application.swf loads, it populates the methods object with your grading function. Subsequent child swfs also load this static class, and expect to find your grade method pre-populated.
Application.swf
import com.atriace.Directory;
Directory.methods["grade"] = gradeAnswer;
Child.swf
import com.atriace.Directory;
Directory.methods["grade"](answers);
Don't know if the quiet is because this kind of question has been answered many times before on Stack Overflow. Anyways here is my take on it..
I want the user working on trigonometry to be able to type in the trig
function the believe to be correct (sin, cos, or tan) and "check" to
see if they are right.
I'm assuming since they have to type the answer you have an input textbox? Also when they have answered they press a button to "submit" their answer? Assuming I'm on the right lines we can do that this way...
note: answer_txt (string) = answer given in external class by user, input_txt = textfield for user input
In the main class have something like
import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.*;
import ExternalClass; //your class file attached to relevant topic swf
public class Main extends MovieClip
{
private var _extClass = new ExternalClass(); //create reference to external Class
public function Main()
{
//Ref 1 means do that function when trigger event received from external class
stage.addChild(_extClass); //gives access to "stage" for ExternalClass.as
_extClass.addEventListener("Check_Answer", indicate_right_wrong); //see Ref 1
}
private function indicate_right_wrong (e:Event)
{
//your answer checking code.
//Access answer_txt variable from external class like example below
//_extClass.answer_txt.text = " ";
}
Now in the external class (Trigonometry.as??) you can set up something like this. Make sure answer_txt exists (public var answer_txt:String;)..
public function Question_1 () : void
{
//your code for question + set submit button
submit_BTN.addEventListener(MouseEvent.CLICK, send_Answer);
}
function send_Answer(e:MouseEvent):void
{
answer_txt = input_txt.text; //set input text as final answer
trace("final answer is..." + answer_txt); //just to test it's there
dispatchEvent (new Event ("Check_Answer")); //Main class is waiting for this event (ref 1)
trace ("event dispatched.." + dispatchEvent); //to confirm it was despatched
}

How to extend class that is inside a loaded swf

I've got an swf loaded externally to my app with loader component. It is loaded to the Application Domain of my loader. I've got a class in that swf wich I would like to extend and override some functions from it. Is it possible somehow?
Here is some code to explain what I want(of course, it is fully incorrect and wold not work):
public var ClassFromLoadedSwf:Class = ApplicationDomain.currentDomain.getDefinition("path.to.needed.class") as Class;
public class MyClass extends ClassFromLoadedSwf
{
override protected function initMovie() : void
{
//function logic goes here
}
}
Thank you for your answers and sorry for my bad english.
No you can't. Basically you don't understand the meaning of ApplicationDomain. Using a loader and Applicaiton Domain you just merge some code to your SWF in Runtime. Before it reaches the Runtime state, it can't be accessed. So at the Compile time you can't do what you're trying to do now. But you can try to use SWC instead of SWF. You just include it in your Project Library and that's all. You'll be able to access all the classes at compile time. Try reading this Article. It will help you understand the meaning of SWCs.

how to access class from embedded swf

I am trying to get an instance of a specific class from an embedded swf, it so happens the class I need is also the Default Application extending Sprite for the swf. I am able to do this successfully if I load the swf however I want to embed it instead.
The class I want to load is also extending a custom interface. Here is what I have tried but is not working:
[Embed(source="resources/MySwf.swf")]
private var MySwf:Class;
private function someFunction() : void
{
var inst:ISomeInterface = new MySwf() as ISomeInterface;
}
I appreciate any pointers.
Thanks.
Docs for embedding are here:
http://livedocs.adobe.com/flex/3/html/help.html?content=embed_4.html
You should be able to do something like:
[Embed(source='resources/MySwf.swf', symbol='TheExportNameInMyFlaLibrary')]
public var MySwf:Class;
Personally, I prefer to use the Flash IDE publish settings to be have Export as SWC checked. That way, you can just drop the SWC into your FlashBuilder projects' lib folder and be done. Don't have to worry about manually setting each class up like this.

Recasting member of a library class to new type in extended class

I created an flv video player using Flash Builder 4. This "BasicVideoPlayer" project is compiled into a SWC that will be eventually be used to create other video players that extend the functionality. One of the features is a view that appears when the video has finished playing that displays a "Play Again" button. This "Play Again" view has its own class, "BasicPlayAgain", that accepts a graphic asset that is exported from a .fla file that contains all of the graphic/UI assets.
In my new project, "EnhancedVideoPlayer", I'm using the BasicVideoPlayer SWC as a library to create a new video player that will add more functionality to the "Play Again" view; specifically it will add more buttons to that view.
The EnhancedVideoPlayer uses a default class that extends the BasicVideoPlayer class. The BasicVideoPlayer class has a member called "playAgainScreen" whose type is BasicPlayAgain. The EnhancedVideoPlayer needs to override the playAgain member and recast it as EnhancedPlayAgain so it can control the new buttons properly.
How do I go about overriding the playAgain member as a new type? Or am I approaching this from the wrong direction?
One possible solution is if the EnhancedPlayAgain object extends BasicPlayAgain, you could still store it in the playAgainScreen variable, and then cast back to EnhancedPlayAgain as needed.
EnhancedPlayAgain(playAgainScreen).someAdditionalMethod();