AS 3.0 Creating instances - actionscript-3

I started programming with Java, then I moved to as 3.0 to enhance my experience in UI.
Something I don't get in as 3.0 is the difference between MovieClip Object and instance.
To clarify, because I don't know if I have used the correct terminology:
The difference between: var name : ObjectName = new ClassName(); and the movie clip created on stage and give it an instance name.
I assume there are differences because I can use assign the movie clip's instance to the tween's object parameter, but cannot assign the one defined using variable.
I don't know if I am making any sense, but thank you in advance.

A MovieClip is one of three types of symbols available in Flash. Those three are MovieClip, Graphic, and Button. All MovieClip and Button symbols have the ability to have instance names set for them so that you can reference them from your ActionScript code. If you choose not to set an instance name for a Button or a MovieClip, Flash will automatically assign an instance name to it during runtime, regardless if you plan on referencing it from your ActionScript.
So for arguments sake, let's say you have a MovieClip on the stage with an instance name of "my_icon_mc". You could reference it in your code like so:
By calling the instance name itself, "my_icon_mc", or by storing a reference to it in a variable like so: (doing it this way has many advantages)
var myIcon:MovieClip = my_icon_mc;
In this example, I am storing a reference to a MovieClip on the stage with an instance name of "my_icon_mc" in the variable named myIcon. This allows me now to use the various MovieClip methods and properties of the MovieClip class in my code to manipulate the MovieClip on the stage.
So let's say I want to change the x coordinate of my movieclip on the stage to be at point 100, I could do the following:
my_icon_mc.x = 100
OR
var myIcon:MovieClip = my_icon_mc;
myIcon.x = 100;
It is important to note that if you create MovieClip via ActionScript, you can set the instance name of that MovieClip by using the name property of the MovieClip class like so:
var myIcon:MovieClip = new MovieClip();
myIcon.name = 'my_icon_mc';
Reference: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/MovieClip.html

Related

Should you explicitly declare an instance name in the code?

