AS3 Class Error - actionscript-3

I am trying to make a game in flash using AS3. I am making a class file that set the parameters for creating a new planet, but when I then add the actions to the first frame of the timeline, it immediately gives me an error. I am new to action script and would love any help. My code should work as far as I know. Here it is:
The actions menu contains this code on the first frame -
var zuun:MovieClip = new Planet();
The class "Planet" looks like this, and is saved in a file name "Planet.as" and is targeted at my main file.
package {
import flash.display.MovieClip;
public class Planet extends MovieClip {
private var planetname:String;
public function Planet() {
// constructor code
}
public function setName(a:String):void {
planetname = a;
}
public function getName():String {
return planetname;
}
}
}
EDIT
The error i'm getting is this -
Error: Error #2136: The SWF file file:///Users/mike/Desktop/jonah/bindings.swf contains invalid data.
at Planet/frame1()[Planet::frame1:1]
Thanks

Related

Flash AS3 - Trying to access public function from another class throws error?

This is my Main.as:
package {
import flash.display.MovieClip;
public class Main extends MovieClip {
public function Main() {
// Code here
}
public function myFunc() {
trace('!!!!');
}
}
}
When I try accessing it from another class using the following code, Flash throws me Error #2136:
package {
import flash.display.MovieClip;
import Main;
public class MyClass extends MovieClip {
public var m:Main;
public function MyClass() {
m = new Main();
m.myFunc();
}
}
}
EDIT: One more thing. The second class is attached to a MovieClip and exported on the first frame. I thought it made no difference, but someone in the comments have told me it does and apparently that's what's causing the error in the first place. In that case, how can I access the public function from a class attached to a MC?
Next time post the error message itself, most of us do not have Flash errors memorized by id. ;)
From the docs:
2136 The SWF file %1 contains invalid data.
This rings a bell for me. Your Main class is probably your document class. The document class is a special class that cannot be instantiated.
To access properties and methods of the document class instance from other code, you simply need a reference to the document class instance.
There are many ways you could get a reference, as it is really just a code dependency design question. Two common, easy solutions are:
1. Use the root property of any display object that is added as a child of the document class instance. Since the root property is typed to DisplayObject you need to cast to your document class to access its methods and property, for example: Main(root).myFunc().
2. Use the singleton pattern and assign a static public reference to the document class instance:
public class Main {
public static var main:Main;
public function Main() {
main = this;
}
public function myFunc():void { }
}
// usage:
Main.main.myFunc();

actionscript 3 - error #1009 issues calling function from another class

Im having trouble calling functions from other classes. I want to call a function in one class which updates a score display in another class. The error code for this is:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at code.functions::EnemyYellow()[code\functions\EnemyYellow.as:18]
at code::Main()[\code\Main.as:27]
Would appreciate if someone could help me out, I set up 2 basic files with the code which is causing an issue. Its normally not set up like this, I just made this for testing and so I can clearly explain the problem.
Main file:
package code {
import flash.display.MovieClip;
import flash.events.*;
import code.*;
public class Main extends MovieClip {
public var _enemy:EnemyYellow;
public var playerHP:Number;
public function Main() {
playerHP = 10;
_playerHPdisplay.text = playerHP.toString();
trace(playerHP)
_enemy = new EnemyYellow;
}
public function lowerHP ():void
{
playerHP = playerHP - 1;
_playerHPdisplay.text = playerHP.toString();
trace(playerHP)
}
}
}
Second File:
package code.functions {
import flash.display.MovieClip;
import flash.events.*;
import code.Main;
public class EnemyYellow extends MovieClip {
public var _main:Main;
public function EnemyYellow() {
_main.lowerHP();
trace ("done")
}
}
}
I also tried adding _main = new Main; in the second file but the game just loads with a blackscreen and an error about invalid data.
First of all, you surely need to "instantiate" the Main class, which means basically to create it.
public var _main:Main;
This line just declares that there will be a variable of type Main. But for now, the value of _main is null. So you are right, that you need to call:
_main = new Main();
After you've done this, the first error will disappear. But then you have things in that MovieClip that are still vague. Like _playerHPdisplay. Where's that from? Is it an instance from stage or what? You just created a brand new object, and it does not have any reference to other objects, TextFields or whatever.
So this basically answers your current question and problem, but for sure you will have more :)

how to work with button in class and packages at AS3

I am a beginner of action script. working with button in action script class file not working.
i have created two file one is stream.as and another is main.as
main.as is the main class file of my frame.
i have drawed a button and converted it in button and gave instance name play_btn.
but the compiler giving me 1120: access of undefined property play_btn.
the both codes are given below;
main.as
package {
import flash.display.MovieClip;
import stream.stream;
public class main extends stream {
public function main() {
}
// constructor code
}
}
stream.as
package {
import flash.events.MouseEvent;
import flash.display.MovieClip;
public class stream extends MovieClip {
public function main() {
play_btn.addEventListener(MouseEvent.CLICK, pausevedio);
function pausevedio(event:MouseEvent):void{
play_btn.visible=false;
}
// constructor code
}
}
}
Correct me if I'm wrong but it's because the play_btn belongs solely to your main class and you're trying to access it through the stream class, to do this properly try to instantiate it through code in the class you wish to use it in instead of on the timeline like so:
playBtn: play_btn = new play_btn();
playBtn.x = x;
playBtn.y = y;
addChild(playBtn);
This is my best guess I'm not exactly sure how your classes are structured but this might be your issue. I hope this helps (I'm new too but I figured my two-cents might help!)!
~Cheers!

