Select a extern class to start with - actionscript-3

my problem is pretty hard to describe and to google for <_< so ill give it a try here.
ihave my main.as, char.as, enemy.as, classes
my main was the stage of course.... it worked pretty well, but now that my main is extern too, it wont be called anymore...
it calls: char, enemy(which is a sub class of char) and then the empty stage but it never calls my main.as which should be called first...
also it never calls any constructors...how can i tell flash to start with my main.as?
i hope you got it, the code is probably to much to post here :P

If get you right.. you have to put super() calls to extended classes.
So for example in enemy class constructor:
public class enemy extends char {
function enemy() {
super(); // calls "char" class constructor
}
}
and if you have params in your constructors:
public class enemy extends char {
function enemy(param1: String, param2:String) {
super(param1, param2); // calls "char" class constructor
}
}
Overriding methods:
override public function doSome():void {
super.doSome(); // call parent class
}
and to make flash start with your main class - select your FLA file stage in flash (professional) and from the right side panel (properties) set Class to your main class (for example: com.myApp.Main).

Related

Call to a possibly undefined method '' through a reference with static type Class

I made small .fla file in Flash Professional, and I have added .as (ActionScript File) in Flash Professional, and I have added something like code below to .as (ActionScript file), but the error appears and I am trying to figure it out, but can't, so I decided to post it in here instead.
package
{
import flash.display.MovieClip;
public class Bag extends MovieClip
{
static var firstBag:String;
public static function set setFirstBag(value:String):void
{
firstBag = value;
}
public static function get getFirstBag():String
{
return firstBag;
}
}
}
and I called it like this:
button1.addEventListener(MouseEvent.CLICK, onClickFirstButton);
function onClickFirstButton(e:MouseEvent):void
{
Bag.setFirstBag("First slot in the bag has been filled up!");
}
But I have received this following error:
Call to a possibly undefined method setFirstBag through a reference
with static type Class.
What could I do wrong?
The .as file and .fla file are on the same folder.
if I changed the Bag class to static. The error will be like this:
The static attribute may be used only on definitions inside a class.
Your answer much appreciated!
Thank you!
You're useing get like it is a mettod, but thay are accessors for properties so intead of:
Bag.setFirstBag("First slot in the bag has been filled up!");
use
Bag.setFirstBag ="First slot in the bag has been filled up!";
A few additional thoughts...
While syntactically valid, the definition and naming of your getter and setter is confusing and atypical, which I think contributed to your confusion about the behavior. You've actually defined two separate properties, one is write-only ("setFirstBag") and one is read-only ("getFirstBag"). Usually you define a getter/setter as the same property (ex "firstBag"), and without any "get" or "set" in the property name, since that is what the getter/setter is defining for you. Example:
private static var _firstBag:String;
public static function get firstBag():String {
return _firstBag:
}
public static function set firstBag(value:String):void {
_firstBag = value;
}
// usage
Bag.firstBag = "stuff";
trace(Bag.firstBag); // "stuff"
Also, you may very well have a good reason to use a getter/setter here, or you might just prefer it, but from the code you posted you could just define a public static var to do the same thing. (If you did, refactoring into a getter/setter if you needed some side-effect logic would be trivial, since the public API remains the same.)

Error when overriding constructor of extended class

I found a quite strange problem while making two classes in AS3. Let's call them ParentClass and ChildClass. In order to make both of them you need a Sprite object, then the ParentClass makes it visible in the stage. ChildClass inherits the ParentClass, too.
ParentClass.as:
package myStudio.basic {
import flash.display.MovieClip;
import flash.display.Sprite;
public dynamic class ParentClass extends MovieClip {
public function ParentClass(mc:Sprite=null) {
addChild(mc);
}
}
}
ChildClass.as:
package myStudio.containers {
import myStudio.basic.ParentClass;
import flash.display.MovieClip;
import flash.display.Sprite;
public class ChildClass extends ParentClass {
public function ChildClass(mc:Sprite=null) {
addChild(mc);
}
}
}
Then, I write this code on Frame 1, Layer Actions of the FLA file:
var mc:MovieClip = new childMC;
var vig:ChildClass = new ChildClass(mc);
addChild(vig);
However, I got run-time error #2007:
TypeError: Error #2007: The value of the parameter child must not be null.
at flash.display::DisplayObjectContainer/addChild()
at myStudio.basic::ParentClass()
at myStudio.containers::ChildClass()
at myStudioComicAnimator_fla::MainTimeline/frame1()
I tried overriding the ChildClass constructor function, but it still doesn't work.
So here's my question: Is there another workaround to solve this problem?
The reason for that is that you are not calling super. You can check what's happening in the error stack (down to top):
you instantiate ChildClass, and you pass the previously created childMC to the constructor
ChildClass extends ParentClass, so when instantiated it always calls the constructor
the constructor of ParentClass tries to add something as a child
The problem is that you cannot add null as a child. But because the constructor is called internally, there is no param that is being passed to it. so mc variable is always null. But as we said - null cannot be added.
Use the super by yourself:
public function ChildClass(mc:Sprite=null) {
super(mc);
}
This way the ParentClass will get reference to the mc object and will be able to add it.
Another option is not to use addChild in the ParentClass, but only in ChildClass. Then it doesn't matter if you pass anything to super, or even if you are calling super at all.
Edit: I forgot to say that this is not a bug, but a standard behavior and works exactly like it should work. The reason for this is that each class can have a whole different override of the constructor. It can take more or less parameters, so the chain for calling parent's constructor is your job to handle.

