ActionScript3. How to cast Embed MovieClip to MovieClip? - actionscript-3

I have HintMenu.swf library with MovieClips :
public class PopupMenuItem extends MovieClip {
[Embed(source="HintMenu.swf", symbol="ExternalMovie")]
private var woodButtonClass:Class;
public var activeItemSkin:MovieClip;
public function PopupMenuItem(
activeItemSkin = new woodButtonClass();
)
But on start, I receive the following Error :
[Fault] exception, information=TypeError: Error #1034: Type Coercion failed:
cannot convert energy.ui.alts.component.popup::PopupMenuItem_woodButtonClass#a7c0e19 to
flash.display.MovieClip.
I need to use it as MovieClip later. Any solutions?

Use as MovieClip.
activeItemSkin = new woodButtonClass() as MovieClip;
You need to make sure that woodButtonClass is actually a MovieClip. If it is not then activeItemSkin will be null.

Actually you can`t cast Embed MovieClip with 1 frame to MovieClip.
Cause of optimization reasons Flash IDE converts it to Sprite :
package {
import flash.display.DisplayObjectContainer;
import flash.display.MovieClip;
import flash.display.Sprite;
[SWF(backgroundColor="0x333333")]
public class Test_MultipleSwfEmbed extends Sprite{
[Embed(source="swf/HintMenu.swf", symbol="button1")]
private var oneFrameMovieClip:Class;
[Embed(source="swf/HintMenu.swf", symbol="button2")]
private var twoFramesMovieClip:Class;
public function Test_MultipleSwfEmbed() {
//"is MovieClip" = true, when > 1 frame
traceType(new oneFrameMovieClip()); // false, true, true
traceType(new twoFramesMovieClip()); // true, true, true
}
private function traceType(mc:*):void {
trace(mc is MovieClip, mc is Sprite, mc is DisplayObjectContainer);
}
}
}

Related

How to call an MovieClip on stage AS3

Trying to addChild() inside a movie clip in the stage from one of my classes. The code looks like this
package {
import flash.display.SimpleButton;
import flash.events.MouseEvent;
import flash.display.MovieClip;
public class createFlask extends SimpleButton {
public function createFlask() {
addEventListener(MouseEvent.CLICK, createAFlask);
}
private function createAFlask(e:MouseEvent):void
{
var Flask = new flask ;
Flask.x = stage.width/2;
Flask.y = stage.height/2;
stage.experiment.addChild(Flask);
}
}
This gives an error as
Access of possibly undefined property experiment through a reference
with static type flash.display:Stage.
Any solutions?
Just omit "stage".
Instead use
experiment.addChild(Flask);
That will work.

calling movieclip of a class from another class, error#1009

i got error: method of null object reference. I'm so confused and don't know what is the real cause.
So i got a player movieClip on the stage which has an instance of "player_mc", which will be pass thru my Document class and into the Player class.
Player.as
import flash.display.*;
import flash.events.*;
public class Player extends MovieClip
{
public var myPlayer:MovieClip;
public function Player(player:MovieClip)
{
myPlayer = player;
addEventListener(Event.ENTER_FRAME,on_enter);
}
Document.as
import flash.display.*;
import Components.Player.Player;
public class Game_Main extends MovieClip
{
public var player:Player;
public function Game_Main()
{
player = new Player(player_mc);
}
}
now here i think is where the problem comes from. I have a green_enemy movieclip on the stage which has base class Enemy.
Enemy.as
import flash.display.MovieClip;
import Components.Player.Player;
import flash.events.Event;
public class Enemy extends MovieClip
{
var theplayer:Player;
public function Enemy()
{
this.addEventListener(Event.ENTER_FRAME,on_enter);
}
public function on_enter(e:Event):void
{
if (this.hitTestObject(theplayer.myPlayer)) //calls player_mc from Player class
{
trace("hi");
}
}
}
what I like to do on Enemy function is when Enemy collides with "player_mc" (which is on the stage) it will do something. Maybe my code is wrong.
Any help/tips will be appreciated. Thanks!
In your Enemy.as I see
var theplayer:Player;
not initialized. It is private, so you can not define it from outside. That means that exception comes from here
this.hitTestObject(theplayer.myPlayer)
You are trying to call myPlayer from null.
Try to define this variable while constructing Enemy class.
To prevent null exception you can check if theplayer is null
public function on_enter(e:Event):void
{
if (theplayer && this.hitTestObject(theplayer.myPlayer))
{
trace("hi");
}
}
To expand on what ZuyEL said, your variable isn't globally defined in your enemy class since you're just adding a var inside your enemy function. Instead, do the following
public var thePlayer:Player;
write the above after your Enemy class, before the Enemy function. Now it is available to the whole class and will no longer be null when you call it later.

Embedded .swf animation not stopping on stop() call?

I have a .swf animation that I created in Flash Professional. To use it in my actionscript project, I embed it as follows:
[Embed(source="../lib/fetching.swf")]
public var Fetching:Class;
I then create an instance and add it to the stage as follows:
//class variable
var mc:MovieClip;
mc = new Fetching();
this.addChild(mc);
This causes my animation to appear on the screen and loop indefinitely. However, when calling mc.stop(), the animation does not stop. I've tried removing the movieclip from the stage by calling removeChild(mc) but adding a listener on the ENTER_FRAME event told me the movieclip is still playing over and over.
you should set a Embed source mimeType, and you convert to ByteArray. and loaded. because you can't Direct Type Casting Fetching Class to MovieClip. If you define explicitly mimeType and convert by force, you'll get about TypeError #1034: Type Coercion failed: cannot convert YourProject_Fetching#108b780d1 to flash.display.MovieClip
refer a following code.
package
{
import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.utils.ByteArray;
public class TestProject extends Sprite
{
[Embed(source="../lib/fetching.swf", mimeType="application/octet-stream")]
public var Fetching:Class;
public var loader:Loader = new Loader();
private var mc:MovieClip;
public function TestProject()
{
loader.loadBytes( new Fetching() as ByteArray );
loader.contentLoaderInfo.addEventListener(Event.INIT, onSwfLoaded);
this.addChild(loader);
}
private function onSwfLoaded(e:Event):void
{
mc = loader.content as MovieClip;
mc.stop();
}
}
}
Have you checked to be sure the embedded clip is compiled for the AVM2 (ie. that it targets AS3 and not AS1 or AS2)? An Avm1 swf may cast to a MovieClip without throwing an error, but will not then respond to commands.

Flash TypeError: Error #1009: Cannot access a property or method of a null object reference. - when accessing TLFTextField from Document Class

I'm lost on this one. I receive a TypeError: Error #1009: Cannot access a property or method of a null object reference. output message the first time my Document Class tries to access a simple textfield on the stage (added from the IDE, not actionscript)
package {
import flash.display.*;
import fl.text.*;
import flash.text.*;
import flash.events.*;
import flash.net.*;
public class Main extends MovieClip {
private var _netConnection:NetConnection;
private var _responder:Responder;
/* some other public + private vars */
public function Main() {
init();
}
public function init(e:*=null):void {
_netConnection = new NetConnection();
_responder = new Responder(uponResult);
txt.text = "init()";
}
/* more functions */
}
}
I tried adding txt.addEventListener(Event.ENTER_FRAME, init); incase the txt TLFTextField wasn't ... there... at the beginning, but it still outputs the error.
I feel like a bit of an idiot atm, what's the prognosis doc?
JB
TLFTextFields are weird creatures, I've had a lot of issues with them recently.
I'd try to use the Event.ADDED_TO_STAGE event because the TLFTextFields have to be on the stage when you try to access them:
public function Main() {
addEventListener(Event.ADDED_TO_STAGE, init);
};
public function init(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
txt.text = "init()";
};
It should work if your TLFTextField is on the first frame on the Main Timeline.
Let me know if this one does the magic,
Rob

Constructor arguments problem ActionScript 3

I have a custom class defined in Actionscript and I want to make an instance of it in the main document of Flash application. However, after calling the constructor with one argument, Flash gives me this error:
Error #1063: Argument count mismatch on coa.application::MenuItem(). Expected 1, got 0.
This is my class:
public class MenuItem extends MovieClip{
var button:SimpleButton;
public function MenuItem(buttonLoc:uint) {
button = new InvBtn();
this.addChild(button);
button.x=-81;
button.y=buttonLoc*33;
button.addEventListener(MouseEvent.CLICK, mybringToFront);
}
}
And this is my attempt to call its constructor:
var menu1:MovieClip = new MenuItem(3);
Any idea, whats wrong?
Apologies, I can't comment yet, or I'd put this in a comment.
Are you sure that:
var menu1:MovieClip = new MenuItem(3);
is the only place that you're constructing a new MenuItem? You don't by any chance have the MenuItem class attached to some instances on the stage?
I changed your code to this (just so I could run it) and it works fine:
package{
import flash.display.MovieClip;
import flash.display.SimpleButton;
import flash.events.MouseEvent;
public class MenuItem extends MovieClip{
var button:SimpleButton;
public function MenuItem(buttonLoc:uint) {
button = new SimpleButton();
this.addChild(button);
button.x=-81;
button.y=buttonLoc*33;
button.addEventListener(MouseEvent.CLICK, mybringToFront);
}
public function mybringToFront(event:MouseEvent):void{
trace('blah');
}
}
}
Like quoo said, most likely you have an instance of the object that the class is attached to on stage. To test for that do this:
public class MenuItem extends MovieClip{
var button:SimpleButton;
// I changed it to int, cuz uint is extremely slow for any math
// other than bitwise operators, int is fast as long as no fractions
public function MenuItem(buttonLoc:int = -1) {
if (buttonLoc == -1)
trace("On stage instance found! Location: "+x+", "+y);
button = new InvBtn();
this.addChild(button);
button.x=-81;
button.y=buttonLoc*33;
button.addEventListener(MouseEvent.CLICK, mybringToFront);
}
}