AS3 pass var from main class to another class - actionscript-3

My code works but its not getting the VAR in the last function. What am I missing?
Main Class Code
package {
import flash.display.MovieClip;
import flash.events.*;
import com.Bubble;
public class numberPop extends MovieClip
{
public var numberBubble:Bubble;
public function numberPop()
{
addBubble(3);
}
public function addBubble(num:Number)
{
var i:Number = num;
numberBubble = new Bubble(i);
addChild(numberBubble);
}
}
}
Second Class Code: Bubbles.as
package com {
import flash.display.MovieClip;
import flash.events.*;
public class Bubble extends MovieClip
{
public var num:Number;
public function Bubble(num:Number)
{
super();
num = num;
trace("Number is: " + num); /// WORKS !!!!!
this.addEventListener(MouseEvent.CLICK, bubbleAction);
}
public function bubbleAction(e:Event)
{
trace(num); // DOES NOT WORK BUT SHOULD.
}
}
}

Your argument in constructor is overriding class property. Change:
num = num;
to
this.num = num;

Related

Symbol class to document class AS3

I have this code in my document class:
package {
import flash.events.Event;
public class Main extends MovieClip {
public var mainMenu = new MainMenu();
public function Main() {
// constructor code
startGame();
}
public function startGame(){
mainMenu.x = stage.stageWidth/2
addChild(mainMenu);
}
public function initGame(event){
//Adding player with and stuff
}
}
}
And this in my MainMenu class:
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() {
// constructor code
logo.x = - logo.width/2;
logo.y = 50;
logo.addEventListener(MouseEvent.CLICK, initGame);
addChild(logo);
}
}
}
I get this message when i try to test the game; 1120: Access of undefined property initGame.
Why can't the mainMenu.as access the public function initGame?
Thanks
You require a reference to the Main object inside MainMenu. You can pass it through the constructor:
mainMenu = new MainMenu(this);
And inside MainMenu you can now register an event to a method inside Main.
public function MainMenu(main:Main) {
logo.addEventListener(MouseEvent.CLICK, main.initGame);
//...
}

how to bring movie clip on front in as3

I have two movieclips one over other. using mouse click event I want to bring one of them in the front. It works 1 or 2 times then it just stops responding to mouse clicks. I don't know what is happening. I tried hard but could not get it work.
Here is my code for document class :
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.SimpleButton;
public class THREE2DP extends MovieClip {
public var page11:page1;
public var page22:page2;
public var scene11:scene1;
public var scene22:scene2;
public function THREE2DP() {
// constructor code
stop();
createscene();
createpage2();
createpage1();
this.addEventListener(Event.ENTER_FRAME, enterframehandler);
}
public function enterframehandler(e:Event):void
{
if(scene11.front)
{
bringToFront(page11);
scene22.front = false;
}
if(scene22.front)
{
bringToFront(page22);
scene11.front = false;
}
}
private function bringToFront(mcl:MovieClip)
{
mcl.parent.setChildIndex(mcl,mcl.parent.numChildren - 1);
}
public function createpage1()
{
page11 = new page1();
addChild(page11);
page11.x = 0;
page11.y = 0;
}
public function createpage2()
{
page22 = new page2();
addChild(page22);
page22.x = 0;
page22.y = 0;
}
public function createscene()
{
scene11 = new scene1();
addChild(scene11);
scene11.x = 0;
scene11.y = 635;
scene22 = new scene2();
addChild(scene22);
scene22.x = 400;
scene22.y = 635;
}
}
}
here is code for scene11 movieclip custom class
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.SimpleButton;
public class scene1 extends MovieClip {
public var front:Boolean = false;
public function scene1() {
// constructor code
stop();
this.addEventListener(MouseEvent.CLICK, clickhandler, false, 0, true);
}
public function clickhandler(event:MouseEvent): void
{
front = true;
}
}
}
code for scene22 custom class is
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.SimpleButton;
public class scene2 extends MovieClip {
public var front:Boolean = false;
public function scene2() {
// constructor code
stop();
this.addEventListener(MouseEvent.CLICK, clickhandler, false, 0, true);
}
public function clickhandler(event:MouseEvent):void
{
front = true;
}
}
}
Upon click on movieclips scene11 and scene22, movieclip page11 and page22 should come on front of stage respectively but that happens only once for each page, after that nothing changes.
my earlier logic for custom classwas faulty. took me 2 day. added mouse_down event to make it work.
i some how managed to do this right by changing custom class code to this
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.SimpleButton;
public class scene1 extends MovieClip {
public var front:Boolean = false;
public function scene1() {
// constructor code
stop();
this.addEventListener(MouseEvent.MOUSE_DOWN, presshandler, false, 0, true);
this.addEventListener(MouseEvent.CLICK, clickhandler, false, 0, true);
}
public function presshandler(event:MouseEvent): void
{
front = true;
}
public function clickhandler(event:MouseEvent): void
{
front = false;
}
}
}
also modified document class code by calling pagechange() function from enterframehandler() function for sake of simplicity
public function pagechange()
{
if (scene11.front && scene22.front == false)
{
bringToFront(page11);
}
if (scene22.front && scene11.front == false)
{
bringToFront(page22);
}
}
private function bringToFront(mcl:MovieClip)
{
mcl.parent.setChildIndex(mcl,mcl.parent.numChildren - 1);
}
Instead of using that bringToFront method, you could just readd page11 and page22 on stage with addChild(). addChild always adds a child on front of everything else in the parent.
public function enterframehandler(e:Event):void
{
if(scene11.front)
{
addChild(page11);
scene22.front = false;
}
if(scene22.front)
{
addChild(page22);
scene11.front = false;
}
}

