AS3 Run gotoAndStop from a class - actionscript-3

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

Related

How to pass variables from .fla file to .as file in as3

I have a .fla file names test.fla and I have this variable in it:
import Main;
var my_var;
stage.addEventListener(MouseEvent.CLICK, onLoaded);
function onLoaded(e:Event):void
{
my_var = "Maziar";
//trace(my_var);
}
I have a .as file called Main.as.
I want to pass my_var from test.fla to the Main.as.
I will really appreciate, if you can help me in this matter!
It is noticeable that I have used the method mentioned in "Actionscript 3 : pass a variable from the main fla to external as file", but it does not work for me!!!
I wrote in my Main.as:
package
{
import flash.display.Sprite;
import flash.geom.Point;
import flash.events.MouseEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.Event;
public class Main extends Sprite
{
public function Main()
{
if (stage)
{
init();
}
else
{
addEventListener(Event.ADDED_TO_STAGE, init);
}
addEventListener(Event.ENTER_FRAME, waitForMyVar);
}
private function waitForMyVar(e:Event):void
{
if (my_var != null)
{
trace(my_var);
removeEventListener(Event.ENTER_FRAME, waitForMyVar);
}
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
}
...
}
}
Thanks in advance!
It's important to note that the constructor Main in your ActionScript document file is run before the code found within the frame. When you are attempting to access the my_var variable in your AS document it has not yet been declared in the frame.
So, we need to wait for Flash to run the frame. This can be done using an Event.ENTER_FRAME listener.
Example:
Timeline Code (.fla file)
var my_var:String = "my variable";
Document Code (.as file)
package {
import flash.display.MovieClip;
import flash.events.Event;
public class Main extends MovieClip {
public function Main() {
addEventListener(Event.ENTER_FRAME, waitForMyVar);
}
private function waitForMyVar(e:Event):void {
trace(my_var);
removeEventListener(Event.ENTER_FRAME, waitForMyVar);
}
}
As a side note, it appears my_var is not assigned a value until the user has clicked the stage. An adjustment could be made in the waitForMyVar function to wait for my_var to be non-null.
Example:
if(my_var != null) {
trace(my_var);
removeEventListener(Event.ENTER_FRAME, waitForMyVar);
}
Hope this helps!
Use static class members.
public class Main extends Sprite
{
static public var globalVar:* = 1;
public function doWhatever():void
{
trace(globalVar);
}
}
Then in FLA:
import Main;
var M:Main = new Main();
// or use sprite instance of Main
M.doWhatever();
Main.globalVar = "Hello World!";
M.doWhatever();

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.

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.

Action Script 3 - Full screen from class

please note that I am a novice when it comes to Actionscript 3 and lot of what I could do with reasonable competency in AS2 I now can't in AS3, to my frustration! OK, I'm fleshing out a simple drag drop and decorate application in Flash. I'm wanting to use the external action script class/package to allow for it full screen from my desktop and I'm in a tangle, with constructor errors being thrown up and all sorts. Could anyone give any pointers?
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.display.Stage;
import flash.display.StageDisplayState;
public class fullmode extends MovieClip {
public function fullmode() {
fullbtn.addEventListener(MouseEvent.CLICK, fullScreen);
}// btn declared - - - - - - - -
//public function fullmode(event:MouseEvent):void {
stage.displayState=StageDisplayState.FULL_SCREEN;
}
}
//--------------------- drag item
public class DragDrop extends MovieClip {
public function DragDrop() {
dragme.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
dragme.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
}
private function mouseDownHandler(evt:MouseEvent):void {
var obj = evt.target;
obj.startDrag();
}
private function mouseUpHandler(evt:MouseEvent):void {
var obj = evt.target;
obj.stopDrag();
}
}
}
Thanks world!
You had a few syntax errors/typos, I've fixed them below:
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.display.Stage;
import flash.display.StageDisplayState;
public class Fullmode extends MovieClip
{
public function Fullmode()
{
fullbtn.addEventListener(MouseEvent.CLICK, fullScreen);
}
private function fullScreen(event:MouseEvent):void
{
stage.displayState = StageDisplayState.FULL_SCREEN;
}
}
}
Standard practice dictates that your class names should be capitalized, hence Fullmode instead of fullmode.
Additionally, you had named your MouseEvent.CLICK listener the same as your class, instead of what you intended to name it.

Add other class as child in Flash Professional

Using Flash Professional CS5, I'm trying to add a child object in my script. I want to give the class which creates the child-object as parameter while creating. The problem is when I try to test the project, I get an error stating Incorrect number of arguments. 0 expected.
My MainClass.as:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class MainClass extends MovieClip {
var menuClass:MenuClass;
var gameClass:GameClass;
var highClass:HighscoreClass;
public function Main() {
this.StartOfProject();
}
public function StartOfProject() {
menuClass = new MenuClass(this);
this.addChild(menuClass);
highClass = new HighscoreClass();
}
And my MenuClass.as:
package {
public class MenuClass extends MovieClip {
var mainClass:MainClass;
public function Menu(mainClass:MainClass) {
this.mainClass = mainClass;
...
}
What am I doing wrong?
You named the constructor of your MenuClass incorrectly. It should be "MenuClass" not "Menu"
change:
public function Main() {
this.StartOfProject();
}
to:
public function MainClass() {
this.StartOfProject();
}
and:
public function Menu(mainClass:MainClass)
to: public function MenuClass (mainClass:MainClass)
and see if this already solves your problem