AS3 - Addchild from library - actionscript-3

I'd like to add a movieclip from the library to a movieclip on the stage.
function setMc(con:Sprite,mc:Sprite):void
{
con.addChild(mc)
mc.x=mc.width/2
mc.y=mc.height/2
}
setMc(myholder,mylibrarymc)
I get this error:
TypeError: Error #1034: Type Coercion failed
What do I need to change?

Library only contains prototypes, not actual objects. I assume "mylibrarymc" is a name of the MC type in library. In this case "mylibrarymc" is type Class, which is used differently.
function setMc(con:Sprite,mc:Class):void
{
var newMC:DisplayObject=new mc() as DisplayObject;
// Here you actually make an object out of a class
con.addChild(newMC);
newMC.x=newMC.width/2;
newMC.y=newMC.height/2;
}
setMc(myholder,mylibrarymc);
Hope this helps. It's been quite some time I dabbled with libraries.

Your function expects a Sprite, and you're probably passing it a MovieClip. Have a look at the "export for actionscript" options you have when right click on the mc in the library

Right click the object in the library and go to 'Properties'. Make sure the box labelled "Export for ActionScript" is ticked and assign it an appropriate name. The class name you give the symbol is what you would use to create an object of that type in the code itself, for example:
Export a symbol for ActionScript with the Class name "Player".
In your ActionScript file:
var player = new Player();
addChild(player);
You can manipulate the object using any of the MovieClip member functions (position, alpha) and assign event listeners to make it interactive.

Related

as3 Trying to access a main timeline variable from inside a movieclip

I just found a way to do it, but I'm still getting an error.
On the main timeline I have:
var onBeat:Boolean = new Boolean;
and inside a movieclip I attempt to access it with:
MovieClip(root).onBeat = true;
and it technically works, the variable changes. But it throws this error which causes problems:
Error #1034: Type Coercion failed: cannot convert flash.display::Stage#7fffaa2c0d1 to flash.display.MovieClip.
Is this just an impossible task?
You got that error because you've added your MovieClip to the stage's display list and not to the main timeline one.
So in your main timeline code (or your document class), you can add your MovieClip using addChild(your_mc_instance) or this.addChild(your_mc_instance).
But you can also get a working code even with your MovieClip instance added to the stage using, for example :
var _root:DisplayObjectContainer = DisplayObjectContainer(root);
MovieClip(_root.getChildAt(0)).onBeat = true;
_root.getChildAt(0) here is returning your main timeline instance as it has also been added to the stage's display list before any other object.
Hope that can help.

Action script adding a background