Call custom method as Display Object AS3

I have a class which extends MovieClip. This class has an update() function which needs to be called every new frame with the deltaTime in the arguments. This works if the class has been declared but not if it has just been added to the display list.
Code in the main class:
package packageFoo{
import flash.display.MovieClip;
import packageFoo.customMovieclip;
public class Main extends MovieClip{
public function Main():void{
var testMc:customMovieClip = new customMovieClip();
addChild(testMc);
testMc.update(dt);
}
}
}
This outputs the correct values where as if I just added it without referencing it:
package packageFoo{
import flash.display.MovieClip;
import packageFoo.customMovieclip;
public class Main extends MovieClip{
public function Main():void{
addChild(new customMovieclip());
this.getChildAt(0).update(dt);
}
}
}
This makes the compile time error: 1061: Call to a possibly undefined method update through a reference with static type flash.display:DisplayObject.
I can't really reference the 'customMovieclip's because I am wanting multiple ones.
It looks like this.getChildAt(0) is not customMovieClip. This can arise if your Main has pre-places components at design time. To check, do trace(this.numChildren) as the first line of Main() constructor. And also, to address any subclass methods properly, you need to typecast your DisplayObject returned by getChildAt() to a proper type.
(this.getChildAt(0) as customMovieClip).update(dt);
Still, using a class-wide variable is better if you want to address that custom MC in more than one function of main class.
If you're trying to avoid a reference to the custom class in the document class, you can call it like this:
this.getChildAt(0)["update"](dt);

Override function in CS5 IDE -> Timeline -> Framescript

I have a baseclasse which implements an interface. I use this baseclass as my "template" ( read: Semantics, i'm not talking about Java/C++ Templates).
In my Flash CS5 IDE I want to override these interface methods.
Yes they are implemented in the base class, but trying to override them in framescript throws me( YES this might probably not be a clean design):
Symbol 'GameTest', Layer 'Layer 1', Frame 1, Line 1 1024:
Overriding a function that is not marked for override.
I don't exactly know in what scope framescripts work. And by framscript I just mean in the timeline frame 1.
in my base class:
public class MiniGameTemplate extends MovieClip implements IMiniGame
{
public function MiniGameTemplate()
{
}
public function update():void
{
}
}
In my Library object's first frame:
override function update():void
{
}
I'm using actionscript linkage to inherit my library object from the base class.
If I clear the framescript, it runs fine. No error.
When overriding a method, you must structure the overriding method exactly like the original. In this case you've missed the public access modifier statement.
Solution:
override public function update():void {
}

How to reference objects in AS3?

I've learned one way to do that, but I want to improve my knowledge. For simplicity I'm not going to use import neither extends in the code below.
1
public class Main
{
public function Main()
{
new MyCustomObject(stage);
}
}
2
public class MyCustomObject
{
public var referenceStage:Stage = new Stage();
public function MyCustomObject(xxx:Stage)
{
this.referenceStage = xxx;
referenceStage.addChild(this);
}
}
I've learned it reading a tutorial over internet, but I want to know where I can find more samples on how to reference objects in AS3. For future codes, I want to add hitTest and the like.
Thanks !
The best place is the ActionScript 3 Reference from Adobe: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/index.html
Here is the specific section on objects: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Object.html
if you absolutely want to pass a stage reference through an argument to a constructor, you can do so about how you have it laid out (although get rid of the new Stage() call, which won't do anything).
that said, .stage is a property available to all display objects that are in the display list (meaning: the have been added via addChild or addChildAt).
you're probably getting that error trying to reference a .stage property of an object before it's been added to the display list. this is a common error, and can be handled by waiting to reference the .stage property until it has been added, usually using addEventListener(Event.ADDED_TO_STAGE...
so instead of
public class MyObject extends Sprite {
public function MyObject():void{
this.x = this.stage.stageWidth/2;
}
}
you'd use something like this
public class MyObject extends Sprite {
public function MyObject():void{
this.addEventListener(Event.ADDED_TO_STAGE, this.addedHandler, false, 0, true);
}
private function addedHandler(e:Event):void{
this.x = this.stage.stageWidth/2;
}
}
HTH
In your example, you don't need do call new Stage() in your CustomObject
public var referenceStage:Stage;
is enough
A hitting function may be found here http://troygilbert.com/2007/06/pixel-perfect-collision-detection-in-actionscript3/
Possible solutions are:
Instead of passing the stage object, you can also pass the main object and calling functions in the main object for the custom object
Maintain an array in the MainObject with which you want do collisions test.
Implementing an Interface (extend an object) with a function which do the hit test agains the array in the MainObject (for example went the EntreFrame Event is fired)
Custom Events are the solution for communicating with the main object loosely
Passing a reference to an object in the constructor is a classic OOP pattern