1120: Access of undefined property stage - actionscript-3

I'm trying to make a class however I get these errors:
... \SubtitleLoader.as, Line 14, Column 8 1120: Access of undefined
property stage. ... \SubtitleLoader.as, Line 20, Column 10 1061: Call
to a possibly undefined method addEventListener through a reference
with static type SubtitleLoader.
Here's my code:
package
{
import flash.events.*;
import flash.display.Stage;
import flash.net.URLRequest;
import flash.net.URLLoader;
public class SubtitleLoader
{
private var str:String;
public function init():void
{
if (stage)
{
LoadText();
}
else
{
this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
}
private function onAddedToStage(event:Event):void
{
this.removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
LoadText();
}
private function LoadText():void
{
var url:URLRequest = new URLRequest("aman.srt");
var urlLoader:URLLoader = new URLLoader();
urlLoader.load(url);
urlLoader.addEventListener(Event.COMPLETE, onComplete);
}
private function onComplete(event:Event):void
{
trace(event.data);
}
}
}
How can I fix this.

You can't add events because your class doesn't extend EventDispatcher. Your class can't be added to the stage because it doesn't extend any display classes. You probably want to extend flash.display.Sprite (which also extends EventDispatcher):
package
{
import flash.events.*;
import flash.display.Sprite; //import sprite
import flash.display.Stage;
import flash.net.URLRequest;
import flash.net.URLLoader;
public class SubtitleLoader extends Sprite //extend sprite, inheriting EventDispatcher as well
{
Also, it is bad coding style to give your function names UpperCamelCase (LoadText). UpperCamelCase is reserved for class names.

Related

actionscript 3 - error #1009 Cannot access a property or method of a null object reference

I've only recently started using as so sorry for this
as its probably pretty simple. Im basically trying to spawn an AI unit but am getting the error 1009, here is the full error.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at code::Init()[D:\FlashGame\code\Init.as:21]
Im trying to use a function from another class which is in another file. Here is the first file.
package code
{
import flash.display.DisplayObjectContainer;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.MouseEvent;
import flash.text.TextField;
import code.functions.AIManager;
public class Init extends MovieClip
{
private var _AI:AIManager;
private var _player:MovieClip;
public function Init()
{
stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
_AI.createAI();
}
public function enterFrameHandler(event:Event):void
{}
}
}
And the second file..
package code.functions
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.DisplayObjectContainer;
public class AIManager extends MovieClip
{
private var _ai:MovieClip;
public function AIManager()
{
createAI();
stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
public function createAI():void
{
_ai = new AI();
_ai.x = stage.stageWidth / 2;
_ai.y = stage.stageHeight / 2;
stage.addChild(_ai);
}
You need to create an instance of a class before you can use it's methods. The exception to that is static methods. In your case you just need to use new AIManager
_AI = new AIManager();
_AI.createAI();

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.

AS3 Run gotoAndStop from a class

I have the problem with Actionscript 3.0 in Adobe Flash. I can't run "gotoAndStop" from a class (not document class).
With the help of the Internet I tried several things, but none of them worked:
1)
MovieClip(root).gotoAndStop(3);
2)
package
{
import flash.display.MovieClip;
public class CustomClassName extends MovieClip
{
public static var mainTimeline:MovieClip;
public function CustomClassName()
{
// constructor code
}
}
}
3)
public class np extends SimpleButton {
var _root:MovieClip;
public function np() {
this.addEventListener(Event.ADDED_TO_STAGE,init);
this.addEventListener(MouseEvent.CLICK,nextF);
}
private function init(e:Event):void{
_root = MovieClip(this.root);
}
private function nextF(e:MouseEvent):void{
_root.addEventListener(Event.RENDER,renderF);
stage.invalidate();
_root.nextScene();
}
private function renderF(e:Event):void {
_root.gotoAndStop(5);
}
}
I have these imports:
import flash.display.MovieClip;
import flash.display.Graphics;
import flash.display.Stage;
import flash.events.Event;
And if I run these lines of code:
trace('frame:',currentFrame);
super(this).gotoAndPlay(2);
trace('frame:',currentFrame);
... I get 0 as currentFrame as a result.
I have a class where I want to run gotoAndStop(2).
And in my .fla file I have these in the first frame:
stop();
import Buzzer.*;
var buzzerClip:Buzzer = new Buzzer();
stage.addChild(buzzerClip);
But the code doesn't run the gotoAndStop function. And actually no error will be returned. Does someone has another idea?
The property root is null until the display object has been added to the display list.
So to adjust your first attempt:
public function MyDisplayObject()
{
init();
}
private function init():void
{
addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
}
private function addedToStageHandler()
{
MovieClip(root).gotoAndStop(3);
}

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.

AS3 > Access of undefined property loader

I think I have al the imports required to display this image however I'm getting this error:
1120: Access of undefined property slideImage.
1120: Access of undefined property loader.
1180: Call to a possibly undefined method addChild.
1120: Access of undefined property loader.
My action script is inside a class named play.as
This is the AS3 code:
package {
import flash.display.MovieClip;
import flash.display.Bitmap;
import flash.net.URLRequest;
import flash.display.Loader;
public class play extends MovieClip {
var loader:Loader = new Loader();
loader.load(new URLRequest("img/Layer_1.jpg"));
addChild(loader);
public function play() {
// constructor code
}
}
}
this code'd be enough:
package {
import flash.display.MovieClip;
import flash.net.URLRequest;
import flash.display.Loader;
public class play extends MovieClip {
private var loader:Loader = new Loader();
public function play() {
loader.load(new URLRequest("img/Layer_1.jpg"));
addChild(loader);
}
}
}