as3 accessing objects and vars on main timeline from class - actionscript-3

I am trying to access vars, functions and objects hard coded on the main timeline from a class. The Objects, vars etc... are loaded when I call a function in the class like this:
Some code from main timeline:
import com.beauMoves;
var bm = new beauMoves();
bm.thisWorks();
and below is the class. But it is not accessing the main timeline. In this case I am trying to access a display object loaded from the lib and place on the timeline. The object is called "Beau" as you can see in the code below.
package com {
import flash.display.MovieClip;
import com.*;
public class beauMoves extends MovieClip
{
public function beauMoves()
{
// constructor code
trace("BeauMoves");
}
public function thisWorks()
{
trace("Cool Beans! This one worked");
// THESE TWO LINES BELOW ARE NOT WORKING
var main:MovieClip = MovieClip(this.parent);
main.Beau.alpha = .3;
}
}
}

Presuming the two lines below are on the main timeline, pass to beauMoves() this as a constructor argument:
import com.beauMoves;
var bm = new beauMoves( this );
Then in your beauMoves() class:
package com {
import flash.display.MovieClip;
import com.*;
public class beauMoves extends MovieClip
{
private var _mainTimeline:MovieClip;
public function beauMoves( mainTimeLine:MovieClip )
{
// constructor code
trace("BeauMoves");
_mainTimeline = mainTimeLine;
}
public function thisWorks()
{
trace("Cool Beans! This one worked");
_mainTimeline.bm.alpha = .3;
}
}
}

Related

How do I run the constructor code of another .as file from the main.as class file attached to the .fla file?

I have a main class file called 'Main' which is linked to the main .fla file. Here is the code currently in this file:
package {
import flash.display.MovieClip;
import CharacterPkg.Character;
public class Main extends MovieClip {
public function Main() {
var newCharacter:Character = new Character;
Character();
}
}
}
Then I have a file which I want to use to create a character on the main stage in the .fla file. Here is the code for that file:
package CharacterPkg{
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Stage;
public class Character extends MovieClip {
public function Character() {
trace("This is running");
}
}
}
I want the constructor code which is in the Character.as file to run when I run the .swf. But I just get a 1136 Error: Incorrect number of arguments. Expected 1.
I'm at wits ends and have spent two days trying to figure this out. I'm fairly new to this and probably just making some stupid error but I can't figure this out. Any help would be greatly appreciated.
Try as:
package
{
import flash.display.MovieClip;
import CharacterPkg.Character;
public class Main extends MovieClip
{
public function Main()
{
//Character(); //is not needed (won't work).
var newCharacter :Character = new Character();
addChild( newCharacter ); //this way auto-runs the constructor function code.
//newCharacter.someFunction(); //example to run any other functions within newCharacter class.
}
}
}
When you addChild a class, you automatically run the constructor function of that specific class.
To run any other functions within that other class, just use path like instanceName.xxx(); where your instanceName is newCharacter and xxx represents a function name, close it with a ();
Edit:
To solve second issue, make sure:
1) You have linked Main.as to the FLA properly.
deselect any item(s) by clicking an empty part of Stage, then press ctrl+F3
Is Main.as listed as Class (under the "Publish" tab)?
2) Code for Main.as :
package
{
import flash.display.MovieClip;
import CharacterPkg.Character;
public class Main extends MovieClip
{
public var newCharacter :Character;
public function Main()
{
newCharacter = new Character();
addChild( newCharacter ); //this way auto-runs the constructor function...
newCharacter.secondFunction(); //example to run any other functions in newCharacter class..
newCharacter.thirdFunction(); //another test
}
}
}
3) Code for Character.as (inside a folder named CharacterPkg) :
package CharacterPkg
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Stage;
public class Character extends MovieClip
{
public function Character() //constructor function
{
trace("This is running");
}
public function secondFunction() :void
{
trace("This is second Function");
}
public function thirdFunction() :void
{
trace("This is third Function");
}
} //end class
}//end package

Main class was not the first class which was called in ActionScript 3.0

