Event dispatcher - actionscript-3

I have two classes and i tried event dispatch from one class to other class. Here i have used button to invoke the class. But dispatch not working.
class name cusDispatcher.as
package
{
import flash.display.MovieClip;
import flash.events.EventDispatcher;
import flash.events.Event;
import flash.events.MouseEvent;
import Globe;
public class CusDispatcher extends EventDispatcher
{
public function CusDispatcher():void
{
Globe.self.realstage.btn_mc.addEventListener(MouseEvent.CLICK,
doAction);
}
public function doAction(event:MouseEvent):void
{
dispatchEvent(new Event('myevent'));
}
}
}
class name EventDispatcherExample.as
package
{
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.EventDispatcher;
import Globe;
public class EventDispatcherExample extends EventDispatcher
{
public function EventDispatcherExample(Mc:MovieClip)
{
Globe.self.realstage = Mc;
var start:CusDispatcher = new CusDispatcher();
Globe.self.realstage.addEventListener('myevent', handler);
trace("one");
}
public function handler(event:Event):void
{
trace("My Event");
}
}
}
In the adobe CC i used this instance to run the class
var Call:EventDispatcherExample = new EventDispatcherExample(this);
Whenever i click the button(btn_mc) in the adobe flash cc it does not show any
trace of dispatch event.So could please analyze and give solution for this one.

Is your other click listener (in the EventDispatcherExample) working?
The problem might be that you have declared your CusDispatcher as a local variable within a function
var start:CusDispatcher = new CusDispatcher();
Once the function is complete these variables are cleaned up. Try to declare it at the beginning of your class as a class variable:
private var start:CusDispatcher;
public function EventDispatcherExample(Mc:MovieClip)...
and then assign your new Dispatcher to it
start = new CusDispatcher();

Related

Flash Programming ActionScript3, double import swf to the main Stage from another class

Let me explain my situation:
We have a file named Main.fla which links to the class MAIN( it's included in the MAIN.as file). I also have a secondary User.as file with a User class.
I managed to import a classic swf button to my stage from the User.as class but i'm finding trouble on adding the pop up window, when the button is clicked. Here are the codes:
MAIN.as
import flash.display.*;
import flash.net.*;
import flash.events.*;
import User; //Importing our User class
public class MAIN extends MovieClip
{
public function MAIN()
{
var k = new User();
k.logocons(this); //This function is made on User class and
//it takes a stage:Object as definition
}
}
User.as
import flash.display.*;
import flash.net.*;
import flash.events.*;
public class User extends MovieClip
{
var myLoader:Loader = new Loader();
public function User()
{
var url:URLRequest = new URLRequest("C:/Project/Button.swf");
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, swfLoaded);
myLoader.load(url);
}
function swfLoaded(event:Event):void
{
myLoader.x = 50;
myLoader.y = 50;
}
public function logocons(stage:Object)
{
stage.stop();
stage.addChild(myLoader);
}
}
This works normally so far When i test the file the Button works perfectly
What i want now is when the button is clicked to show at my MAIN.Stage a pop up window which is also in the same folder named PopUp.swf.
I tried really many things but i couldn't find how i can access the MAIN.stage from another class.
Thanks in advance
User.as
import flash.display.*;
import flash.net.*;
import flash.events.*;
import fl.motion.MotionEvent;
public class User extends MovieClip
{
var myLoader:Loader = new Loader();
private var _mainStage:Stage;//MAIN.stage
public function User()
{
var url:URLRequest = new URLRequest("C:/Project/Button.swf");
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, swfLoaded);
myLoader.load(url);
}
function swfLoaded(event:Event):void
{
myLoader.x = 50;
myLoader.y = 50;
}
public function logocons(stage:Object)
{
_mainSage = (stage as MovieClip).stage;// now you can use _mainStage anywhere on User class.
stage.stop();
stage.addChild(myLoader);
}
private function onButtonClick(e:MouseEvent){
//ex. _mainSage.addChild(popWindows);
}
}

Actionscript 3 - Class Referring Errors

