Error: Call to a possibly undefined method - actionscript-3

I have the simplest code ever. Main class:
package
{
import field.Field;
import flash.display.Sprite;
import flash.events.Event;
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
var field:Field = new Field();
addChild(field);
field.test();
}
}
}
and a Field class:
package field
{
import flash.display.Sprite;
public class Field extends Sprite
{
public function Field()
{
super();
}
public function test():void
{
}
}
}
test method is presented.
But when I try to compile I get this:
Main.as(26): col: 10 Error: Call to a possibly undefined method test.
field.test();
How could this be happening?

field is your package, that's why you can not do field.test(). So you have to choose another name of your Field instance. You can do like this :
var _field:Field = new Field();
addChild(_field);
_field.test();
Hope that can help.

Related

FlashDevelop Errors in Sample program

I'm a beginner to this program, the code is
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.text.TextField;
/**
*...
* #author FXDS
*/
public class Main extends Sprite
{
public function Main():Void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry
var greeting:TextField = new TextField();
}
greeting:text = ("Hello World");
greeting.x = 100;
greeting.y = 100;
addChild(greeting);
}
}
However, when I run it, the program complains with the following error :
\src\Main.as(23): col: 17 Error: A constructor cannot specify a return type.
Any Help would be greatly appreciated.
You get that error because you have a little mistake in your code :
public function Main():Void { }
should be
public function Main():void { }
the compiler didn't know Void (with a capital V) which is different from void.
and to avoid that mistake in the future, you can simply write :
public function Main() { }
Hope that can help.

ADDED_TO_STAGE function never called

I have this class which should take a button from stage (xBtn).
package com.stx.utils {
import flash.display.MovieClip;
import flash.events.*;
public class STXbutonx extends MovieClip {
private var xBtn : MovieClip;
public function STXbutonx() {
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event=null) : void {
trace("int!"); //this is never called
xBtn = stage.getChildByName('ics') as MovieClip;
xBtn.addEventListener(MouseEvent.CLICK, onX);
removeEventListener(Event.ADDED_TO_STAGE, init);
}
private function onX(e:MouseEvent) : void {
trace("x Clicked!");
}
}
}
and in Document class I called like this :
import flash.display.MovieClip;
import com.stx.utils.*;
public class Main extends MovieClip {
var xx : STXbutonx;
public function Main() {
xx = new STXbutonx();
}
}
Why my init function is never called?
Thank you!
Because you never add it to the stage.
Change your Document class to
public function Main() {
xx = new STXbutonx();
addChild(xx);
}
Main holds the reference to the stage, adding a child to Main will add the child to the stage. Thus the event listener in STXbutonx will fire.
xBtn = stage.getChildByName('ics') as MovieClip; // now I have acces of undefined property stage...
You don't have access to the stage because STXButon is not on the stage, it is not an DisplayObject. To get around this, do this:
package com.stx.utils {
import flash.display.MovieClip;
import flash.events.*;
public class STXbutonx{
private var xBtn : MovieClip;
private var stage : Stage;
public function STXbutonx(stage:Stage) {
this.stage = stage;
init();
}
private function init() : void {
trace("int!");
xBtn = stage.getChildByName('ics') as MovieClip;
xBtn.addEventListener(MouseEvent.CLICK, onX);
removeEventListener(Event.ADDED_TO_STAGE, init);
}
private function onX(e:MouseEvent) : void {
trace("x Clicked!");
}
}
}
And of course
import flash.display.MovieClip;
import com.stx.utils.*;
public class Main extends MovieClip {
var xx : STXbutonx;
public function Main() {
xx = new STXbutonx(stage);
}
}

How can I dispatch event to another class in Action Script 3?

