as3 how to use a URLLoader in a class - actionscript-3

Is it possible to use a URLLoader in a class?
I am trying to use it in a class. Code is taken from: http://www.republicofcode.com/tutorials/flash/as3externaltext/
And here is my class I am trying to use it in but I get this error:
C:\Users\com\defaultVars.as, Line 36 1046: Type was not found or was not a compile-time constant: Event.**
package com {
import flash.display.Stage;
import flash.*;
public class defaultVars
{
// loader
public var myTextLoader:URLLoader = new URLLoader();
public function defaultVars()
{
myTextLoader.addEventListener(Event.COMPLETE, onLoaded);
myTextLoader.load(new URLRequest("myText.txt"));
}
public function sampleFunction()
{
// trace("not used");
}
/// ERROR LINE right below. But even if I fix this I get more errors.
function onLoaded(e:Event):void {
trace(e.target.data);
}
////////////////////////
}
}

try adding this to the top of your file.
import flash.events.*
import flash.net.*;

Related

actionscript 3 - Error #2136 - simple issue

This is extremely basic, but to help me understand could someone please explain why this doesn't work. Trying to call a function from one as file to another, and get the following error.
Error: Error #2136: The SWF file file:///test/Main.swf contains invalid data.
at code::Main()[C:\Users\Luke\Desktop\test\code\Main.as:12]
Error opening URL 'file:///test/Main.swf'
Main.as
package code {
import flash.display.MovieClip;
import flash.events.*;
import code.Enemy;
public class Main extends MovieClip
{
public function Main()
{
var enemy:Enemy = new Enemy();
}
public function test():void
{
trace("Test");
}
}
}
Enemy.as
package code {
import flash.display.MovieClip;
import flash.events.*;
import code.Main;
public class Enemy extends Main {
public function Enemy() {
var main:Main = new Main();
main.test();
}
}
}
Assuming Main is your document class, you can't instantiate it. That might explain the SWF invalid data error.
What it looks like you are trying to do is access a function on Main from your Enemy. To do that you just need a reference to Main from inside your Enemy class. If you add the Enemy instance to the display list you can probably use root or parent to get a reference to Main. You could also pass a reference to Main through the constructor of your Enemy class:
public class Main {
public function Main() {
new Enemy(this);
}
public function test():void {
trace("test");
}
}
public class Enemy {
public function Enemy(main:Main) {
main.test();
}
}
From the constructor of the class Main you are creating the Object of Enemy. In the constructor of Enemy you are creating the Object of Main. Hence it continues to create those two objects until there is Stack overflow. It never reaches to the line where you have main.test();
if you wana get data frome main.as you can use the static var.
package {
import flash.display.MovieClip;
public class Main extends MovieClip {
// i well get this var in my Enemy as.
public var i:uint=1021;
public function txtuto() {
// constructor code
}
}
}`
// the Enemy.as
`package {
import flash.display.MovieClip;
public class Enemy extends MovieClip {
public static var tx:Main = new Main;
public function Enemy() {
trace(tx.i);
}
}
}
good luck.

Error 1046: Type was not found or was not a compile-time constant: Program

I'm trying to link my .as file to my Flash program. Below is the code from my .as file:
package com.project {
import flash.display.Sprite;
import flash.events.*;
public class Program extends Sprite{
public var value:Number;
private var max:Number;
private var min:Number;
function draggable()
{
min = bar_mc.y;
max = bar_mc.height - Erhu_H3_btn.height;
Erhu_H3_btn.addEventListener(MouseEvent.MOUSE_DOWN, dragHandle);
}
function dragHandle(event:MouseEvent):void
{
Erhu_H3_btn.startDrag(false, new Rectangle(0,min,0,max));
stage.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
}
function stopDragging(event:MouseEvent):void
{
Erhu_H3_btn.stopDrag();
stage.removeEventListener(MouseEvent.MOUSE_UP, stopDragging);
}
}
}
Below is the code in my Flash file:
import com.script.Script;
stop();
var Program:Program = new Program();
Can someone please tell me what I'm doing wrong? I keep getting error 1046! Thanks! :)
You probably missed importing statement
import com.project.Program

Error #1010 as3

How do I fix this Error #1010: : A term is undefined and has no properties. at Main/showIntro() at Main().
The code that I used is from a online tutorial.
package {
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.MovieClip;
public class Main extends MovieClip {
private var intro:Introduction;
public function Main() {
intro = new Introduction();
showIntro();
}
private function showIntro():void {
//add intro
addChild(intro);
//add eventlistener
intro.begin_btn.addEventListener(MouseEvent.CLICK, clickBegin);
intro.x = stage.stageWidth/2;
intro.y = stage.stageHeight/2;
}
private function clickBegin(e:MouseEvent):void {
trace("0");
}
}
}
}
I'm pretty sure this is because begin_btn isn't defined in the Introduction class when you instantiate it.
Try adding a definition in the class like so:
public var begin_btn:MovieClip;
If you have defined it, check that it is a publicly accessible

