AS3 > Access of undefined property loader - actionscript-3

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

Related

Error #1009: Cannot access a property or method of a null object ref AS3 project

I have AS3 project and Im trying to create a way to display GIF or SWF (converted online from the gif) in a similar way that I display a single image.
A single image is displayed with class and then referenced in Main.as. Similarly for gif or swf I extend MovieClip:
(yes I read other threads did not help)
package com.mee.mytest
{
import flash.display.Bitmap;
import flash.events.Event;
import flash.display.MovieClip;
/**
* ...
* #author Mee
*/
public class MyTest extends MovieClip
{
[Embed(source="../../../../assets/spfx_MyClip.swf", mimeType="application/octet-stream")]
private static const cMyTest : Class;
private var swfMyClip : MovieClip;
public function MyTest()
{
swfMyClip = new cMyTest() as MovieClip;
swfMyClip.scaleX = 600;
swfMyClip.scaleY = 400;
addChild(swfMyClip);
}
}
}
And now my main:
import com.mee.mytest.MyTest
import flash.desktop.NativeApplication;
import flash.display.Bitmap;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
public class Main extends Sprite
{
public var vMyTest : MyTest; //this is var = the class
public function Main()
{
stage.align = StageAlign.TOP_LEFT;
stage.addEventListener(Event.DEACTIVATE, deactivate);
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.setAspectRatio(StageAspectRatio.LANDSCAPE);
// touch or gesture? BLAH BLAH
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
testMyGif();
}
function testmyGif():void
{
vMyTest = new MyTest();
addChild(vMyTest);
}
ERROR ERROR ERROR
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at com.mee.mytest::MyTest()
at Main/testmyGif()
at Main()
dem ... just for any1's sake. It mostly comes from 'Your displayed SWF is supposed to be in the same folder as the built swf, then the url has new URLRequest("myfile.swf") forget other folders or ..exiting folders, this is the only way to avoid any other URL errors
using
public var loader : Loader = new Loader();
public var urlRequ : URLRequest = new URLRequest("MyFile.swf")
loader.load(urlRequ);
addChild(loader);

1120: Access of undefined property stage

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.

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

Creating a rectangle with stage sizes

This is my first class and i try to make rectangle with stage sizes, but flash gives me these errors:
addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
// 1180: Call to a possibly undefined method addEventListener.
removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
// 1180: Call to a possibly undefined method removeEventListener.
stageW = stage.stageWidth;
// 1120: Access of undefined property stage.
stageH = stage.stageHeight;
// 1120: Access of undefined property stage.
addChild(mc_background);
// 1180: Call to a possibly undefined method addChild.
My code is:
package {
import flash.display.MovieClip;
import flash.events.Event;
public class main {
var mc_background:MovieClip = new MovieClip();
var stageW:Number;
var stageH:Number;
public function init() {
addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
}
private function addedToStageHandler(evn:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
stageW = stage.stageWidth;
stageH = stage.stageHeight;
drawBackground();
}
private function drawBackground():void {
mc_background.beginFill(0xFF00CC);
mc_background.graphics.drawRect(0,0,stageW,stageH);
mc_background.graphics.endFill();
addChild(mc_background);
}
}
}
Your class "main" should extend a Sprite to use the addChild() and removeEventListener() methods.
So you should import the Sprite class and extends your class from Sprite, like so:
package {
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
public class main extends Sprite
And it's also considered a nice practice to call class names with first capital letter, e.g main > Main. All lower-case are usually variables, so it will confuse you later.

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.