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

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);
}
}

Related

Event dispatcher

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();

How do I create an object using code in AS3 and give it a picture as display?

package
{
import flash.events.*;
public class declareImage extends Sprite
{
var ship:Sprite = new Sprite();
public function declareImage()
{
}
}
}
I declared an object.
Now I want to give it a background as a picture from my Compute
Should I use Sprite as data type or something else?
Here is an example. Of course, it's a very simple case (using fixed file name, etc), just to illustrate how to achieve what do you want and provide you the foundation to move on.
A tip, begin a class name with an uppercase letter.
package
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.net.URLRequest;
public class DeclareImage extends Sprite
{
private const IMAGE_URL:String = 'myImage.jpg';
private var ship:Sprite;
private var loader:Loader;
public function DeclareImage()
{
ship = new Sprite();
addChild(ship);
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgressHandler, false, 0, true);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadCompleteHandler, false, 0, true);
loader.load(new URLRequest(IMAGE_URL));
}
private function loadProgressHandler(event : ProgressEvent) : void
{
trace('Loading: ' + Math.round((event.bytesLoaded/event.bytesTotal) * 100) + '%');
}
private function loadCompleteHandler(event:Event):void
{
loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, loadProgressHandler);
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadCompleteHandler);
ship.addChild(loader);
trace('complete');
}
}
}

AS3 I don't understand the different treatment of an extended movieclip class vs extended simplebutton class

