Actionscript3 setting time delay - actionscript-3

trying to make a time delay before redirection to a specific web page , I got many errors during the compiling process , sorry new to actionscript :
package
{
import flash.display.*;
import flash.net.*;
import flash.system.*;
import flash.utils.*;
import flash.events.*;
public class test extends flash.display.Sprite
{
public function test()
{
super();
flash.net.navigateToURL(new flash.net.URLRequest("http://youpassed-theexam.com/congrats"), "_self");
return;
}
}
setInterval(test,5000);
}

A couple of issues with your code:
Constructors of classes are immediately called once the class is
instantiated. You should create a separate method and call that with
a delay from within your constructor.
setInterval would fire repeatedly after every set interval. You
should rather use setTimeout.
Classes should have a Sentence caps naming convention, so Test and not test. Just a best practice. Nothing wrong syntactically.
Constructors do not return anything so we do not need the return statement.
Once you have imported a class, you do not need to write the full name of the class to access it's methods.
Try to avoid * based import statements. It does tend to import a lot more classes than just the required class. Again, just a best practice.
So your code should look something like this below:
package
{
import flash.display.Sprite;
import flash.net.URLRequest;
import flash.net.navigateToURL;
import flash.utils.setInterval;
import flash.utils.setTimeout;
public class Test extends flash.display.Sprite
{
public function Test()
{
super();
setTimeout(gotoURL, 5000);
}
protected function gotoURL():void
{
navigateToURL(new URLRequest("http://youpassed-theexam.com/congrats"), "_self");
}
}
}
Hope this helps. Cheers.

Related

AS3 addEventListener not a recognized method in my custom class

