AS3 TypeError: Error #2007: Parameter hitTestObject must be non-null - actionscript-3

TypeError: Error #2007: Parameter hitTestObject must be non-null.
I am working on a game right now and trying to get it so the player will collide with the ground (he already collides with table)
It says the problem is happening in playerOneCollisions
table and ground are just classes with the basic class code linked to movieclips,
they are extended movieclips (if that matters), and they have imported stage and movieclip (again, if that matters)
(the parts I think are important)
Player.as
public var main:Main;
public var table:Table;
public var player:Player;
public var ground:Ground;
public function playerOneCollisions(table, ground)
{
if(this.hitTestObject(table))
{
trace("tabled");
animation = "kong";
}
if(this.hitTestObject(ground))
{
trace("grounded");
animation = "idle";
}
}
The function playerOneCollisions is called in Main.as
public function GameClock(timerEvent:TimerEvent):void
{
player.playerOnePress();
player.playerOneKeyResults();
player.playerOneCollisions(table, ground);
player.playerAnimaitons();
}
The rest of the code incase I was wrong about the important parts
Player.as
package
{
import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.Event;
import KeyObject;
import Table;
public class Player extends MovieClip
{
public var stageRef:Stage;
public var key:KeyObject;
public var main:Main;
public var table:Table;
public var player:Player;
public var ground:Ground;
public var leftPressed:Boolean = false;
public var rightPressed:Boolean = false;
public var upPressed:Boolean = false;
public var downPressed:Boolean = false;
public var grounded:Boolean = false;
public var animation:String = "idle";
public var runSpeed:Number = 5;
public var animationState:String = "idle";
public function Player (stageRef:Stage, X:int, Y:int):void
{
this.stageRef = stageRef;
this.x = X;
this.y = Y;
key = new KeyObject(stageRef);
}
public function playerOnePress()
{
if(key.isDown(37) || key.isDown(65)){ // if left arrow or A is pressed
leftPressed = true;
//trace("left pressed");
} else {
leftPressed = false;
}
if(key.isDown(38) || key.isDown(87)){ // if up arrow or W is pressed
upPressed = true;
//trace("up pressed");
} else {
upPressed = false;
}
if(key.isDown(39) || key.isDown(68)){ //if right arrow or D is pressed
rightPressed = true;
//trace("right pressed");
} else {
rightPressed = false;
}
if(key.isDown(40) || key.isDown(83)){ //if down arrow or S is pressed
downPressed = true;
//trace("down pressed");
} else {
downPressed = false;
}
}
public function playerOneKeyResults()
{
if(leftPressed)
{
// trace("left");
this.x -= runSpeed;
}else if(rightPressed)
{
// trace("right");
this.x += runSpeed;
}
}
public function playerOneCollisions(table, ground)
{
if(this.hitTestObject(table))
{
trace("tabled");
animation = "kong";
}
if(this.hitTestObject(ground))
{
trace("grounded");
animation = "idle";
}
}
public function playerAnimaitons()
{
if(animation == "kong")
{
this.gotoAndStop(2);
}
if(animation == "idle")
{
this.gotoAndStop(1);
}
}
}
}
Main.as
package
{
import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;
public class Main extends MovieClip
{
public var gameTimer:Timer;
public var player:Player;
public var table:Table;
public var ground:Ground;
public function Main():void
{
gameTimer = new Timer (30);
gameTimer.addEventListener(TimerEvent.TIMER, GameClock);
gameTimer.start();
player = new Player(stage, 80, 420);
stage.addChild(player);
}
public function GameClock(timerEvent:TimerEvent):void
{
player.playerOnePress();
player.playerOneKeyResults();
player.playerOneCollisions(table, ground);
player.playerAnimaitons();
}
}
}