What is the null-object error with this AS3 code for loading external swf?

I am getting a null object error when I add the mouse event listener for the log in button. (Look at the comments in the constructor)
I am using Flash CS6, and objects such as logInbutton and screen_log_in are instance names from the .fla file. This here is the .as file.
Error I get is:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at actions::indexPage()
My AS3 code:
package actions
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.events.IEventDispatcher;
import flash.net.URLRequest;
import flash.display.Loader;
import fl.motion.MotionEvent;
import flash.events.MouseEvent;
public class indexPage extends MovieClip
{
public function indexPage():void
{
loadSWF("http://mathlympics.cu.cc/loginsystem.swf");
//THIS IS THE LINE WHICH IS CAUSING THE ERROR
//WHEN I COMMENT IT OUT THE ERROR IS GONE
logInButton.addEventListener(MouseEvent.CLICK, goToLogIn);
}
var _swfLoader:Loader;
var _swfContent:MovieClip;
public function loadSWF(path:String):void
{
var _req:URLRequest = new URLRequest();
_req.url = path;
_swfLoader = new Loader();
setupListeners(_swfLoader.contentLoaderInfo);
_swfLoader.load(_req);
}
function setupListeners(dispatcher:IEventDispatcher):void
{
dispatcher.addEventListener(Event.COMPLETE, addSWF);
}
function addSWF(event:Event):void
{
event.target.removeEventListener(Event.COMPLETE, addSWF);
event.target.removeEventListener(ProgressEvent.PROGRESS, preloadSWF);
_swfContent = event.target.content;
screen_log_in.addChild(_swfContent);
}
function unloadSwf():void
{
_swfLoader.unloadAndStop();
screen_log_in.removeChild(_swfContent);
_swfContent = null;
}
function goToLogIn(e:MouseEvent):void
{
unloadSwf();
screen_log_in.loadSWF("http://mathlympics.cu.cc/loginsystem.swf");
}
function goToRegister(e:MouseEvent):void
{
unloadSwf();
screen_log_in.loadSWF("http://mathlympics.cu.cc/register.swf");
}
}
}
You can not access stage until stage is available.
public function indexPage():void
{
addEventListener(Event.ADDED_TO_STAGE,init)
}
public function init(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE,init)
loadSWF("http://mathlympics.cu.cc/loginsystem.swf");
//THIS IS THE LINE WHICH IS CAUSING THE ERROR
//WHEN I COMMENT IT OUT THE ERROR IS GONE
logInButton.addEventListener(MouseEvent.CLICK, goToLogIn);
}
I am just going to answer my question for the future visitors. The cause of problem I have note been able to figure out but what I have figured out is how to get around it. I simply copy pasted my code from document class to the frames and it works absolutely fine there.

addEventListener ENTER_FRAME

when I try to make a addEventListener I get an error:
Line 20 1046: Type was not found or was not a compile-time constant: Event.
package player {
import flash.media.Sound;
import flash.net.URLRequest;
public class Stream {
private var _Sound = null;
private var _Channel = null;
function Stream(){
this._Sound = new Sound();
}
public function play(url){
this._Sound.load(new URLRequest(url));
this._Channel = this._Sound.play();
this.addEventListener(Event.ENTER_FRAME, this.myFunction);
}
private function myFunction(e:Event){
}
}
}
import flash.events.Event; goes at the top under package player {.
You need to import the event before using it.
Update:
package player {
import flash.events.Event;
import flash.media.Sound;
import flash.net.URLRequest;
import flash.display.Sprite;
public class Stream extends Sprite {
private var _Sound = null;
private var _Channel = null;
public function Stream(){
this._Sound = new Sound();
}
public function play(url){
this._Sound.load(new URLRequest(url));
this._Channel = this._Sound.play();
this.addEventListener(Event.ENTER_FRAME, this.myFunction);
}
private function myFunction(e:Event){
}
}
}
Use this code. Generally, you want to add an ENTER_FRAME event to a display object. The Sprite class is a display object. I'm making it a Sprite by using the extends keyword. Please note that you need to import the class you're extending, as I've done.
The instruction:
this.addEventListener(Event.ENTER_FRAME, this.myFunction);
uses this to self-reference the player instance, but this.myFunction is redundant since myFunction is already a method belonging to the player instance.
Instead use:
this.addEventListener(Event.ENTER_FRAME, myFunction);