I a document class (Main) and a class connected to a symbol (MainMenu), and I get an error I just can't figure how to solve.. I get this error:
1136: Incorrect number of arguments. Expected 1.
it is reffering to "public var mainMenu = new MainMenu();" in my document class.
Anyways, here are my classes:
Main(document class):
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Stage;
public class Main extends MovieClip {
public var mainMenu = new MainMenu();
public function Main() {
// constructor code
startGame();
}
public function startGame(){
addChild(mainMenu);
}
public function initGame(event){
//Adding player and such..
}
}
}
MainMenu:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
public class MainMenu extends MovieClip {
private var logo = new Logo();
public function MainMenu(main:Main) {
// constructor code
mainMenu = new MainMenu(this);
logo.addEventListener(MouseEvent.CLICK, main.initGame);
placeButtons(event);
}
public function placeButtons(event:Event){
logo.addEventListener(MouseEvent.CLICK, initGame);
logo.x = - logo.width/2;
logo.y = 50;
addChild(logo);
trace ("MainMenu added");
}
}
}
Thanks
A few pointers...
Be mindful of your indentation.
Datatype your variables, arguments, and function returns.
Your MainMenu class was self instantiating itself (that's an infinite loop)
If you really need direct access to another classes function, consider making the child class extend the parent class. Alternatively, you could make the specific function a static function and call the function directly off the class without passing variable references. For example: Main.initGame()
Here are your classes, with a potential solution...
Main:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Stage;
public class Main extends MovieClip {
public var mainMenu:MainMenu;
public function Main() {
mainMenu = new MainMenu();
addChild(mainMenu);
}
public function initGame(e:Event):void {
//Adding player and such..
}
}
}
MainMenu:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
public class MainMenu extends MovieClip {
private var logo:Logo;
public function MainMenu() {
// Wait for it to be added to the parent.
logo = new Logo();
addChild(logo);
addEventListener(Event.ADDED_TO_STAGE, placeButton);
}
private function placeButton(e:Event):void {
// We only need this to happen once, so remove the listener
removeEventListener(Event.ADDED_TO_STAGE, placeButton);
// Now that we've formed the connection, we can reference the function dynamically
logo.addEventListener(MouseEvent.CLICK, this["parent"].initGame);
logo.x = - logo.width/2;
logo.y = 50;
}
}
}
I've left the structure as similar to your original intent as possible, but the above function reference is poor form. As a general rule, children should not have connections to their parents as it's a potential memory leak. You might instead add the event listener from your Main class.

Action Script 3 - Full screen from class

please note that I am a novice when it comes to Actionscript 3 and lot of what I could do with reasonable competency in AS2 I now can't in AS3, to my frustration! OK, I'm fleshing out a simple drag drop and decorate application in Flash. I'm wanting to use the external action script class/package to allow for it full screen from my desktop and I'm in a tangle, with constructor errors being thrown up and all sorts. Could anyone give any pointers?
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.display.Stage;
import flash.display.StageDisplayState;
public class fullmode extends MovieClip {
public function fullmode() {
fullbtn.addEventListener(MouseEvent.CLICK, fullScreen);
}// btn declared - - - - - - - -
//public function fullmode(event:MouseEvent):void {
stage.displayState=StageDisplayState.FULL_SCREEN;
}
}
//--------------------- drag item
public class DragDrop extends MovieClip {
public function DragDrop() {
dragme.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
dragme.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
}
private function mouseDownHandler(evt:MouseEvent):void {
var obj = evt.target;
obj.startDrag();
}
private function mouseUpHandler(evt:MouseEvent):void {
var obj = evt.target;
obj.stopDrag();
}
}
}
Thanks world!
You had a few syntax errors/typos, I've fixed them below:
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.display.Stage;
import flash.display.StageDisplayState;
public class Fullmode extends MovieClip
{
public function Fullmode()
{
fullbtn.addEventListener(MouseEvent.CLICK, fullScreen);
}
private function fullScreen(event:MouseEvent):void
{
stage.displayState = StageDisplayState.FULL_SCREEN;
}
}
}
Standard practice dictates that your class names should be capitalized, hence Fullmode instead of fullmode.
Additionally, you had named your MouseEvent.CLICK listener the same as your class, instead of what you intended to name it.

How to call an MovieClip on stage AS3

Trying to addChild() inside a movie clip in the stage from one of my classes. The code looks like this
package {
import flash.display.SimpleButton;
import flash.events.MouseEvent;
import flash.display.MovieClip;
public class createFlask extends SimpleButton {
public function createFlask() {
addEventListener(MouseEvent.CLICK, createAFlask);
}
private function createAFlask(e:MouseEvent):void
{
var Flask = new flask ;
Flask.x = stage.width/2;
Flask.y = stage.height/2;
stage.experiment.addChild(Flask);
}
}
This gives an error as
Access of possibly undefined property experiment through a reference
with static type flash.display:Stage.
Any solutions?
Just omit "stage".
Instead use
experiment.addChild(Flask);
That will work.

Add other class as child in Flash Professional

Using Flash Professional CS5, I'm trying to add a child object in my script. I want to give the class which creates the child-object as parameter while creating. The problem is when I try to test the project, I get an error stating Incorrect number of arguments. 0 expected.
My MainClass.as:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class MainClass extends MovieClip {
var menuClass:MenuClass;
var gameClass:GameClass;
var highClass:HighscoreClass;
public function Main() {
this.StartOfProject();
}
public function StartOfProject() {
menuClass = new MenuClass(this);
this.addChild(menuClass);
highClass = new HighscoreClass();
}
And my MenuClass.as:
package {
public class MenuClass extends MovieClip {
var mainClass:MainClass;
public function Menu(mainClass:MainClass) {
this.mainClass = mainClass;
...
}
What am I doing wrong?
You named the constructor of your MenuClass incorrectly. It should be "MenuClass" not "Menu"
change:
public function Main() {
this.StartOfProject();
}
to:
public function MainClass() {
this.StartOfProject();
}
and:
public function Menu(mainClass:MainClass)
to: public function MenuClass (mainClass:MainClass)
and see if this already solves your problem