One thing I notice is that you have table and ground as class properties of Player, and they are also parameters for the method you are having trouble with.
The table and/or ground properties of your Player class are likely null as I don't see code that sets them.
It's not good practice to have method parameters named the same as your class properties.
Try removing the properties in your Player class. (I don't see them being set or used)
If you still get the error, trace out their values at the beginning of the playerOneCollisions method and ensure they are not null.
If they are null, the problem exists outside this class and you are passing in a null value.
I should also note that I don't see anywhere in the Main class where you set the table or ground properties, so they are null unless you have not shown us all the code. If they are null, then that will certainly cause the issue you are experiencing.

Related

Movement Keys not Working

Nothing is happening when I press the arrow keys, but neither are there any errors: what's wrong with this? If I remove the key press testing it accelerates accordingly...
At this stage I am just trying to move a block around a screen in an inertial manner using the arrow keys. However, this is my first foray into AS3 so I may be going about it in completely the wrong manner.
Any help would be greatly appreciated.
Unit.AS:
package {
import flash.display.MovieClip;
import flash.events.*
import flash.ui.Keyboard
public class Unit extends MovieClip {
var velocityX:Number = 1;
var velocityY:Number = 1;
var accellerationX:Number = 1;
var accellerationY:Number = 1;
public function Unit(){
addEventListener("enterFrame", move);
}
private function move(e:Event){
accellerate()
this.x += velocityX;
this.y += velocityY;
}
private function accellerate(){
if (Key.isDown(Keyboard.UP)){
velocityY += accellerationY;
trace("Accellerating");
}
if (Key.isDown(Keyboard.DOWN)){
velocityY -= accellerationY;
trace("Accellerating");
}
if (Key.isDown(Keyboard.RIGHT)){
velocityX += accellerationX;
trace("Accellerating");
}
if (Key.isDown(Keyboard.LEFT)){
velocityX -= accellerationX;
trace("Accellerating");
}
}
}
}
Key.AS:
package
{
import flash.display.Stage;
import flash.events.Event;
import flash.events.KeyboardEvent;
public class Key {
private static var initialized:Boolean = false;
private static var keysDown:Object = new Object();
public static function initialize(stage:Stage) {
if (!initialized) {
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
stage.addEventListener(Event.DEACTIVATE, clearKeys);
initialized = true;
}
}
public static function isDown(keyCode:uint):Boolean
{
return Boolean(keyCode in keysDown);
}
private static function keyPressed(event:KeyboardEvent):void {
keysDown[event.keyCode] = true;
}
private static function keyReleased(event:KeyboardEvent):void {
if (event.keyCode in keysDown) {
delete keysDown[event.keyCode];
}
}
private static function clearKeys(event:Event):void {
keysDown = new Object();
}
}
}
On your unit constructor function call the initialize(stage) static function.
Key.initialize(stage);

How do I get my movieclip character to move?

I've been trying for a few hours now and I cant get my little character to move with the keyboard.
I have ran a trace to make see if anything was happening and the position value does change but my character doesn't react to that position change.
I receive no errors. Both my Character and BrickBlock are movieclips and they have been imported for ActionScript.
If any other information is needed please let me know. Thank you! :)
My following code:
package {
import flash.events.Event
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class CharMove extends MovieClip {
var char1 :Character;
var block :BrickBlock;
public function CharMove()
{
char1 = new Character();
block = new BrickBlock();
//this.addEventListener(Event.ENTER_FRAME, collide)
stage.addEventListener(KeyboardEvent.KEY_DOWN, kDown);
}
/*function collide(e:Event):void
{
if(char.hitTestObject(block))
{
char.visible = !char.visible;
}
}*/
function kDown(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.LEFT:
char1.x -= 5;
trace(char1.x);
break;
case Keyboard.RIGHT:
char1.x +=5;
trace(char1.x);
break;
}
}
}
}
You might want to consider writing a static Input class.
package input {
import flash.display.Stage;
import flash.events.KeyboardEvent;
public class Input {
private static var keys:Array = new Array(255);
public static function setup(stage:Stage):void {
stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, KeyUp);
}
private static function keyDown(e:KeyboardEvent):void {
keys[e.keyCode] = true;
}
private static function keyUp(e:KeyboardEvent):void {
keys[e.keyCode] = false;
}
public static function isKeyDown(k:int):Boolean {
return keys[k];
}
public static const A:uint = 65;
public static const B:uint = 66;
public static const C:uint = 67;
// The rest of the keys...
}
}
To use it first call setup() which adds the listeners for KEY_DOWN and KEY_UP events.
They you can easily query keys and do relevant actions accordingly.
Input.setup(stage);
/...
if(Input.isKeyDown(Input.A)) {
char1.x -= 5;
}

