Referencing Flash Professional Object in Flash Builder Actionscript - actionscript-3

I am new to working with Flash Builder and Flash Professional. I have a movie clip called myplayer that I created in Flash Professional, and I am trying to code some ActionScript for it in Flash Builder that will change its position on the stage, but I keep getting the following error: TypeError: Error #1009: Cannot access a property or method of a null object reference. at level_1()
Here's the code:
package
{
import flash.display.MovieClip;
public class level_1 extends MovieClip
{
public function level_1()
{
myplayer.x=650;
myplayer.y=350;
}
}
}
I know I am missing something, but I'm not sure what. Any advice?

There are two symbols here: level_1 and myplayer. Naming convention for classes usually start with a capital letter; so, I'm going to refer to these types as Level1 and MyPlayer.
So, here's the scene:
Level1 is your game level
MyPlayer instance is a child of Level1
The player of this level needs an instance name defined as myplayer.
myplayer is an instance of the MyPlayer class (ActionScript linkage).
Now from Flash Builder, our Level1 class may manipulate the child myplayer instance:
package {
import flash.display.MovieClip;
public class Level1 extends MovieClip {
public var myplayer:MyPlayer;
public function Level1() {
super();
myplayer.x = 650;
myplayer.y = 350;
}
}
}
Flash CS5 example source code with Level1 ActionScript class at: http://labs.jasonsturges.com/stack-overflow/examples/referencing-flash-professional-object-in-flash-builder/

Related

AS3: Why do I get so many error 1120s?

I started a new AS3 document today in Flash CC. My stage was empty. I made the document class a .as file called test.as - my .fla was also called test.fla.
So I created a movieclip called mirror and gave it a AS3 linkage name of mirror. I put it in my library and deleted it from the stage. Then I went to my external .as file and wrote this:
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class test extends MovieClip {
public var mirror1:MovieClip = new mirror();
public function dragMirror1(event:MouseEvent):void
{
mirror1.startDrag();
}
public function releaseMirror1(event:MouseEvent):void
{
mirror1.stopDrag();
}
mirror1.addEventListener(MouseEvent.MOUSE_DOWN,dragMirror1);
mirror1.addEventListener(MouseEvent.MOUSE_UP,releaseMirror1);
}
}
This seemed perfectly harmless code, but when I ran the code I got four errors:
C:\Users\Raphael\Creative Cloud Files\LightStage\Testing\test.as, Line 20, Column 48 1120: Access of undefined property releaseMirror1.
C:\Users\Raphael\Creative Cloud Files\LightStage\Testing\test.as, Line 20, Column 3 1120: Access of undefined property mirror1.
C:\Users\Raphael\Creative Cloud Files\LightStage\Testing\test.as, Line 19, Column 50 1120: Access of undefined property dragMirror1.
C:\Users\Raphael\Creative Cloud Files\LightStage\Testing\test.as, Line 19, Column 3 1120: Access of undefined property mirror1.
Does anyone have any idea why this is happening? Maybe I'm missing something basic, but I've created a few new .fla and .as files to test this and it keeps happening, even when I rewrite the code and use different AS3 linkage names.
To avoid these errors, you have to use your mirror1.addEventListener() inside the constructor of your class after adding your mirror object to your stage :
public class Test extends MovieClip {
public var mirror1:MovieClip = new mirror();
public function Test():void
{
addChild(mirror1);
mirror1.addEventListener(MouseEvent.MOUSE_DOWN, dragMirror1);
mirror1.addEventListener(MouseEvent.MOUSE_UP, releaseMirror1);
}
public function dragMirror1(event:MouseEvent):void
{
mirror1.startDrag();
}
public function releaseMirror1(event:MouseEvent):void
{
mirror1.stopDrag();
}
}
Hope that can help.

ActionScript and Scenes

