Error #1010 as3 - actionscript-3

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

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.

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 of undefined property through static type

I got an error : access of possibly undefined property destinationY through a reference with static type Gold. I don't use the destinationY property in Gold class. Only in main class.
I'm still amateur at classes and defining properties for a object itself. Please help. Thanks!
Here is the Main class:
package
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.Event;
public class Main extends MovieClip
{
private var field:Array;
public var gold:Gold;
public var goldContainer:Sprite = new Sprite();
private var goldTimer:Timer = new Timer(2000);
public function Main():void
{
setupField();
goldSet();
addEventListener(Event.ENTER_FRAME,onEnterFrm);
}
private function goldSet():void
{ addChild(goldContainer);
goldTimer.start();
goldTimer.addEventListener(TimerEvent.TIMER, newGold);
}
private function newGold(e:TimerEvent):void {
var goldRow:int=Math.floor(Math.random()*5);
var goldCol:int=Math.floor(Math.random()*9);
gold = new Gold(this);
gold.buttonMode=true;
goldContainer.addChild(gold);
gold.x=30+goldCol*65;
gold.destinationY = 35+goldRow*75;
gold.y=-2
}
private function onEnterFrm(e:Event):void {
for (var i:uint=0; i<goldContainer.numChildren; i++) {
var fallingGold:Gold=goldContainer.getChildAt(i) as Gold;
if (fallingGold.y<fallingGold.destinationY) {
fallingGold.y++;
} else {
fallingGold.alpha-=0.01;
if (fallingGold.alpha<0) {
goldContainer.removeChild(fallingGold);
}
}
}
}
}
}
Here is the Gold class :
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Sprite
import flash.events.MouseEvent
public class Gold extends MovieClip
{
private var _main:Main
private var gold:Gold;
public function Gold(main:Main):void
{
_main=main;
addEventListener(MouseEvent.CLICK, goldClicked);
}
private function goldClicked(e:MouseEvent):void
{
e.currentTarget.removeEventListener(MouseEvent.CLICK,goldClicked);
_main.goldContainer.removeChild(e.currentTarget as Gold);
}
}
}
From first glance, you don't have a destinationY property in your Gold Class. MovieClip does not have this property either, so you're trying to manipulate a variable that doesn't exist.
Unless this isn't the whole Class you're showing us, of course.
To elaborate a bit
I don't use the destinationY property in Gold class. Only in main
class.
Yes, but the you're trying to manipulate a property called destinationY in your Gold Class from the main class.
If you do this:
public class Gold extends MovieClip
{
private var _main:Main
private var gold:Gold;
public var destinationY:int = 0;
...
}
you shouldn't get this error anymore, but i doubt this will solve all your problems.
On line xx of the Main class, you have the code:
gold.destinationY = 35+goldRow*75;
You're trying to access the "destinationY" property of the Gold class, but the Gold class doesn't have a public property named destinationY. To add it, just add this line to the Gold class:
public var destinationY;

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

Dynamically change text field from sub class Flash AS3?

I'm trying to dynamically change the text on the main stage from a sub class, but I cannot figure out how to do it. I can change the field text from the main class just fine by using myTextArea.text = "Blarg", but I'm stumped for doing it from a sub class and google hasn't helped.
My application structure is similar to:
//Main class file
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.display.Stage;
public class Main extends Sprite {
static public var mainStage:Stage;
public function Main() {
if (stage) stageLoader();
else addEventListener(Event.ADDED_TO_STAGE, stageLoader);
}
private function stageLoader() {
mainStage = this.stage;
removeEventListener(Event.ADDED_TO_STAGE, stageLoader);
//This is working just fine
myTextArea.text = "Blarg";
}
}
}
//Sub class
package {
import flash.display.Sprite;
public class OtherClass extends Sprite {
public function OtherClass() {
//This throws "Access of undefined property myTextArea" error
myTextArea.text = "Blarg";
}
}
}
I'm sure the solution is simple, but I just can't wrap my head around it and I would love your help!
When I create classes that need a link to the main timeline, I'll pass the scope as an argument in the constructor. Something like this:
package {
import flash.display.Sprite;
public class OtherClass extends Sprite {
public function OtherClass(_path) {
//This throws "Access of undefined property myTextArea" error
_path.myTextArea.text = "Blarg";
}
}
}
Then from your main class:
var _otherClass = new OtherClass(this);