First of all I've just a couple hours experience with Flash and AS3 therefore, I'm sorry if it is a bit easy question.
What I want do to is a simple space war games to learn basics of AS3 and flash. Altough I dont know much things about layers, I think my game should contain two layers, one for background and the second one is for enemies spaceships and our spaceship.
I added a jpeg format file to library to use it as a background. (as you can see from the link :http://prntscr.com/2pe6zb and http://prntscr.com/2pe733 )
And I create a as3 documentFile which is called Arkaplan.as and content of it is:
package{
import flash .display.*;
public class Arkaplan extends MovieClip{
public function Arkaplan(){
var hero:backGround =new backGround();
addChild(hero);
}
}
}
However, I got an error which says that : "1180: Call to a possibly undefined method BackGround."
Is there anyone to help me to solve what is wrong ?
EDIT
When I changed the code above lilke :
package{
import flash .display.*;
public class Arkaplan extends MovieClip{
public function Arkaplan(){
var myBitmap:Bitmap = new Bitmap(new backGround(500, 500));
var myMovieclip:MovieClip=new MovieClip();
myMovieclip.addChild(myBitmap);
addChild(myMovieclip);
trace("deneme 1-2");
}
}
}
Problem is solved but I dont know why it runs correctly know ? To be able to use Bitmap Do I have to add it as a child to movieClip ?
Any time you use the word 'new' you are creating a new object. The word after new will be the name of a Class that Flash will try to locate. You must either have an external .as file that matches that name or have a library item set to 'export for ActionScript' with a Class name that matches that name.
You can access a library item's properties by right-clicking on it in the library and clicking 'properties'. With the 'advanced' option open, check the 'export for ActionScript' box and enter a Class name that matches the one you want to create.

Using a MovieClip as a parameter in AS3

I'm having problems attaching MovieClips to different instances of a class. I'm kinda new to ActionScript 3, honestly, so this question might be a bit noobish. I did the research, though, but haven't found the kind of answer that I expected.
function AddNewElement(clip:MovieClip, array:Array, name:String, firstValue:int, secondValue:int):Element
As you may be able to guess, this is the function I made to create instances of a class in a dynamic way and add them to an array. I'm having problems with the very first parameter, though. How do I pass a MovieClip from my library to this function?
I saw a lot of answers to problems similar to this one stating that each MovieClip should be a class on its own, but since I have like forty MCs and I want to use them all for more or less the same thing I feel that it kills the purpose of classes, really.
How should I approach this?
First you need to give your library symbols unique class names from the linkage section while exporting or later from the "properties" option. You will see this input when you check the "Export for ActionScript" option there. Then you will need to instantiate your library symbol (with the new keyword) and cast it to MovieClip to pass to this function. So
AddNewElement(new LibrarySymbolClass() as MovieClip,[],'etc',0,0);
AddNewElement(MovieClip(new LibrarySymbolClass()),[],'etc',0,0);
will both let you do what you want.
However, since not all your library elements need to extend the MovieClip class, you would better pick DisplayObject instead of MovieClip. So a better version of your function would be
import flash.display.DisplayObject;
function AddNewElement(clip:DisplayObject, ...):* {
// some code here
return clip;
}
var clip:LibrarySymbolClass = AddNewElement(new LibrarySymbolClass() as DisplayObject,[],'etc',0,0);
trace(clip);
Using asterisk in the return value type will let it return the object with its right type (as [object LibrarySymbolClass] in this example).
Why not to create the movie clips on runtime, aka. creating them in the runtime execution context then instantiating each of them at the moment when you invoke the class. If each of the MC's is different, then either you create a MC class for each of them, giving them a name which end up in a ascending number, then using a for loop you put them in an array like so:
var mc_num:int = 40 // the number of MovieClips
var arr:Array = new Array();
for (var i:int=0; i < mc_num; i++) {
arr.push("myMovieClip" + String(i));
}
..then making reference to each of them by using the array index. I skip the part where you associate the images to MovieClips.
After that you invoke the desired MC as below:
var mc_1:MovieClip = arr[1] as MovieClip;
stage.addChild(mc_1);
When you create a MovieClip in Flash it gives you certain options, one of those options is for Flash to create a class for that MovieClip. That being said if you have that option applied to all forty movie clips then you create something like a master movie clip class and have each movie clip class extend the master movie clip class. The only thing is that you would have to create a .as file for each of your 40 movie clips and add extends MasterMovieClip to the class declaration. For example:
public class MasterMovieClip extends MovieClip {
// All of the variables and methods pertaining to each movie clip go here
}
Then each individual movie clip would resemble this class.
public class IndividualMovieClip_1 extends MasterMovieClip {
// Just include a constructor, even though you don't have to
}
Now all of your individual movie clips will have the same methods and variables, as long as said methods and variables are public, not private.
With this method you would have to create all 40 classes, however there might be a way in Flash when creating a new movie clip to set which class the movie clip extends and then you wouldn't have to create 40 different classes.
Update:
I re-read your question and thought of something else, that option I talked about in the first sentence, the one about Flash giving the option to create a class. Well if a class is not given then Flash will dynamically create a class at run-time. I think when it dynamically creates a class it does not maintain the same name as the library movie clip, so when your trying to pass the static name of your movie clip to your function, it has no clue what your talking about and throws a runtime error.

1119:Access of possibly undefined property Click through a reference with static type Class

I am making an edutainment game using flash cs5, I'm really new at using flash, in fact we never yet tackle it at school, but I insist on learning about it.
In my codes, I encountered this error
C:\Users\acer\Desktop\JikanLibrary\Main.as, Line 16 1119: Access of possibly undefined property Click through a reference with static type Class.
This is the code I used in my program
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Main extends MovieClip
{
var startPage:StartPage;
var jikanBookshelf:JikanBookshelf;
public function Main()
{
startPage = new StartPage;
jikanBookshelf = new JikanBookshelf;
startPage.jikanBookshelf.addEventListener(MouseEvent.Click, onJikanBookshelf);
addChild(startPage);
function onJikanBookshelf(event:MouseEvent):void
{
addChild(jikanBookshelf);
removeChild(startPage);
}
}
}
}
The error is in this line
startPage.jikanBookshelf.addEventListener(MouseEvent.Click, onJikanBookshelf);
Since I'm really new at flash, I really don't know what went wrong with my codes, it was working a while ago before I put the mouse event. I hope someone could help me.
ActionScript is a case sensitive language. This means that Click is not the same as CLICK. So what you need here is MouseEvent.CLICK
"Why is CLICK all uppercase? Most property names aren't.", you might ask.
That's because CLICK is a static constant property of MouseEvent and the convention amongst ActionScript (and many other languages) programmers is that static constants are written in all uppercase to distinguish them visually from other variables.
'static' means it's a property of the MouseEvent class, not of an instance of MouseEvent.
'const' means it's not a variable: you can't change it's value.
It's a name conflict problem: The class definition name is same as the object name.
The problem in your script is that you have a class definition name startPage and you are trying to create an object of same name startPage.
You have to change the object name to something different. Let say startpage1.

Recreating stage layout in as3

I have 27 MovieClips in my Library which are all images. At the moment they are positioned on the stage as instances of their parent and then made to function in the first frame of my actions layer. I want to recreate this layout solely in code so there is nothing on the stage. How do I do this?
Thanks in advance.
Sam
Right click on a movieclip in the library, then go to Properties.
Tick "Export for ActionScript", then check the name where it says "Class". Hit OK.
Let's say this name was "Symbol1".
Then type this script:
var symbol1:MovieClip = new Symbol1();
addChild(symbol1);
var symbol1 means you created a variable, MovieClip is the type. This MovieClip variable is a "new " Symbol1 (this was the name in the library, Properties, Class.
Then add this to the stage:
addChild(symbol1)
If you want to position it on the stage, set the coordinates of the variable:
symbol1.x = 10;
symbol1.y = 10;
puts it to (10, 10).
Depending on how many objects you have you can type this code for each one of them (don't forget to Export them for actionscript in Library->Properties).
If you have tons of movieclips and you don't want to type evertyhing, but would rather write some dynamic code, give us a hint on your library structure and how you named your objects.
Hope this helps.