I am new to ActionScript.
When I create a MovieClip and attach an instance name to it, I have access to it via the class of the stage.
However, I saw some tutorials that explicitly declare a variable with the instance name.
Is there any particular reason for that?
There's a lot of confusion in your post, so let me clarify things for you, maybe.
You can instantiate (create an instance of) class with new operator. Yes, with the new operator you can omit () if there are no mandatory arguments, it's not an error.
new MovieClip;
You can assign the result of that operation to a variable, declared as the variable of exact class.
var M:MovieClip = new MovieClip;
Or any superclass of the class you instantiate.
var D:DisplayObject = new MovieClip;
var E:EventDispatcher = new MovieClip;
Or an untyped variable.
var U:* = new MovieClip;
The variable type is what compiler looks at when you work with the referenced object (with no regard to actual object's class):
// Fine, DisplayObject.name is a valid property.
D.name = "Movie1";
// Error, EventDispatcher does not have a name property.
E.name = "Movie2";
Then. Setting an instance name is meaningful if you are going to access that instance as a child of some container with getChildByName(...) method. Or, maybe, if you are going to have a lot of different children in one container and you want to store their meta-essence in their names, like "Enemy2" or "Bullet15".
But it is not mandatory. If you can/want organize your app so that all references are stored as variables or in Arrays, there's no need to name instances.

Having problems with hitTestObject-ing a child

I've just started to learn actionscript for a month or so, and I've been stuck on this part of the code for about 2 days now ><
So basically, I wanted to write a hitTestObject in a movieclip ("fire_mc") to detect if it overlaps the the child I added on stage (enemy)
So here's what I wrote in the main stage...
var newtarget:enemy=new enemy();
stage.addChild(newtarget);
newtarget.x=40;
newtarget.y=30;
and every time I traced the newtarget in the fire_mc, it turns out to be NULL...
So should I be tracing enemy or newtarget? (Cuz I tried both and nothing works).
And how can I hitTestObject in the fire_mc movieclip?
Is it possible for me to hitTestObject newtarget if I were to create multiple newtarget-s?
If not, what should I do?
And can someone tell me the difference between root, and MovieClip(root) (Because sometimes in a movieclip I have to use the former one to work, and sometimes the latter, and I have no idea why cuz I'm all referencing to the same place)
Sorry for these simple and lengthy questions, I'm really just a noob and I tried looking up for answers in the web for hours, but nothing helpful turned up><
Thanks in advance :)
In this statement
var newtarget:enemy=new enemy();
var - keyword used to define varibles, newtarget - variable name in which pointer to new class instance stored, :enemy - data type (the class name), new - keyword used to create new class instances, and finally enemy is class constructor (by the way there is a rule of good manners by which class names are capitalized)
So answer for you question which should you use when you want check is some is overlapping is 'newtarget'.
Now about hit test - all you need do to check if two objects hit each other is correctly use their references from the part of project where your code is writen.
For example if you have your fire_mc on MainTimeline created by IDE, and code of creation of you enemy instance stored in newtarget sameplace, then if you check will be placed in frame of MainTimeline where both object present it will be like this
fire_mc.hitTestObject(newtarget);
or this
newtarget.hitTestObject(fire_mc);
All these statements give you same result - if objects intersect each other you have true returned.
If you created 'newtarget' on MainTimeline and checks will be from fire_mc (which is on MainTimeline to) frame, then code will something like that
this.hitTestObject(MovieClip(root).newtarget);
Now about root. Primarily it is a property of DisplayObject in which reference to the top-most display object in swf-file structure is stored. So as such it is inherited by all child classes. So here's the trick. When you try get your objects in swf structure using root here the differences are based on the fact that root always return object as DisplayObject and that means what if you object contains something that DisplayObject shouldn't you can't get it. So you must convert returned reference to need data-type usign MovieClip(...).

Swapping specific movieclips after event?

I've created a drag and drop "puzzle" that has 16 unique pieces, each with their own instance name.
The problem I have is that there are 4 target zones made up of arrays (4 pieces per zone, but the order of the pieces in the zone does not matter). When a piece gets dropped into its correct zone, I would like to "switch" it with another movieclip and have that new movieclip be in the drop target area.
So, for each of the 16 puzzle pieces, I also have 16 unique companion pieces that need to somehow be paired up so that when the visible puzzle piece gets dropped, it is both removed from view, but also replaced with its companion piece.
Any ideas on how to do this?
MovieClip is a dynamic class, meaning that you can add properties to it at runtime. Leveraging this, you can assign a property to your original pieces and call it something like pairedPiece. In this property, you'll store the appropriate value (the name of its pair in the library).
var firstMovieClip:MovieClip;
// do whatever you need to set up your firstMovieClip, attach listeners, etc
firstMovieClip.pairedPiece = "SecondMovieClip";
// the following will occur when the piece is dropped and you need to swap it
var secondMovieClip:MovieClip = new (getDefinitionByName(firstMovieClip.pairedPiece) as Class)() as MovieClip;
secondMovieClip.x = firstMovieClip.x;
secondMovieClip.y = firstMovieClip.y;
firtMovieClip.parent.addChildAt(secondMovieClip, firstMovieClip.parent.getChildIndex(firstMovieClip));
firstMovieClip.parent.removeChild(firstMovieClip);

this[String] can't be added into a MovieClip

Lets say I have a MovieClip called "Box", and "String" is actually 'box' just that its not a MovieClip
The problem I'm facing now is I can't use something like circle.addChild(this[String])
I've tried tracing this[Strin]==Box and the result returns true.
And when I remove circle.addChild(this[String]), it does not add the Box into it.
BUT, when I traced is there any new object added to circle, the amount still remains the same.
Any idea what seems to be the problem here?
If I understood you, you are trying to add a MovieClip called (has a instance name of) String inside another MovieClip called Box, right?
Well, you can't give the name String to an object, because String is a class name in ActionScript 3.0
You can't name any object with any class name or protected keyword, such if, for, class, Boolean...
Those are words that ActionsScript uses globally (they are Top Level keywords).
Also, note that although you can name an object (give it an instance name of) MovieClip, Sprite, Loader, Stage and such, all theses names are used by ActionScript as class names, inside packages, that can be imported into your animation/application, and Flash will automatically import almost every one of them for you.
If you name your objects with those words you run some risks, like codes that don't work properly or don't compile at all...
As pointed by #Bosworth99 and #merv, you may note, as well, the naming conventions used by ActionScript, the UpperCamelCase and lowerCamelCase.
Classes are (by convention) written with UpperCamelCase (with the first letter capitalized), which indicates that name is a class name.
Objects are, generally, written with lowerCamelCase (with lower first letter and every new composed word with upper case), indicating that word is a object name (or another keyword, which may be reserved already).
Your syntax is a little odd - you say you have a MC called 'Box'. Are you creating this in the flash ide - or programatically? either way - you appear to be referencing Classes, and not instances of a class (an object). Try:
var _circle:MovieClip;
var _box:Sprite;
private function createDisplayObjects():void
{
_circle = new MovieClip();
this.addChild(_circle);
_box = new Sprite();
_circle.addChild(_box);
}
And - just as a generally agreed upon practice, class names are capitalized, and instance name are lowerCamelCase. I like underscores prefixing private vars, as well.
NemoStein is absolutely correct - reserved keywords will bork your code everytime...
good luck

How do I change subclass' variable from superclass?

For some time now I have been making a very easy game for iPhone in flash using as3.
Recently I came in contact with a small problem, which is why I am posting this!
The problem:
I have a superclass from which everything derives. In the superclass I initiate and place an Object on stage.
1. var myObject:typeA = new typeA();
2. stage.addChild(myObject);
As you can see this object follows the class 'typeA' which, ocf, has its own actionscript file. Inside of this file I have declared a global variable of type string.
What I want to do is change the varbiable on the new object from the superclass. Therefor I tried as following:
1. myObject.myVariable = 'someSortOfString';
Unfortunatly it didn't work and so I wonder how to do this; change a subclass' variable from the superclass.
You need to declare the variable that is being accessed from the subclass as protected (Or public), by default the variable is private so only accesible by the superclass.
e.g. protected var myObject:typeA = new typeA();
BTW did you mean change the superclass variable from the subclass instead of "change the subclass variable from the superclass"?