Actionscript 3.0: Save data from an fla file into an .as file

Can anyone help me how to save changes of an object from an fla file into a .as file? Say I have an integer which will increment by one if a button is clicked and let's say I clicked it 5 times which would make it's value from zero to five...How can I send this data into my .as file? I already looked on the internet but I got no clear answers...I'm using actionscript 3.
If you want to 'send' data to an .as file give it a property and, from your Main file, set the property of an instance of that Class, eg:
package {
import flash.display.MovieClip;
// other imports as necessary
public class Tracker extends MovieClip {
private var _numIncrements:int = 0;
public function Tracker() {
}
public function set numIncrements(p_value:int):void {
_numIncrements = p_value
}
}
}
Then, in the variable list of your Main class:
private var _tracker:Tracker = new Tracker();
And in the Main CLICK handler:
_tracker.numIncrements ++;
You should be handling the click and count functionality from your .as file.
Don't start putting code inside your Library Objects. Its a very bad practice
something like this:
package {
//imports
public class Main extends Sprite
{
private var clickCount:int = 0;
public function Main()
{
yourButton.addEventListener(MouseEvent.CLICK, clickHandler);
}
function clickHandler(event_object:MouseEvent) {
clickCount++; // increments by 1
}
}
}

How AS3 class files.as work together with FLA file?

I have two questions: Can i write app using just *.as files, and then compile them somehow in SWF? (i am making myself a webpage now)
Secondly, please look at the code, my problem is the same - I can't render text field onto stage, to be visible.
Flash say's 'undefined method addChild.' and 'Access of undefined property tekstuKaste through a reference with static type Class.'
This is a constructor type class inic which kinda serves for initialization, cos all i do is, I make an instance OF THIS class in FLA file by ActionScript, and expect, all application to work.
package {
import pacinas.visuals.*;
import pacinas.visuals.AE_kanva;
public class inic {
public function inic() {
trace("===========");
trace("inicializēt un izsaukt visu no Kanvas klases");
trace("===========");
trace(" ");
var kanvas:AE_kanva = new AE_kanva();
trace(" ");
kanvas.varis();
trace(" ");
trace("===========");
trace("inicializēt un izsaukt visu no Lauki klases");
trace("===========");
trace(" ");
var laukiTxt:BE_tekstaLaukiPrimitive = new BE_tekstaLaukiPrimitive();
trace("");
laukiTxt.simpleText();
addChild(BE_tekstaLaukiPrimitive.tekstuKaste);
}
}
}
There is another EXTERNAL CLASS By whom i hoped to place a rectangles - that does not work too. Example:
package pacinas.visuals
{
import flash.display.Sprite;
public class AE_kanva extends Sprite
{
public function AE_kanva()
{
var kvad:Shape = new Shape();
kvad.graphics.beginFill(0xFF0000);
kvad.graphics.drawRect(0, 0, 100,100);
kvad.graphics.endFill();
addChild(kvad);
trace("konstruktors - zīmē kanvu");
}
public function varis()
{
trace("te glabaas variaabljus");
var ff:int = 4;
var dd:int = 8;
}
}
}
And here is class i hoped will make text box for me (to fill it with XML later)
package pacinas.visuals
{
import flash.text.*;
import flash.display.Sprite;
public class BE_tekstaLaukiPrimitive extends Sprite
{
public var tekstuKaste:TextField = new TextField();
private var kontinents:String = new String ("XML SATURU CMON! a123");
public function BE_tekstaLaukiPrimitive():void
{
trace("teksta rāmis = konstruktora klase");
addChild(tekstuKaste); <--CAN'T GET THIS TO WORK!!!
tekstuKaste.text = kontinents;
}
public function simpleText()
{
trace("nekonstruktora f-cija no Teksta lauki");
}
}}
p.s. I do not use document Class. Ok I will if it's needed. But how?
Can I write app using just *.as files, and then compile them somehow into a SWF?
Yes - using the Flex SDK you can write pure ActionScript and compile it down into a working SWF. FlashDevelop is a good IDE that takes advantage of this.
You will however need to understand how the document class works.
Flash says undefined method addChild. and Access of undefined property tekstuKaste through a reference with static type Class.
In your code, this line is causing your issue:
addChild(BE_tekstaLaukiPrimitive.tekstuKaste);
The first error undefined method addChild is because your class inic does not extend a class that implements the method addChild(). DisplayObjectContainer defines this method, so you'll want to extend that as a minimum, like this:
public class inic extends DisplayObjectContainer
The second error is because you're attempting to access a property of the class BE_tekstaLaukiPrimitive as if it were static. I suspect what you actually wanted to do was this:
addChild(laukiTxt); // laukiTxt is the instance you created.