I have a URL link at the end of a Flash 5.5 file that does not work. I have the following code in AS3. I do not get an error message when I save the SWF.
import flash.events.*;
import flash.display.*;
//import flash.ui.Keyboard;
var weblinkURL:String = "http://www.optiosolutions.com";
weblink_btn.addEventListener(MouseEvent.CLICK, webLink);
function webLink(e:Event):void {
var request:URLRequest = new URLRequest(weblinkURL);
try {
navigateToURL(request, '_blank');
} catch (e:Error) {
trace("Error occurred!");
}
}
I pasted your code in a new Flash project, and this works as it should! Nothing seems to be wrong... So I would advice you to look at your other code...
You are sure that the button id weblink_btn is the correct button id of the button you suppose it to be?
One small tip:
import flash.events.*;
import flash.display.*;
It's not really needed to import with a *, since you are working on a KeyFrame, these imports can be forgotten.
In general you should never import with *, but only the classes that are needed.
Related
I am receiving an error to suggest that my URL is not found. I am running the below simple script from Flash Builder. As I understand the application launches from bin-debug directory within the main project folder.
I have a image folder also within the main project folder yet it will not find the image. It works when I type the full directory.
The code I have is:
package
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.net.URLRequest;
[SWF(width="550", height="400", backgroundColor="#FFFFFF", frameRate="60")]
public class Kitties extends Sprite
{
//Declare the variables for the background.
public var backgroundURL: URLRequest;
public var backgroundLoader: Loader;
public var background: Sprite;
public function Kitties()
{
backgroundURL = new URLRequest();
backgroundLoader = new Loader();
background = new Sprite();
backgroundURL.url = "..\\images\\background.png";
backgroundLoader.load(backgroundURL);
background.addChild(backgroundLoader);
stage.addChild(background);
}
}
}
I understand I need to add an event listener to capture this error but for now it's not necessary as i know the file exists. Has anyone experienced this before?
Thank you.
You shouldn't use backslashes in actionscript3. Normal slashes work on every OS, even on Windows.
Try this:
backgroundURL.url = "../images/background.png";
If the file exists, it should load correctly.
I'm trying to load an image from the internet with AS3 following this tutorial. When I try to compile the application I get the following error:
Call a possibly undefined method load through a reference with static type Loader.
my_loader.load(where, loaderContext);
^
Here is the code I'm using:
package {
import flash.system.ApplicationDomain;
import flash.system.SecurityDomain;
import flash.net.URLRequest;
import flash.system.LoaderContext;
import flash.display.Loader;
import flash.events.*;
import flash.external.ExternalInterface;
import flash.display.Sprite;
public class Loader extends Sprite {
public function Loader() {
var where:URLRequest = new URLRequest("image_from_web.png");
var loaderContext:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain, null);
var my_loader:Loader = new Loader();
my_loader.load(where, loaderContext);
addChild(my_loader);
}
}
}
In this page compilerErrors(ErrorCode = 1061) says that this occurs when I try to call a method that does not exist.
I'm using Ubuntu 14.10 and compiling with ProjectSprout that uses the flex compiler.
Your issue is namespace clashing (ambiguous class names). The class you've posted is called Loader, yet you try to import another Loader class. AS3 doesn't know what you refer to when you reference Loader now. So it is looking for a load method on your custom Loader class (which doesn't exist).
To resolve the issue, either rename your custom class to something less ambiguous (MyImageLoader maybe, or whatever) - or use the fully qualified class path when referring to the display package Loader. eg.
var my_loader:flash.display.Loader = new flash.display.Loader();
I loaded a test swf into a class. The swf has its own code that loads a link when clicked. This works fine but I want to UNLOAD the class when the swf is clicked.
How can I unload a class when clicking on the loaded content?
I called the function "meow", a simply random name, as I am testing anyway. :)
Here is what I have that does not work:
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Loader;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.net.URLRequest;
import flash.events.MouseEvent;
public class theAd extends MovieClip
{
public function theAd()
{
var my_loader:Loader = new Loader();
my_loader.load(new URLRequest("http://www.massmediamail.com/testing/Manga.swf"));
my_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, startListener);
function startListener(e:Event):void
{
addChild(my_loader.content);
trace("Content Loaded");
my_loader.content.addEventListener(MouseEvent.CLICK, meow);
}
function meow(e:Event):void
{
//this.parent.parent.removeChild(this);
trace(this.name);
}
}
}
}
Are you having troubles with correct removal of loader.content? You should employ event.target property to get the correct instance.
function meow(e:MouseEvent):void {
var mc:DisplayObject=e.target;
if (!mc) return;
mc.parent.removeChild(mc);
}
I'm trying to load in a remote SWF and access it's methods and properties, using an interface. (There's a similar question here that got as far as "that's weird!" but didn't resolve it: Loading swf and using it through interface)
My remote SWF looks like this:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.system.Security;
import IMultiplayer;
[SWF(width="238", height="60", frameRate="60", backgroundColor="#FFFFFF")]
public class Main extends Sprite implements IMultiplayer
{
public function init(e:Event):void
{
}
public function TEST():void
{
trace("TEST()");
}
}
}
I then have an interface that looks like this:
package
{
import flash.events.*;
import flash.display.*;
public interface IMultiplayer
{
function init(e:Event):void;
function TEST():void;
}
}
And finally, I've got a loader class that pulls down the SWF and tries to cast it as the same interface that the remote SWF implements. EDIT - apologies for length, was asked to post the full source:
package uk.co.MyDomain
{
import flash.display.DisplayObject;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.ErrorEvent;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.TimerEvent;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;
import flash.system.Security;
import flash.system.SecurityDomain;
import flash.utils.Timer;
import uk.co.MyDomain.*;
import utils.Console;
public class MultiplayerLoader extends Sprite
{
private var ld:Loader;
private var _environment:String;
public var _mpInstance:IMultiplayer;
private const SANDBOX_SWF:String = "http://static.sandbox.dev.MyDomain.co.uk/smartfoxtest/dev/swf/MP.swf";
public function MultiplayerLoader(environment:String)
{
_environment = environment;
}
public function loadMultiplayer():void
{
ld = new Loader();
var context:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
ld.contentLoaderInfo.addEventListener(Event.COMPLETE, multiplayerLoaded);
ld.contentLoaderInfo.addEventListener(ErrorEvent.ERROR, onLoadError);
ld.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIOLoadError);
ld.contentLoaderInfo.addEventListener(IOErrorEvent.DISK_ERROR, onDiskError);
ld.contentLoaderInfo.addEventListener(IOErrorEvent.NETWORK_ERROR, onNetworkError);
ld.contentLoaderInfo.addEventListener(IOErrorEvent.VERIFY_ERROR, onVerifyError);
if(Security.sandboxType == Security.REMOTE)
{
trace('[MP Loader] Loading with context');
ld.load(new URLRequest(SANDBOX_SWF), context);
}
else
{
trace('[MP Loader] Loading with NO context');
ld.load(new URLRequest(SANDBOX_SWF));
}
}
private function onIOLoadError(e:IOErrorEvent):void
{
notifyFailure('IOLoadError');
}
private function onDiskError(e:IOErrorEvent):void
{
notifyFailure('IOLoadError');
}
private function onNetworkError(e:IOErrorEvent):void
{
notifyFailure('IOLoadError');
}
private function onVerifyError(e:IOErrorEvent):void
{
notifyFailure('IOLoadError');
}
private function onLoadError(e:ErrorEvent):void
{
notifyFailure('IOLoadError');
}
private function multiplayerLoaded(e:Event):void
{
var tester:IMultiplayer = e.currentTarget.content as IMultiplayer;
Console.log('Loaded: ' + tester);
dispatchEvent(new MultiplayerEvent(MultiplayerEvent.SWF_LOAD_SUCCESS));
}
private function notifyFailure(reason:String):void
{
var failEvent:MultiplayerEvent = new MultiplayerEvent(MultiplayerEvent.SWF_LOAD_FAILURE);
failEvent.params = {'reason':reason}
dispatchEvent(failEvent);
}
}
}
Now, if I DON'T cast it to use the interface, I can trace it out successfully and call functions (so e.target.content.TEST() will fire). However, as soon as I cast it to the interface, it fails.
Any ideas?
EDIT
OK, I'm getting the same issue with a custom event class that's shared between both applications. Flash errors, saying it cannot convert from one class to the other - even though they're identical they're in different projects, and so I imagine different namespaces. I assumed that loading into the same applicationDomain would fix this, but it hasn't. Is there any other way I can get around this without resorting to a common library/SWC or similar?
I think this might be an issue of the application domain. The loaded SWF resides in its own application domain so it does not share the exact same interface. Try to load the swf into the »current application domain«, using the applicationDomainproperty of the Loader's LoaderInfo Object.
look here
good luck…
EDIT::
it has to be done in the loaders load method
var context:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
var loader:Loader = new Loader();
loader.load(new URLRequest("myButtons.swf"), context);
from here
EDIT 2::
Once I ran into an even more strange error, it was caused by the fact that i created an interface, compiled the »to load files« and »file that loaded«, changed the interface through adding a method and forgot to compile the »to load files« (there were a lot). Perhaps happened something like this…
EDIT 3::
If I remember right than one has to wait for the Event.INIT event of loaded swf's, first after this event the constructor of the loaded swf ran.
EDIT 4::
found this, perhaps you need to do:
var YourInterfaceClass:Class = ApplicationDomain.currentDomain.getDefinition( "YourInterface" ) as Class;
var myInstance:YourINterface = event.target.content as YourInterface;
I am a newbie in flash. I want to test passing variables to swf file using flashvars, then I had been creating a action script file with name "test_adver.as". I use this file in the fla file as class document. This is the code of "test_adver.as":
package src{
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.display.MovieClip;
import flash.events.Event;
import flash.external.ExternalInterface;
import flash.net.URLRequest;
import flash.text.TextField;
public class test_adver extends MovieClip {
public function test_adver() {
try {
var param:Object=loaderInfo(this.root.loaderInfo).parameters;
} catch (error:Error) {
trace("Loading failed");
}
}
}
}
But when I run the code, the output displays "1180: Call to a possibly undefined method loaderInfo action script error". I try debug an hours but cannot fix this issue. help me. Please! Sorry my English is not good.
It should be a lot more simple. You are trying to request loaderInfo property, but for this you don't even need type casting.
var param:Object=this.root.loaderInfo.parameters;
You did everything right except the below
var param:Object = LoaderInfo(this.root.loaderInfo).parameters;
Yes, there should be caps "L". It is enough to import LoaderInfo class for flashvars.