Bitmap from Library - actionscript-3

I looked this up on Google and watched a video, but this doesn't work for me.
When I try to link a symbol in the library with an AS Linkage, then attach it to a BitmapData variable, this error code shows:
Scene 1, Layer 'Layer 1', Frame 1, Line 4 1067: Implicit coercion of a value of type Gun to an unrelated type flash.display:BitmapData.
I don't use bitmap or bitmapdata much, and I don't use classes much, so I have no idea what is wrong. This is my code:
import flash.display.BitmapData;
var gun:BitmapData = new Gun;
And this is my advanced properties for the symbol:
Export for ActionScript = true
Export in frame 1 = true
Class = Gun
(I do not have a file named Gun)
Base Class = flash.display.MovieClip
I do not know what I am doing wrong, because I looked at multiple videos and websites saying that this works.
Can somebody help me? Am I writing the code incorrectly, or is something wrong with my settings?

You can't declare it as BitmapData - BitmapData is just raw bitmap data :)
Since you have declared MovieClip as Base Class, this should become a MovieClip:
var gun:MovieClip = new Gun();
If your gun has no timeline (so it's a graphics with just one frame), set the Base Class to Sprite and handle it as sprite - this is better for memory and performance:
var gun:Sprite = new Gun();

Related

Awkwardness Between Scenes (What happened to my symbol?)

Say we have a flash file, and all the code and objects on the file are described below:
Scene One:
only one symbol with the instance name "char" is on the stage
Code for Scene One:
import flash.events.Event;
stage.addEventListener(Event.ENTER_FRAME,update);
function update(e:Event){
trace(char);
}
play();
Scene Two:
only one symbol with the instance name "char" is on the stage
Code for Scene Two:
stop();
If you try this out yourself, you will find that the Flash traces the object char to be "null" for a split moment, and then traces it properly once it has discovered the symbol on the second scene. Does anyone know why this is, since there is a symbol with the instance name "char" on both consecutive scenes?
I could not find any specific documentation related to this other than that which is provided by Adobe for the Event class, but I believe what you are experiencing is related to the differences between the events Event.ENTER_FRAME and Event.EXIT_FRAME and how the Flash runtime initlializes objects for use.
I ran a test using your code with Event.ENTER_FRAME, and experienced the same results you encountered; however, when I used the Event.EXIT_FRAME event, the display object did NOT trace as null at all.
Then I took it a step further and set up my timeline exactly as yours; however, changed the event code in Scene 1, Frame 1 to be:
import flash.events.Event;
import flash.display.MovieClip;
stage.addEventListener(Event.ENTER_FRAME,update);
stage.addEventListener(Event.EXIT_FRAME,update);
function update(e:Event){
if( e.type == Event.ENTER_FRAME ) {
trace("ENTER FRAME: " + currentScene.name);
}
else if( e.type == Event.EXIT_FRAME ) {
trace("EXIT FRAME: " + currentScene.name);
}
trace(char);
}
play();
And when executed noticed something interesting:
EXIT FRAME: Scene 1
[object MovieClip]
ENTER FRAME: Scene 2
null
EXIT FRAME: Scene 2
[object MovieClip]
ENTER FRAME: Scene 2
[object MovieClip]
EXIT FRAME: Scene 2
[object MovieClip]
...
The Event.ENTER_FRAME event was never called on Scene 1. Probably because that event had already occurred prior to the code on Scene 1, Frame 1 being executed.
The null reference was actually in relation to the char instance not yet initialized on Scene 2. Once the playhead was exiting the frame, probably when the instance was able to be referenced, it read as a MovieClip.
These behaviors are [probably] the reason why so many people recommend using a document class to add objects to the stage as necessary, attaching listeners to the Event.ADDED_TO_STAGE to know when they were added, so that you can handle functionality at the proper point in time that they are actually added to the stage; instead of waiting for the object to be able to be referenced via the timeline. My best guess is that if the ENTER_FRAME event had fired on Scene 1, it too probably would have traced null for char, just like it traced null on Scene 2. It may be null because the display object on the stage isn't yet initialized and so the code reference to that object isn't yet initialized either.
I wish I had more time to afford to investigating this for you, but this is the best test and explanation I could come up with to describe the behavior you are experiencing.
Cheers!

Accessing variables on Document class from a child class

There is a lot of confusion online about this topic, and I am amongst the confused.
Every time I try to change a variable on the Main.as from another class it fails.
What's worse? I remember doing this in the past in as3.
public var mainVar:String = "CHANGE ME"; //on Main.as
Types of things I try:
MovieClip(root).mainVar = "changed"; //error #1009
parent.mainVar = "changed"; //error #1119
this.parent.mainVar = "changed"; //error #1119
Main..mainVar = "changed"; //error #1119
I try to call a function and get similar results using the same language.
Thanks in advance for anyone who tries to help.
There have been so many times that it seems like the best idea to store the functions in the class and have them work off the main.as vars once they are called, but I can never find a reliable way to do this, and end up adding children and setting event listeners dynamically, and only working with vars from the main.as. It's easy to do the opposite, changing a var stored on the class from main.as.
Your "problem" is that AS3 is OOP, which means that classes work separately and you need to connect them. The old "way" of doing this (using root) is absolutely wrong when dealing with bigger projects.
There are many ways to do the connection between classes. First, your Main class acts like root (if defined as base class through Properties in Flash IDE). So if you create a class that is DisplayObject and add it to the main class (using addChild();), then you will be able to do much like before:
MovieClip(parent).myFunction();
I don't recommend this, but instead more reliable solution - pass the main class to the classes that must use it:
var somethingCustom:MyClass = new MyClass(this); // inside Main.as
Then in your newly created class save this as a variable and call functions from it:
var _root:DisplayObject;
public function MyClass(root:DisplayObject) { // MyClass.as
_root = root;
_root.callPublicFunction();
}
There are many resources that can help you understanding classes (saying so because this is the normal way they should work):
How Actionscript 3 Classes Work
http://www.untoldentertainment.com/blog/2009/08/25/tutorial-understanding-classes-in-as3-part-1/

AS 3.0 Creating instances

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

1067: Implicit coercion of a value of type Class to an unrelated type flash.display:DisplayObject

So for instance, I've got three .as files called 'Helicopter.as, Game.as, Blue.as'
and I also have a .fla file called Helicopter.fla (These files are all suppose to link together, to make the helicopter game) . In the Game.as file, I have the following;
if (blue1.hitTestObject(Helicopter))
{
trace("YOU HIT THE BLOCK!");
Helicopter.x = 76;
Helicopter.y = 217;
}
I have drawn the so called 'Helicopter'^ using API in a different file called Helicopter.as using this code;
graphics.beginFill(0x00FF00);
graphics.drawCircle(0, 60, 35);
graphics.endFill();
However, I originally had the "Helicopter' symbol drawn in the Helicopter.fla file (which I've now deleted), and now that I've drawn the 'Helicopter' using API, I get this error;
''1067: Implicit coercion of a value of type Class to an unrelated type flash.display:DisplayObject.''
Flash doesn't recognise the original Helicopter symbol (in the Helicopter.fla file, because I deleted it). But I want the system to detect the 'circle' drawn using API (In the Helicopter.as file). And I have no idea how to how to name the API drawn circle 'Helicopter', thus I'm getting an error. So how do I name the API circle to 'Helicopter', so the Game.as file recognises it. If you have no idea what I'm talking about, then don't worry, because I don't know too. Thank you. If you want, I can paste all the code I've done so far.
Helicopter is a class not a variable, therefore you cannot assign Helicopter.x. You need to create a variable var heli:Helicopter = new Helicopter(); and use heli.x
it also could be that you're not following the programming standards, and Helicopter is in fact a variable and not a Class, though the error seems to indicate otherwise.

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"?