I want to have a Class called Commands that I can put different key presses and functions into that will control game states and variables for quick and easy game testing. I'm trying this, but it's not working...
package gameTesting {
import flash.events.KeyboardEvent;
import flash.events.*;
import flash.ui.Keyboard;
import flash.display.*;
import flash.events.EventDispatcher;
public class Commands {
public function Commands() {
addEventListeners();
}
public function addEventListeners():void{
addEventListener(KeyboardEvent.KEY_DOWN,keyDown);
}
public function keyDown(ke:KeyboardEvent):void{
trace("key pressed");
}
}
}
which throws this error:
C:...\Commands.as, Line 15, Column 4 1180: Call to a possibly undefined method addEventListener.
So, I tried having my class extend something that inherits the EventDispatcher methods:
//...
public class Commands extends DisplayObject{
// ...
but I just get this error thrown from my main .as file when trying to instantiate this Class:
ArgumentError: Error #2012: Commands$ class cannot be instantiated.
I also tried throwing the static keyword around just for lols, but no dice.
What am I missing here?
by the way, my reason for doing things this way is just so that I can remove this functionality (so users can't use it) by simply removing the line of code that instantiates this class. I think it's pretty nifty, but if this seems asinine, by all means speak up!
Try to pass stage to Commands,so you can add addEventListener on stage.
import flash.display.Stage;
public class Commands {
public function Commands(stage:Stage) {
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDown);
}
public function keyDown(ke:KeyboardEvent):void{
trace("key pressed");
}
}

How to access parent or wrapper variables from an AS3 Worker Class

I am using Flash Builder 4.7 and have created a Worker Class. Below is the code:
package co.fuix.mobile.system.model
{
import flash.display.DisplayObjectContainer;
import flash.display.Sprite;
import flash.events.Event;
import flash.system.MessageChannel;
import flash.system.Worker;
import flash.utils.getDefinitionByName;
import mx.managers.SystemManager;
public class InstantMessengerWorker extends Sprite
{
private var bm:MessageChannel;
private var mb:MessageChannel;
public function InstantMessengerWorker()
{
super();
bm = Worker.current.getSharedProperty("BACK_TO_MAIN_CHANNEL");
mb = Worker.current.getSharedProperty("MAIN_TO_BACK_CHANNEL");
mb.addEventListener(Event.CHANNEL_MESSAGE, onMainToBack);
}
protected function onMainToBack(event:Event):void
{
if(mb.messageAvailable){
var s:SystemManager;
trace('*'+mb.receive());
trace('**'+mb.receive());
trace('***'+mb.receive());
trace(mx.core.FlexGlobals.topLevelApplication.myVariable);
}
}
}
}
How do I reference a variable in the main mxml file. I know how to use message channels but I want to get that variable straight.
When I run the above code, this part
trace(mx.core.FlexGlobals.topLevelApplication.myVariable);
is giving me an error.
Any help will be regreatly appreciated
You can't access a variable from the main app like that. They are running separately. What you need to do is:
Link to Adobe docs

In ActionScript 3, is conditional if/else compilation of class variables valid?

I'm working with an AS3 class in Flash Builder 4.6, AIR 3.5.
Can anyone confirm that the following if/else syntax is valid in the class definition. I've tested it and it compiles and runs on Mac, but I can't find documentation that supports it.
package controller
{
import flash.desktop.NativeApplication;
import flash.display.NativeMenu;
import flash.display.NativeMenuItem;
import flash.display.NativeWindow;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;
[Bindable]
public class AppMenu extends EventDispatcher
{
if (Capabilities.os.search("Mac")>-1) {
public var titularMenu:NativeMenu;
public var fileMenu:NativeMenu;
public var editMenu:NativeMenu;
private var optionsMenu:NativeMenu;
private var addOnsMenu:NativeMenu;
private var helpMenu:NativeMenu;
} else {
// Assign same variables as NativeMenuItem, for Windows.
}
// etcetera
}
No, you can't have if/else conditionals outside methods or initializers.
For your purpose, you could juste use define the generic properties/methods inside an interface IAppMenu, and then have one implementation per OS (of course, you will still need to define which one to create using Capabilities.os).

AS3 call to a possibly undefined method through a refernce with a static type?

I have been away from actionscript for a long time and not 100% why this is happening, I will simplify the class below:
package{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
public class OpiaRobot extends MovieClip{
public function OpiaRobot(){ }
public function botAction(e:MouseEvent):void{ }
public function run(e:Event):void{
this.botaction();
}
}
var opiaBot:OpiaRobot = new OpiaRobot();
The call to botaction causes:
call to a possibly undefined method through a refernce with a static type?
Why as it should be an instance? Any help is appreciated.
If that is your exact code, it's probably because you try to use botaction() when you should use botAction(MouseEvent).

Why put import statements in package?

In AS3, why do we put our import statements in the package statement, and not inside the Class declaration?
This is a typical (but rather pointless) AS3 class:
package my.foo.package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;
public class FooClass extends MovieClip
{
public static const EVENT_TIME_UP:String = 'timeUpEvent';
public function FooClass()
{
var timer:Timer = new Timer(2000, 1);
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.start();
}
private function timerHandler(e:TimerEvent):void
{
dispatchEvent(new Event(FooClass.EVENT_TIME_UP));
}
}
}
But why are all the import statements meant to go all the way up there, outside on the Class? The class works perfectly fine when I move the imports to inside the class declaration like below.
package my.foo.package
{
import flash.display.MovieClip;
public class FooClass extends MovieClip
{
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;
public static const EVENT_TIME_UP:String = 'timeUpEvent';
public function FooClass()
{
var timer:Timer = new Timer(2000, 1);
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.start();
}
private function timerHandler(e:TimerEvent):void
{
dispatchEvent(new Event(FooClass.EVENT_TIME_UP));
}
}
}
It's only the MovieClip import that needs to be in package, because my class is extending it.
There is nothing helpful about this in the Adobe AS3 coding conventions.
So why do we put the imports in the package and not the class that's actually using them. The package is not using the imported classes is it? And why does it still work if I move the imports into the Class?
This is simply a convention that's in use so there is one central place to view all of the imports. Let's say we take your logic to the next extreme and place an import statement on the line just before every variable we declare (which compiles just fine by the way). We'd wind up needlessly duplicating import statements whether on purpose or by accident. Also, someone wanting to see what dependencies a given file has would have to scan the whole file rather than looking at just the top of the file. Your point of putting the imports within the class declaration isn't as crazy, but you'd still wind up with the problem of imports scattered throughout a file if your file declares more than one class (as in the case of internally used classes, for example).