Projectiles not moving as they are supposed to

package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
public class supportForce extends MovieClip
{
public function supportForce()
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, onDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onUp);
stage.addEventListener(Event.ENTER_FRAME, keyCheck);
}
private function onUp(e:KeyboardEvent):void
{
_keyDownStatus[e.keyCode] = false;
}
private function onDown(e:KeyboardEvent):void
{
_keyDownStatus[e.keyCode] = true;
}
private function keyCheck(event:Event)
{
if (_keyDownStatus[90])
{
var GreenLight:Projectile1 = new Projectile1();
stage.addChild(GreenLight);
GreenLight.x = Player2Child.x;
GreenLight.y = Player2Child.y;
}
GreenLight.x -= defaultSpeed;
}
}
}
So the projectile here is Projectile1 and it is declared as GreenLight in the keyCheck Function. So in the same function (the ENTER.FRAME function) GreenLight is supposed to move to its left. This does not happen and also an error saying TypeError: Error #1009: Cannot access a property or method of a null object reference.
at supportForce/keyCheck() appears. Thank you in advance.
In your keyCheck function, GreenLight will be null if _keyDownStatus[90] is false
so put the GreenLight as a class variable
private var lights:Array = [];
private function keyCheck(event:Event)
{
if (_keyDownStatus[90])
{
var GreenLight:Projectile1 = new Projectile1();
stage.addChild(GreenLight);
GreenLight.x = Player2Child.x;
GreenLight.y = Player2Child.y;
for each (var l:Projectile1 in lights)
{
l.x -= defaultSpeed;
}
lights.push(GreenLight);
}
else
{
//remove all Projectile1s in lights from stage and clear lights if you want
}
}
you create GreenLight in if condition scope and if keyCode isnot 90, object of Projectile1 isnot created and GreenLight.x -= defaultSpeed; there GreenLight is null

Inherit MovieClip From Another Class