In an usual flash movie, the document has many scenes. I can add and create scenes as I need (Say I have startingScene, middleScene, and endingScene).
Then, I can assign a Document Class to my movie. The Document Class (say I declare a Main.as class and link it), since it inherits from MovieClip, has a scene attribute.
What's the actual relationship between the Main instance which would be the document, and the current scenes?
Is Main class owner of all scenes? Is a Main instance created for each scene? Who's the owner of the scene list?
The Document Class is presenting everything you have in the stage so if you have your scene defined in the timeline you can call
this.gotoAndPlay(0, "Scene 2");
Where this is your Main Class Which is something like this
package
{
import flash.display.MovieClip;
public class Main extends MovieClip
{
// instance variables go here
public function Main()
{
this.gotoAndPlay(0, "Scene 2");
}
// other functions can go here
}
}
So Main Class is owning the timeline and the stage you have defined in Adobe Flash professional for example if you have a button called myButton in the stage it is part of the Main Class so you can do
myButton.addEventListener(MouseEvent.CLICK, clickHandler);
function clickHandler( event:MouseEvent ):void
{
//button clicked
}

Referring to objects with the same parent via as3 class file

recently I got many (about 70) #1119 and #1120 errors in Flash. I've searched the web, but none of the solutions has solved my problem. Attempting to find the cause of the error myself, I made a new Flash animation. The contents:
A movieClip called "nr1" without instance name.
Inside of nr1 there are two movieClips, "nr2" with the instance name "ob2" and "nr3" with the instance name "ob3". Associated with nr2 is the as3 class file "nr2.as3". Here is the code inside nr2.as3:
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class nr2 extends MovieClip {
public function nr2() {
// constructor code
this.addEventListener(MouseEvent.CLICK,func1);
}
function func1(e:MouseEvent){
parent.ob3.x += 50;
}
}
}
This should refer to the object with the instance name "ob3", which has the same parent as this (nr2). Still, I get two identical #1119 errors at line 15 (parent.ob3.x += 50;). How do I refer to an object with the same parent via an as3 class file?
It isn't a good idea to set ob3's property in nr2. You could dispatch an event in nr2, and add eventListener in parent, so the parent could catch the event and do something with bo3.
If you really want to set ob3's property in nr2, try this
function func1(e:MouseEvent) {
var ob3:MovieClip = parent['ob3'] as MovieClip;
if (ob3)
{
ob3.x += 50;
}
}
Agree with Pan, it's not a good idea to control a child from another child. Let the parent (nr1) who has references to both child do the control. So you should create nr1 class
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class nr1 extends MovieClip {
public function nr1() {
// constructor code
ob2.addEventListener(MouseEvent.CLICK,func1);
}
function func1(e:MouseEvent){
ob3.x += 50;
}
}
}

Use Squiggly for Spell-checking on a TLFTextField

In CS6, I am trying to apply the Squiggly functionality to a TLFTextField in a movie clip. I've read the thread here: Using Squiggly in Flash CS5 and followed the setting recommendations, except my AS3 class is on the movieclip not the document. But there is no spell-checking and there is no compiler or run-time errors.
The movie clip that contains the TLF has a AS3 class called UserInput.
package {
import flash.display.MovieClip;
import fl.text.TLFTextField;
import flashx.textLayout.edit.EditManager;
import com.adobe.linguistics.spelling.SpellUIForTLF;
public class UserInput extends MovieClip {
// stage instances
public var userInput:TLFTextField;
public function UserInput() {
// add spell checking
userInput.textFlow.interactionManager = new EditManager();
SpellUIForTLF.enableSpelling(userInput.textFlow, "en_US");
}
}
}
I can enter text but there is no spell-checking. Am I missing something?
Thanks for your help.

Flash reference error: "Cannot create property __id0_ on MyMovieClip"

I'm using Flash Builder as the main flash dev tool, and use Flash as assets creating tool.
Basically this works fine, but one day I made a 3D tween in Flash timeline, and then link the asset "MyMovieClip" with my actionscript code, like this:
package
{
import flash.display.MovieClip;
[Embed(source="somefile.swf", symbol="MyMovieClip")]
public class MyMovieClip extends MovieClip
{
public function MyMovieClip()
{
super();
}
}
}
Then this Error came up: "Error #1056: Cannot create property __id0_ on MyMovieClip".
I'm sure there's no "__id0_" in my fla file.
So I deleted the 3D tween animation layer and re-published the fla file, no Error encountered.
Any clues? Is that impossible to make a movieclip asset with 3D timeline animation?
Make your MyMovieClip class a dynamic class:
package
{
import flash.display.MovieClip;
[Embed(source="somefile.swf", symbol="MyMovieClip")]
public dynamic class MyMovieClip extends MovieClip
{
public function MyMovieClip()
{
super();
}
}
}
Otherwise, you may have a conflict with an instance on your timeline and property within the somefile.swf movie clip.