I've searched few docs and I couldn't find about dispatching event to another classes.
I'm trying to make like this.
'main' class has button.
'main','sub' class gonna trace "button is clicked" when button is clicked
I can trace that in 'main' class with dispatch event.
but 'sub' class is problem.
how can I dispatch event like that into 'sub' class?
Main class
package com
{
import flash.events.*;
import flash.display.MovieClip;
import com.sub;
public class main extends MovieClip
{
public static const BTN_CLICKED:String = "btn_Clicked";
public function main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
public function init(e:Event = null):void
{
var flashVars:Object = {};
removeEventListener(Event.ADDED_TO_STAGE, init);
if(parent != null && parent.parent != null)
{
flashVars = parent. parent.loaderInfo.parameters;
}
else
{
flashVars = this.root.loaderInfo.parameters;
}
//entry point
var subClass:sub = new sub;
subClass.init();
btn.addEventListener(MouseEvent.CLICK, onClick);
addEventListener(BTN_CLICKED, onbtnClicked, false, 0, true);
}
public function onClick(e:MouseEvent)
{
dispatchEvent(new Event(BTN_CLICKED));
}
public function onbtnClicked(e:Event)
{
trace("clicked");
}
}
}
Sub Class
package com
{
import flash.events.*;
import flash.display.MovieClip;
import com.main;
public class sub extends MovieClip
{
public function sub():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
public function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
//entry point
trace("sub class loaded");
}
}
}
You should relocate var subClass:sub to the main set of main class definitions, beside public static const BTN_CLICKED:String = "btn_Clicked";. Then, you can do subClass.dispatchEvent(...).
public class main extends MovieClip {
private var subClass:sub;
public function main() {
...
subClass=new sub();
addChild(subClass); // not just "init()", it's wrong
...
}
public function onClick(e:MouseEvent)
{
dispatchEvent(new Event(BTN_CLICKED));
subClass.dispatchEvent(new Event(BTN_CLICKED));
}
}

ActionScript 3.0 - Dispatch event class to class

I'm working in Action Script 3.0 and I have a question in DispatchEvent class.
following code is standalone class.
I want to dispatch event to 'main' class and 'sub' class when event occured.
I'm stuck on this issue. please help me.
package com
{
import flash.events.*;
import flash.display.MovieClip;
import com.sub;
public class main extends MovieClip
{
public static const BTN_CLICKED:String = "btn_Clicked";
public function main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
public function init(e:Event = null):void
{
var flashVars:Object = {};
removeEventListener(Event.ADDED_TO_STAGE, init);
if(parent != null && parent.parent != null)
{
flashVars = parent. parent.loaderInfo.parameters;
}
else
{
flashVars = this.root.loaderInfo.parameters;
}
//entry point
var subClass:sub = new sub;
subClass.init();
btn.addEventListener(MouseEvent.CLICK, onClick);
addEventListener(BTN_CLICKED, onbtnClicked, false, 0, true);
}
public function onClick(e:MouseEvent)
{
dispatchEvent(new Event(BTN_CLICKED));
}
public function onbtnClicked(e:Event)
{
trace("clicked");
}
}
}
and below is 'sub' class.
package com
{
import flash.events.*;
import flash.display.MovieClip;
import com.main;
public class sub extends MovieClip
{
public function sub():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
public function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
//entry point
trace("sub class loaded");
}
}
}
yes, there's nothing in 'sub' class... how can I get dispatch event in sub class?

#2015: Invalid BitmapData

In my class, if I create bitmapData like this:
private var tImage:BitmapData;
public function object():void {
tImage = new BitmapData(30,30,false,0x000000);
}
I get the following error:
ArgumentError: Error #2015: Invalid BitmapData.
But if I declare the variable inside the method:
public function object():void {
var tImage:BitmapData;
tImage = new BitmapData(30,30,false,0x000000);
}
It works fine. WHY!?!?! It's driving me crazy.
Thanks guys!
I think it might be some other code in your class.
The following works, but I didn't name the function "object" (since I'm guessing that's a reserved word??)
package
{
/**
* ...
* #author your name here
*/
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Bitmap;
public class TestBitmap extends MovieClip
{
private var tImage:BitmapData;
public function TestBitmap():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
tImage = new BitmapData(30,30,false,0x000000);
}
}
}
This simplified version below also works too:
package
{
/**
* ...
* #author your name here
*/
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Bitmap;
public class TestBitmap extends MovieClip
{
private var tImage:BitmapData;
public function TestBitmap():void
{
tImage = new BitmapData(30,30,false,0x000000);
}
}
}
You declared tImage as private...
private var tImage:BitmapData;
public function object():void {
tImage = new BitmapData(30,30,false,0x000000);
}
Its should be
var tImage:BitmapData;
public function object():void {
tImage = new BitmapData(30,30,false,0x000000);
}
Derp