I recently discovered the custom classes in actionscript 3. I started using them in my latest project but I find it hard to bend my brain around how it all works.
I created two different classes to test.
One is an extended movieclip called "Persoon" and the other is an extended simplebutton called "SpeakerBtn".
package {
import flash.display.Sprite;
public class Persoon extends Sprite {
public function Persoon(xPos:Number, yPos:Number, naam:String) {
var persoon:Sprite = new Sprite;
persoon.graphics.beginFill(0x000000,1);
persoon.graphics.drawCircle(xPos, yPos, 2);
persoon.graphics.endFill();
this.addChild(persoon);
trace ("hij heet " + naam);
}
}
}
package {
import flash.display.SimpleButton;
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
public class SpeakerBtn extends SimpleButton {
public var snd:Sound;
public var cnl:SoundChannel = new SoundChannel();
public function SpeakerBtn(xp:Number,yp:Number,naam:String) {
var speaker:SimpleButton = new SimpleButton();
speaker.name = naam;
speaker.x = xp;
speaker.y = yp;
speaker.addEventListener(MouseEvent.CLICK, playSnd);
//this.addChild(speaker);
}
public function playSnd (event:MouseEvent) : void {
trace ("ping");
}
}
}
Then I have my main:
package {
import flash.display.MovieClip;
import SpeakerBtn;
import flash.display.SimpleButton;
import Persoon;
public class Main extends MovieClip {
var sp:SpeakerBtn;
var ps:Persoon;
public function Main() {
sp = new SpeakerBtn(50,50,"donna");
addChild(sp);
ps = new Persoon(300,300,"wilf");
addChild(ps);
}
}
}
Persoon wilf works like I expected, displays fine and traces correctly.
SpeakerBtn donna does not display and does not trace correctly. I commented out the addChild in the SpeakerBtn package because if I turn it on, I get the error 1061: Call to a possibly undefined method addChild through a reference with static type SpeakerBtn
I noticed that when I define the x and the y and addChild in Main for the speakerBtn it does work. But I don't want to have to define all that in Main, I want my SpeakerBtn to do all that.
I checked this question but it does not provide me with an answer. Can someone explain to me what is happening, or alternatively link me to a comprehensible tutorial (one not too heavy on techspeak, more like an explain-it-to-me-like-I'm-5-years-old)? Thanks!
Update
I forgot to add a button with the class SpeakerBtn to my library, so there was nothing to display. Fixed that now, and with this code the button does appear on the stage, only the x and y values are not registered and it appears on 0,0. Also, the event playSnd does not trigger the trace and I assume is not working.
Solution
With help of Cherniv's information I came to the following solution for my SpeakerBtn.
Main does this:
package {
import flash.display.MovieClip;
import SpeakerBtn;
import flash.display.SimpleButton;
public class Main extends MovieClip {
var sp:SpeakerBtn;
public function Main() {
sp = new SpeakerBtn("donna", 300, 50);
addChild(sp);
}
}
}
And SpeakerBtn does this:
package {
import flash.display.SimpleButton;
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;
public class SpeakerBtn extends SimpleButton {
private var snd:Sound;
private var cnl:SoundChannel = new SoundChannel();
private var _naam:String;
private var _x:Number;
private var _y:Number;
public function SpeakerBtn(naam:String, xp:Number, yp:Number) {
_naam = naam;
_x = xp;
_y = yp;
addEventListener(Event.ADDED_TO_STAGE, addBtn);
}
private function addBtn (event:Event) : void {
this.x = _x;
this.y = _y;
this.name = _naam;
snd = new Sound(new URLRequest("mp3/" + _naam + ".mp3"));
addEventListener(MouseEvent.CLICK, playSnd);
}
private function playSnd (event:MouseEvent) : void {
cnl = snd.play();
}
}
}
So what I did was add an EventListener for when the button was added to the stage and then set all the variables like x-position, y-position and name.
That's because of inheritance. Your SpeakerBtn doesn't inherits the addChild method from his ancestors , because as we can see in SimpleButton's documentation it is inheritor of DisplayObject and not of DisplayObjectContainer , which do have a addChild method and passes it to all his inheritors including MovieClip and Persoon.

Event listener on button not working

So, my menu for my game is in a separate .fla file and I have used a loader like so to load the menu into my game:
package {
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.display.Stage;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.ui.Mouse;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.text.TextFormat;
import flash.text.TextField;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.system.LoaderContext;
import flash.display.Sprite;
import flash.net.Socket;
import caurina.transitions.Tweener;
public class Main extends MovieClip {
public static var gameLayer:Sprite = new Sprite;
public static var endGameLayer:Sprite = new Sprite;
public static var menuLayer:Sprite = new Sprite;
public var gameTime:int;
public var levelDuration:int;
public function Main()
{
addChild(gameLayer);
addChild(endGameLayer);
addChild(menuLayer);
var myMenu:Loader = new Loader();
var url:URLRequest = new URLRequest("Menu.swf");
myMenu.load(url);
myMenu.contentLoaderInfo.addEventListener(Event.COMPLETE, menuLoaded);
function menuLoaded(event:Event):void
{
menuLayer.addChild(myMenu.content);
}
playBtn.addEventListener(MouseEvent.CLICK, startGame);
}
public function startGame(e:Event)
{
// Code to remove the menu (menuLayer.removeChild?)
// Code here to start timers etc
}
I set instance names for my buttons but when I try to do something like menuLayer.playBtn.addEventListener(MouseEvent.CLICK, startGame);, I get messages saying Access of undefined property playBtn.
Now, I double checked on my Menu.fla and I definitely gave the button an instance name of playBtn but it's not working. Any help please? Might be something really obvious I've missed but I'm not sure what.
EDIT: Trying it another way (Converting the menu to a movieclip) but not 100% sure how to do it exactly. The code I have is:
public class Main extends MovieClip {
var mainMenu:menuMain = new menuMain;
// Other variables
public function Main()
{
addChild(gameLayer);
addChild(endGameLayer);
addChild(menuLayer);
menuLayer.addChild(mainMenu);
mainMenu.addEventListener(Event.COMPLETE, menuLoaded);
}
function menuLoaded(event:Event):void
{
//var mainMenu:LoaderInfo = event.currentTarget as LoaderInfo;
//var menuInstance:MovieClip = menuLayer.getChildAt(0) as MovieClip;
// now you can actually add the listener, because the content is actually loaded
mainMenu.playBtn.addEventListener(MouseEvent.CLICK, startGame);
}
public function startGame(e:Event)
{
// Code to execute timers etc.
}
My guess is that you think that when you go menuLayer.addChild(myMenu.content) that menuLayer suddenly becomes an instance of menu.swf That is not the case. It becomes a child of menuLayer.
Try this :
menuLayer.addChild(myMenu.content);
var menuInstance:MovieClip = menuLayer.getChildAt(0) as MovieClip;
trace (menuInstance.playBtn);
This code assumes that you have nothing else added to menuLayer and in that case your menu.swf content would be the only child on the display list of menuLayer.
I am also assuming that menu.swf's contents are a MovieClip.
If my assumptions are wrong, this may not work.
I also noticed that you have your menuLoaded method inside your constructor. Not a good idea. Especially since the next line is expecting playBtn to exist and the menu hasn't even been loaded.
Try something like this :
public function Main()
{
addChild(gameLayer);
addChild(endGameLayer);
addChild(menuLayer);
var myMenu:Loader = new Loader();
var url:URLRequest = new URLRequest("Menu.swf");
myMenu.load(url);
myMenu.contentLoaderInfo.addEventListener(Event.COMPLETE, menuLoaded);
}
function menuLoaded(event:Event):void
{
var myMenu:LoaderInfo = event.currentTarget as LoaderInfo;
menuLayer.addChild(myMenu.content);
var menuInstance:MovieClip = menuLayer.getChildAt(0) as MovieClip;
// now you can actually add the listener, because the content is actually loaded
menuInstance.playBtn.addEventListener(MouseEvent.CLICK, startGame);
}
public function startGame(e:Event)
{
// Code to remove the menu (menuLayer.removeChild?)
// Code here to start timers etc
}

How to update children in actionscript?

For starters let me just say that I am very new to action script and also that I'm not using adobe creative suite, I am using notepad with flex as a compiler. I have two classes, a main class and a class called OBJECT_square.
Here is the MAIN class:
package
{
import flash.display.*; import mx.core.*;
import flash.events.*; import mx.collections.*;
import flash.geom.*; import mx.controls.*;
import flash.text.*; import mx.events.*;
import mx.styles.*;
import mx.containers.*;
public class MAIN extends Sprite
{
public var APPLICATION:Application = Application(Application.application);
public var keyDownText:TextField = new TextField();
public function MAIN()
{
stage.addEventListener(KeyboardEvent.KEY_DOWN,KEY_DOWN);
addEventListener(Event.ENTER_FRAME,STEP);
this.addChild(new OBJECT_square().CREATE(10,100,1));
this.addChild(new OBJECT_square().CREATE(10,200,1));
}
public function STEP():void {}
public function DRAW():void {}
public function KEY_DOWN(event:KeyboardEvent):void
{
keyDownText.text = "Key code: " + event.keyCode;
this.addChild(keyDownText);
}
}
}
Here is the OBJECT_square class:
package
{
import flash.display.*;
import flash.events.*;
public class OBJECT_square extends Sprite
{
public var X:int = 0;
public var Y:int = 0;
public var DEPTH:int = 0;
public var SPRITE:Sprite = new Sprite();
public function CREATE(X:int,Y:int,DEPTH:int):Sprite
{
addEventListener(KeyboardEvent.KEY_DOWN,KEY_DOWN);
this.DEPTH = DEPTH;
this.X = X;
this.Y = Y;
DRAW();
return SPRITE;
}
public function KEY_DOWN(event:KeyboardEvent):void
{
if (event.keyCode == 39) {X += 1; DRAW();}
}
public function DRAW():void
{
SPRITE.graphics.beginFill(0xFF0000,1);
SPRITE.graphics.drawRect(X - 10,Y - 10,20,20);
SPRITE.graphics.endFill();
}
}
}
Now my problem is this. Simply put I would like the square that OBJECT_square draws to move right when I press the right arrow key. Right now I can only get it to be drawn once (though I am actually creating two separate squares in two different spots). Now the thing I need to know is if I have used the keyboard event correctly in OBJECT_square. I used keyboard event in the main class as well to display the last key I pressed but I'm not sure how to use it in a class other than the main one. Also I'm not sure how to 'update' the square so that it is redrawn when it moves, that is remove its old self from the display list and reinsert its new self. The whole kinda point to the code is that the square be 'self-sufficient', ie contains all the code needed to draw it and move it rather than relying on any other class. I basically want it so that once another class has made a square the square is capable of doing everything else on its own.
You don't need to keep manually redrawing the square. Sprites have x and y properties that you can change and will automatically be moved. There is also no reason to be creating a sprite inside a class that is already extending sprite. There are a couple of other weird things you are doing so a fixed up version of your class would look something like:
package
{
import flash.display.*;
import flash.events.*;
public class ObjectSquare extends Sprite
{
public function ObjectSquare (x:int,y:int):void
{
graphics.beginFill(0xFF0000,1);
graphics.drawRect(x,y,30,30);
graphics.endFill();
addEventListener(KeyboardEvent.KEY_DOWN,KEY_DOWN);
}
public function KEY_DOWN(event:KeyboardEvent):void
{
if (event.keyCode == 39)
{
x += 1;
}
}
}
}
package
{
import flash.display.*; import mx.core.*;
import flash.events.*; import mx.collections.*;
import flash.geom.*; import mx.controls.*;
import flash.text.*; import mx.events.*;
import mx.styles.*;
import mx.containers.*;
//rename your class to Main. Should not be all caps
public class Main extends Sprite
{
public var APPLICATION:Application = Application(Application.application);
public var keyDownText:TextField = new TextField();
public function Main()
{
stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyPress);
addChild(new ObjectSquare(10,100));
addChild(new ObjectSquare(10,200));
}
public function onKeyPress(event:KeyboardEvent):void
{
keyDownText.text = "Key code: " + event.keyCode;
this.addChild(keyDownText);
}
}
}
You would probably be better off creating a class to handle all your keyboard inputs and then change the relevant objects.