I have a weird problem in a game which I want to create. At first I have created a project without external classes.
On the root I have three Characters and one Level. Also there is a script for the key listeners and I have eventListeners to register the level, levelElements, coins and the characters. Then I have a CharacterControl MovieClip in the library. This MovieClip contains the character behaviour. As example walk, jump, idle, gravity if not colliding to the ground. There are also different events and eventListeners.
The scripts are on the timeline. If I call in both timelines a trace-function, the root was called before the CharacterController.
After that in my next exercise I created a document class Main. Now there are all root scripts. And for the CharacterController I also copied the timeline code and put it into an external class.
Now my problem is that the CharacterController class is called before the main class gets called. This leads to the problem that the eventListener and events can't get called in right order. There are happening a few errors. No Coin and no Character collides on the ground or a plattform. Everything is falling down.
How can I achieve that the Main gets called at first? Should I remove the characters and create them by script?
EDIT:
Ok, I give a short example which shows the basic problem without the complex code of my game.
package {
import flash.display.MovieClip;
public class Main extends MovieClip {
public function Main() {
trace("main was called");
}
}
}
package {
import flash.display.MovieClip;
public class My_Circle extends MovieClip {
public function My_Circle() {
// constructor code
trace("circle was called");
}
}
}
Here are some pictures of the configuration and structure of my project:
I need Main called as first. I think it's a basic problem in as3.
You'd make the class file of your stage Main.as in the properties pane.
Edit: Interesting. Just replicated this. I believe then that flash/air constructs elements so they're ready to be put on the stage instead of constructing the stage first and elements after. You should put the code you want to execute for your circle in some sort of init function and execute it in.
package
{
import flash.display.MovieClip;
public class Main extends MovieClip
{
public function Main()
{
super();
trace("Hello");
(circle_mc as Circle).init();
}
}
}
Circle:
package
{
import flash.display.MovieClip;
public class Circle extends MovieClip
{
public function Circle()
{
super();
}
public function init():void
{
trace("World");
}
}
}
ok, I figured out a simple solution how you can make it by code. At first I think it's a better solution to create the object (circle, character, whatever) by code.
The timeline code:
import flash.events.Event;
Main.setStageRef(this.stage); //Super-important
stop();
The Main class code:
package {
import flash.display.MovieClip;
import flash.display.Stage;
import flash.display.DisplayObject;
import flash.events.Event;
public class Main extends MovieClip {
public static var stageRef : Object = null;
var movieClipExample : My_Circle;
public function Main() {
this.addEventListener(Event.ENTER_FRAME,this.afterMainTimeLine);
stage.addEventListener("myEvent", myEventFunction);
}
public function afterMainTimeLine(e:Event) {
trace("Hello");
movieClipExample = new My_Circle;
movieClipExample.init();
stageRef.addChild(movieClipExample);
removeEventListener(Event.ENTER_FRAME, this.afterMainTimeLine);
this.addEventListener(Event.ENTER_FRAME,this.updateFunction);
}
public function updateFunction(e:Event){
movieClipExample.moveFunction();
}
public function myEventFunction(_event: Event) {
trace("myEvent was triggered");
}
//Getter/setter for stage
public static function setStageRef(_stage : Object) : void
{
stageRef = _stage;
}
public static function getStageRef() : Object
{
return stageRef;
}
}
}
The Object code (as example My_Circle):
package {
import flash.display.MovieClip;
import flash.display.Stage;
import flash.display.DisplayObject;
public class My_Circle extends MovieClip {
public function My_Circle()
{
stageRef = Main.getStageRef();
trace("World");
this.x = x;
this.y = y;
var evt = new Event("myEvent", true);
stageRef.dispatchEvent(evt);
}
public function init():void
{
trace("Created Circle");
this.x = 300;
this.y = 100;
}
function moveFunction(){
this.y += 1;
}
}
}
The output is following:
Hello
World
myEvent was triggered
Created Circle
Maybe this could be helpful for others.
Just one thing. For different objects from the same class it's better to use an array. The position (maybe) should be random.

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.

access public var of an instance from a different object in actionscript3

how do you access property from one class instance to another, assuming both MCButtonA and MCButtonB instances are defined in the main class. This is without using static var.
I keep on getting:
1120: Access of undefined property varA
package {
import flash.display.MovieClip;
public class MCButtonA extends MovieClip {
public var varA:String = "abc";
public function MCButtonA() {
// constructor code
}
}
}
package {
import flash.display.MovieClip;
public class MCButtonB extends MovieClip {
public var varB:String = "abc";
public function MCButtonB() {
// constructor code
trace( ?????varA)
}
}
}
Main class:
var aButton:MCButtonA = new MCButtonA();
var bButton:MCButtonB = new MCButtonB();
trace(this.parent.aButton.varA);
upd
will work only after both buttons are added to stage