AS3 Object appears onClick where the mouse clicks on the stage

I want in actionscript to place an object in my library onto the stage where I clicked. Seems easy? Right? TOTALLY BLANKING. any help would be awesome :)
my code thus far is:
package code {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Main extends MovieClip {
public var redBox: Box = new Box(mouseX, mouseY);
public function Main() {
// constructor code
stage.addEventListener(MouseEvent.CLICK, mouseClickEvent);
}
public function mouseClickEvent(e:MouseEvent):void {
addChild(redBox);
}
}
}
that is the main and then the box code is:
package code {
import flash.display.MovieClip;
public class Box extends MovieClip{
public function Box(myX:Number, myY:Number) {
// constructor code
myX = x;
myY = y;
}
}
}
Just do this :
package code {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Main extends MovieClip {
public var redBox: Box = new Box();
public function Main() {
// constructor code
stage.addEventListener(MouseEvent.CLICK, mouseClickEvent);
}
public function mouseClickEvent(e:MouseEvent):void {
redBox.x = stage.mouseX;
redBox.y = stage.mouseY;
addChild(redBox);
}
}
}

Propagate custom event to Parent

I have got a custom event set up (see below), but when I listen for the event in my main class, and it gets dispatched from the child class, it never gets captured.
TRIED:
this.b.addEventHandler(GameLaunchEvent.GAME_LAUNCH_EVENT, this.eventHandler)
package com.thom.events
{
import flash.display.MovieClip;
import flash.events.Event;
/**
* ...
* #author
*/
public class LaunchEventAbstract extends Event
{
public var parent:MovieClip;
public function LaunchEventAbstract(type:String, parent:MovieClip = null)
{
super(type, true);
this.parent = parent;
}
}
}
package com.thom.events
{
import flash.display.MovieClip;
import flash.events.Event;
/**
* ...
* #author
*/
public class GameLaunchEvent extends LaunchEventAbstract
{
public static const GAME_LAUNCH_EVENT:String = "GameLaunchEvent";
public function GameLaunchEvent(parent:MovieClip = null) {
trace("GameLaunchEvent");
super(GAME_LAUNCH_EVENT, parent);
}
}
}
//example code
package {
import com.thom.events.*;
public class A extends MovieClip{
public var b:B;
public function A(){
addEventListener(GameLaunchEvent.GAME_LAUNCH_EVENT, eventHandler);
this.b = new B();
addChild(b);
}
public function eventHandler(e:GameLaunchEvent){
trace("Success");
}
}
}
package {
import com.thom.events.*;
public class B extends MovieClip{
public function B() {
dispatchEvent(new GameLaunchEvent(this));
}
}
}
Event Bubbling is what you want:
Parent:
childClip.addEventListener('CUSTOM_EVENT', handler);
Child:
this.dispatchEvent(new Event('CUSTOM_EVENT', true, true));
This will propagate it up the display list. The problem with listening to a loader directly is that it looks like this:
Loader
- Content
Without bubbling you'd have to listen to the content directly, which is kind of pointless since you can't listen to it until the content has been loaded.
You don't really need to pass the parent as a parameter , particularly if you intend to listen to your event in the parent itself. What you can do is pass a dispatcher as a parameter and let the dispatcher both dispatch & listen to the event.
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.EventDispatcher;
public class A extends MovieClip
{
public var b:B;
private var _dispatcher:EventDispatcher = new EventDispatcher();
public function A()
{
_dispatcher.addEventListener('test', eventHandler);
this.b = new B(_dispatcher);
}
public function eventHandler(e:Event):void
{
trace("Success");
}
}
}
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.EventDispatcher;
public class B extends MovieClip
{
private var _dispatcher:EventDispatcher;
public function B(dispatcher:EventDispatcher)
{
this._dispatcher = dispatcher;
_dispatcher.dispatchEvent(new Event('test'));
}
}
}

Actionscript: add a sprite from a subclass to the display list of its super class?

Say i have these two classes:
MAIN.as
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);
new OBJECT_square().CREATE(10,100,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);
}
}
}
OBJECT_square.as
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):void
{
this.DEPTH = DEPTH;
this.X = X;
this.Y = Y;
DRAW();
}
public function DRAW():void
{
SPRITE.graphics.beginFill(0xFF0000,1);
SPRITE.graphics.drawRect(X - 10,Y - 10,20,20);
SPRITE.graphics.endFill();
addChild(SPRITE);
}
}
}
Now how is it that I can add the variable SPRITE which is a Sprite in the OBJECT_square class to the display list of MAIN class? I've tried addChild(SPRITE) and super.addChild(SPRITE). If everything works I should see a red square somewhere on the screen but right now its all blank, except for the text drawn in the MAIN class.
Basically I want it so i can just make a new OBJECT_square and it will draw itself without any more instructions from the MAIN class.
Try this:
var obj:OBJECT_square = new OBJECT_square();
obj.CREATE(10,100,1);
addChild(obj);
Or, if you really want to do it in one go, you could try this:
In main
addChild((new OBJECT_square()).CREATE(10,100,1));
And change your draw function to return the square object
public function DRAW():OBJECT_square
{
SPRITE.graphics.beginFill(0xFF0000,1);
SPRITE.graphics.drawRect(X - 10,Y - 10,20,20);
SPRITE.graphics.endFill();
addChild(SPRITE);
return this;
}