Hey I seem to be having some problems inheriting Movie-clip from a class, I'm fairly new to as3, I've had a look around and can't tell if I'm doing something fundamentally wrong or not.
Let me show you
So I have a class I want to use to move EVERYTHING but the player sprite. So I want everything but the player to extend it. (or so I'm assuming.)
So I declare my class
package code {
import flash.display.*;
import flash.events.*;
import flash.ui.*;
import code.Main;
public class everythingContainer extends MovieClip {
function brackets and so on...
(I'm just importing everything in an attempt to avoid errors)
I then have a class I want to inherit everythingContainer and Movieclip
package code {
import flash.display.*;
import flash.events.*;
import flash.ui.*;
import code.Main;
import code.everythingContainer;
public class Tree1 extends everythingContainer {
Yet when I run this I get the error:
Line 1 5000: The class 'code.Tree1' must subclass 'flash.display.MovieClip' since it is linked to a library symbol of that type.
Why am I getting this error?
Any help would be greatly appreciated!
I haven't got the full code to run yet so there may still be other obvious bugs laying about.
everythingContainer
Full code:
package code {
import flash.display.*;
import flash.events.*;
import flash.ui.*;
import code.Main;
public class everythingContainer extends MovieClip {
var speed: Number = 4;
var wpressed: Boolean = false;
var apressed: Boolean = false;
var spressed: Boolean = false;
var dpressed: Boolean = false;
var xprev:int = 0;
var yprev:int = 0;
public function everythingContainer() {
// constructor code
trace ('Container started');
if(stage) init();
else
this.addEventListener(Event.ADDED_TO_STAGE, init);
}
function init(eventInfo:Event = null):void
{
if(eventInfo != null)
{
this.removeEventListener(Event.ADDED_TO_STAGE, init);
trace ('Container init removed');
}
// constructor code
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPress);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyRelease);
this.addEventListener( Event.ENTER_FRAME, containerEveryFrame);
}
public function containerEveryFrame (Event): void {
if (stage.contains(Main.player)) {
xprev = this.x;
yprev = this.y;
checkPlayerMovement();
}
}
// check the set keypress variables
public function checkPlayerMovement () : void {
if (wpressed) {
this.y -= this.speed;
}
if (spressed) {
this.y += this.speed;
}
if (apressed){
this.x -= this.speed;
}
if (dpressed) {
this.x += this.speed;
}
}
//assign key presses to variables
public function onKeyPress (event:KeyboardEvent):void {
//up
if (event.keyCode == 87){
wpressed = true;
}
//down
if (event.keyCode == 83) {
spressed = true;
}
//left
if (event.keyCode == 65){
apressed = true;
}
//right
if (event.keyCode == 68) {
dpressed = true;
}
}
//reset key press variables
public function onKeyRelease (event:KeyboardEvent) : void {
//up
if (event.keyCode == 87){
wpressed = false;
}
//down
if (event.keyCode == 83) {
spressed = false;
}
//left
if (event.keyCode == 65){
apressed = false;
}
//right
if (event.keyCode == 68) {
dpressed = false;
}
}
}
}
Main (there's some other stuff going on in here, but at the minute I'm just trying to get the trees working with my other class.)
package code
{
import flash.display.*;
import flash.events.*;
import flash.ui.*;
import flash.geom.Rectangle;
import flashx.textLayout.container.ContainerController;
public class Main extends MovieClip
{
//public static var main
public static var player:PC;
//public static var firstenemy: WolfEnemy;
public static var MainContainer:everythingContainer;
public function Main()
{
// constructor code
trace('main started');
if (stage)
{
init();
}
else
{
this.addEventListener(Event.ADDED_TO_STAGE, init);
}
}
function init(eventInfo:Event = null):void
{
if (eventInfo != null)
{
this.removeEventListener(Event.ADDED_TO_STAGE, init);
trace('Main init removed');
}
MainContainer = new everythingContainer ;
MainContainer.x = stage.stageWidth / 2;
MainContainer.y = stage.stageHeight / 2;
stage.addChild(MainContainer);
player = new PC();
player.x = stage.stageWidth / 2;
player.y = stage.stageHeight / 2;
stage.addChild(player);
//firstenemy = new WolfEnemy();
//firstenemy.x = 100;
//firstenemy.y = 100;
//stage.addChild(firstenemy);
stage.addEventListener( Event.ENTER_FRAME, everyFrameMain);
}
// check if an enemy hits the player.
/*public function enemycollison(): void {
if(firstenemy.hitTestObject(player)){
trace ('hit enemy');
player.health--;
firstenemy.kill();
}
}*/
// manage events that need to haapen globally for every frame
public function everyFrameMain(Event):void
{
/*if (stage.contains(firstenemy)){
enemycollison();
} */
//root.scrollRect = new Rectangle(player.x - 400, player.y - 300, 800, 600);
}
// finish and close game
public function endgame():void
{
}
}
}
and finally my tree class
package code {
import flash.display.*;
import flash.events.*;
import flash.ui.*;
import code.Main;
import code.everythingContainer;
public class Tree1 extends everythingContainer {
public function Tree1()
{
// constructor code
trace ('Tree1 started');
if(stage) init();
else
this.addEventListener(Event.ADDED_TO_STAGE, init);
}
function init(eventInfo:Event = null):void
{
if(eventInfo != null)
{
this.removeEventListener(Event.ADDED_TO_STAGE, init);
trace ('Tree1 init removed');
}
this.addEventListener( Event.ENTER_FRAME, tree1EveryFrame);
}
public function tree1EveryFrame (Event): void {
playercollision();
}
public function playercollision(): void {
if(this.hitTestObject(Main.player)){
trace ('hit wall');
Main.player.x = Main.player.xprev;
Main.player.y = Main.player.yprev;
}
}
}
}
Probably due to linkage errors inside Fla-file.
RMB on library-item: Tree1
Export for ActionScript + Export in frame 1 + "class: code.Tree1"
File/Publish Settings/Actionscript Settings (the small wrench, right of Script-dropdown)
Source Path (add the linkage to where the compiler can find your package-folder), usually for me I crate a folder next to the fla file called src or something like that so the code-file would be found at "/MyProject/src/code/Tree1.as", in that case I add "./src/" inside Source path inside Advanced ActionScript 3.0 settings
Added an example project in Flash CS6 found at url:
https://user.iter.org/filesharing/?uid=927205f7-cdfe-4915-a175-bc87f64af444
that is available for ~40 days.
Project structure in that file:
"/MyProject/DeepInheritage.fla"
"/MyProject/src/code/Foobar.as"
"/MyProject/src/code/Tree1.as"
Foobar.as which extends MovieClip
Tree1 library item which extends Foobar
That should be the exact same thing that you described in your issue, meaning that there is nothing wrong with that approach, it is just a matter of finding what is wrong. Most likely that is due to errors inside FLA-file, but might be something else.
code files:
package code {
import flash.display.MovieClip;
public class Foobar extends MovieClip {
public function Foobar() {
trace("foobar ctor()");
}
}
}
package code {
import flash.display.MovieClip;
public class Tree1 extends Foobar {
public function Tree1() {
trace("Tree1 ctor()");
}
}
}

as3 addEventListner on a function in another class

I have a class calling a function in another class. I want to know
if we can add an Event Listener to know if that function has fired and is finished etc.
Here is the section of the code that is relevant.
myMC = new pictures();
addChild(myMC);
myMC.loadImages("Joyful",1)
// Add Event Listener here to let me know loadImages was called and worked.
As you can see the function is called loadImages() and it is located in a class called pictures.as
Can I add an event listener to this?
Here is a new issue. I am using a similar method and I used your example below and it did not work. Am I missing an import or something? I am only showing the functions that pertain and not my whole script.
Main.class
package
{
import flash.display.MovieClip;
import flash.events.*;
import flash.display.*;
import flash.filesystem.*;
import com.greensock.*;
import com.greensock.easing.*;
import flash.system.System;
// Ed's Scripts
import com.*;
import com.views.*;
public class iRosaryMain extends MovieClip
{
/// (Get Main Doc flow) this creates an instace of the main timeline
/// and then I send it
private static var _instance:iRosaryMain;
public static function get instance():iRosaryMain
{
return _instance;
}
/// Declaring Vars
var audio:audioPrayers;
/// Loading Images
//public var theImages:pictures = new pictures();
/// Movie Clips
public var myMary:beautifulMary;
public var myMC:MovieClip;
public var myPic:MovieClip;
public var myFlags:MovieClip;
public static var mt:MovieClip;
var vars:defaultVars = new defaultVars();
public function iRosaryMain()
{
// (Get Main Doc flow) Here I send the an instacne of iRosaryMain to defaultVars.as
_instance = this;
vars.getMainDoc(_instance);
// Sets timeline to mt :) I hope
mt = _instance;
audio = new audioPrayers();
trace("Jesus I trust in you!");// constructor code
audio.sayHailMary();
if (stage)
{
init();
}
}
public function moveLanguages()
{
/// File to languages folder
var checkLanguageFolder:File = File.applicationStorageDirectory.resolvePath("Languages");
///// CHECK LANGUAGES FOLDER - if not in App Storage move it
if (! checkLanguageFolder.exists)
{
var sourceDir:File = File.applicationDirectory.resolvePath("Languages");
var resultDir:File = File.applicationStorageDirectory.resolvePath("Languages");
sourceDir.copyTo(resultDir, true);
trace( "Moved Language!");
}
}
//// MAIN FUNCTIONS IN iRosaryMainClass
function init()
{
//loadFlags();
moveLanguages();
//addChild(theImages);
intro();
}
public function loadFlags()
{
myFlags.addEventListener("MyEvent", eventHandler);
myFlags = new viewLanguages();
addChild(myFlags);
myFlags.x = stage.stageWidth / 2 - myFlags.getBounds(this).width / 2;
function eventHandler()
{
trace("yes loaded");
TweenMax.fromTo(myMary,3, {alpha:1}, {alpha:0, ease:Quint.easeOut, onComplete: closePic} );
}
}
function intro()
{
myMary = new beautifulMary();
addChild(myMary);
loadFlags();
}
public function closePic()
{
removeChild(myMary);
}
public function languageLoaded(lang:String)
{
trace("YES... " + lang);
homeIn();
}
public function homeIn()
{
if (myFlags)
{
TweenMax.fromTo(myFlags,1, {x:myFlags.x}, {x:0-myFlags.width, ease:Quint.easeInOut, onComplete:unloadMyFlags} );
}
function unloadMyFlags()
{
trace("Called Ease out");
if (myFlags is MovieClip)
{
//trace("CURRENT DISPLAY " + currentDisplayScreen);
trace("CURRENT mc " + myFlags);
removeChild(myFlags);
System.gc();
myFlags = null;
trace("CURRENT mc " + myFlags);
}
}
if (! myMC)
{
myMC = new viewHome();
myMC.name = "Home";
myMC.x = stage.stageWidth / 2 - myMC.width / 2;
addChild(myMC);
}
theEaseIn(myMC);
//Home.B1.addEventListener(MouseEvent.CLICK, theEaseOut(Home));
}
public function loadLanguage(Language:String):Function
{
return function(e:MouseEvent):void ;
{
trace("Did it work? " +Language);
vars.loadButtonVars(Language);;
};
//TweenMax.fromTo(EnglishButton,1, {x:EnglishButton.x}, {x:0-EnglishButton.width, ease:Quint.easeOut} );
}
public function loadView(screen:String)
{
trace("Received " + screen);
if (screen=="Home")
{
myMC = new viewHome();
addChild(myMC);
theEaseIn(myMC);
//Home.B1.addEventListener(MouseEvent.CLICK, theEaseOut(Home));
}
if (screen=="PrayTheRosary")
{
myMC = new viewPrayTheRosary();
addChild(myMC);
theEaseIn(myMC);
//Home.B1.addEventListener(MouseEvent.CLICK, theEaseOut(Home));
}
if (screen=="Options")
{
myMC = new viewOptions();
addChild(myMC);
theEaseIn(myMC);
}
if (screen=="Downloads")
{
myMC = new viewDownloads();
addChild(myMC);
theEaseIn(myMC);
}
if (screen=="beautifulMary")
{
myMC = new beautifulMary();
addChild(myMC);
theEaseIn(myMC);
}
if (screen=="Flags")
{
myFlags = new viewLanguages();
addChild(myFlags);
theEaseIn(myFlags);
}
if (screen=="Joyful" || screen=="Sorrowful" || screen=="Glorious" || screen=="Luminous")
{
myPic = new pictures();
addChild(myPic);
myPic.addEventListener( "ImagesLoaded", doTheEaseIn);
myPic.loadImages(""+screen+"",1);
function doTheEaseIn()
{
theEaseIn(myPic);
}
}
}
public function theEaseIn(mc:MovieClip)
{
if (myMC)
{
TweenMax.fromTo(mc,1, {x:stage.stageWidth+mc.width}, {x:stage.stageWidth/2 - mc.width/2, ease:Quint.easeInOut} );
}
}
public function theEaseOut(mc:MovieClip,nextMC:String):Function
{
return function(e:MouseEvent):void ;
{
loadView(nextMC);
/// Tweens out view on screen
TweenMax.fromTo(mc,1, {x:mc.x}, {x:0-mc.width, ease:Quint.easeInOut, onComplete:unloadMC} );
function unloadMC();
{
trace("Called Ease out");
if(mc is MovieClip);
{
//trace("CURRENT DISPLAY " + currentDisplayScreen);
trace("CURRENT mc " + mc);
removeChild(mc);
System.gc();
myMC = null;
trace("CURRENT mc " + mc);
};
};
};/// end return
}
////////////
}/// End iRosaryMain
}// End Package
viewLanguages.as
package com.views
package com.views
{
import flash.display.MovieClip;
import flash.filesystem.File;
import com.roundFlag;
import flash.events.*;
import flash.display.*;
public class viewLanguages extends MovieClip
{
var Flag:MovieClip;
//public static const FLAGS_LOADED:String = "flagsLoaded";
public function viewLanguages()
{
getLang();
}
function numCheck(num:int):Boolean
{
return (num % 2 != 0);
}
/// Get for App Opening
public function getLang()
{
/// When Live
var folderLanguages:File = File.applicationStorageDirectory.resolvePath("Languages");
var availLang = folderLanguages.getDirectoryListing();
var Lang = new Array();
var LangPath = new Array();
var fl:int = 0;
for (var i:uint = 0; i < availLang.length; i++)
{
if (availLang[i].isDirectory)
{
//flag = new Loader();
//flag.load(new URLRequest(File.applicationStorageDirectory.url + "Languages/" + Lang[i] + "/roundFlag.png"));
Lang.push(availLang[i].name);
LangPath.push(availLang[i].nativePath);
Flag = new roundFlag(availLang[i].name);
Flag.name = availLang[i].name;
if (numCheck(i)==false)
{
Flag.y = fl;
Flag.x = 0;
}
else
{
Flag.y = fl;
Flag.x = Flag.width + 33;
fl = fl + Flag.height + 33;
}
Flag.btext.text = availLang[i].name;
addChild(Flag);
trace(availLang[i].nativePath);// gets the name
trace(availLang[i].name);
}
}
trace("Get Lang Called");
dispatchEvent(new Event("MyEvent"));
}
}
}
Yes, you can. Considering you are adding your clip to the display list I assume it extends either Movieclip or Sprite, both of which extend EventDispatcher. In your pictures class you can simply use dispatchEvent(new Event("MyEvent"); to dispatch an event. Then you could add myMC.addEventListener("MyEvent", eventHandler); to react to it.
You should also not use strings for events like I wrote above. It would be better if you declared a public static const IMAGES_LOADED:String = "imagesLoaded"; in your pictures class. Then you should use this constant by dispatching and by listening to it.
EDIT: Here how it would look with the constant:
//your class
package {
import flash.events.Event;
import flash.display.Sprite;
public class Pictures extends Sprite {
public static const IMAGES_LOADED:String = "imagesLoaded";
public function Pictures() {
}
public function onAllImagesLoaded():void {
dispatchEvent(new Event(IMAGES_LOADED));
}
//rest of your methods
}
}
//you would call it like this:
var myMc:Pictures = new Pictures();
myMc.addEventListener(Pictures.IMAGES_LOADED, onImagesLoaded);
myMc.loadImages();
function onImagesLoaded(e:Event):void {
//...etc
}
You have to create custom event class for this to work. Like:
package com.some
{
import flash.events.Event;
public class SomeEvent extends Event
{
// Loading events
public static const MAINDATA_LOADING:String = "onMainDataLoading";
// Other event
public static const SOME_LOADING:String = "onSomeLoading";
public var params:Object;
public function SomeEvent($type:String, $params:Object, $bubbles:Boolean = false, $cancelable:Boolean = false)
{
super($type, $bubbles, $cancelable);
this.params = $params;
}
public override function clone():Event
{
return new SomeEvent(type, this.params, bubbles, cancelable);
}
}
}
Add event dispatch inside wanted class (Pictures) and pass wanted params to event (this or any else)
dispatchEvent(new SomeEvent(SomeEvent.IMAGES_LOADED, this));
Handle event listening:
var myMc:Pictures = new Pictures();
myMc.addEventListener(SomeEvent.IMAGES_LOADED, onImagesLoaded);
myMc.loadImages();
function onImagesLoaded(e:SomeEvent):void {
// handle event.
var picts:Pictures = (e.params as Pictures);
}