Scrubbing timeline of movieclip that executes actionscript within - actionscript-3

I have an as3 script that will scrub the timeline of a movieclip on the stage.
This scrubs the timeline and displays the frame by frame animation but does not execute any actionscript placed on key frames within that movieclip. Users can scrub backwards or forward. I would like to load the script as well as unload depending on the direction the user scrubs. Can this be done?

What you can do is to make new class and extend MovieClip, Inside of that class you can create public methods that you can call from timeline:
public class MyMC extends MovieClip {
public function myMC() {
// constructor
}
public function load():void{
// Do something
}
public function unload():void{
// Do something
}
}
Than if you used "interface" to to add movie clip, click on movie clip and change instance from MovieClip to your class MyMC. Give it also a name (variable name) for example my_mc Than you can call your function from action on timeline
my_mc.load();
my_mc.unload();
Or if you are using just actions to create movie clip you can do like this:
var my_mc:MyMC = new MyMC();
my_mc.load();
my_mc.unload();

Related

Can't cast embeded MovieClip to MovieClip type

I'm having trouble trying to embed a MovieClip in an ActionScript file I'm composing in FlashBuilder.
public class ItRock extends Item
{
public static const ID:String = "rock";
[Embed (source="/../art/menu/console.swf", symbol="itRock")]
private var IconClass:Class;
public function ItRock(game:Game)
{
super(ID, game);
var icon = new IconClass();
// var icon : MovieClip = new IconClass();
// var icon : MovieClip = new IconClass() as MovieClip;
addChild(icon);
}
}
My console.swf file contains a symbol called itRock which is of type MOvieClip and set to Export for ActionScript. In my code, I want to create an instance of this symbol and add it as a child of my Item class(which extends Sprite). However, when I create an instance of the embedded class, I create an object with the type name of console_swf$831ea9c30fe7882fadc388b74e115654-652499362. I can add it as a child fine, but if I try to cast it to a MovieClip implicitly I get an error that cannot be converted to a MovieClip. If I try to cast explicitly, I just get null.
Any idea what I'm doing wrong here?
Okay, it looks like Flash will automatically convert your MovieClips to Sprites when it can. So my above code will work if I change the MovieClip classes to Sprite.
The below website describes this. It suggested that adding an extra frame to your symbol will force Flash to treat is as a MovieClip and not a Sprite - however, I've created a simple two frame animation and its still exported as a Sprite. (It even plays the animation - I thought Sprites were supposed to be just static images with no animation?)
http://chrismweb.com/2011/03/20/problems-with-embedding-swfs-in-actionscript-or-flex/

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
}

Referencing a object that is on the stage inside a class?

So I had the issue with ENTER FRAME so I moved it to a separate class and this is what the class looks like
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.accessibility.Accessibility;
import flash.display.DisplayObject;
import flash.display.Stage;
public class enemy extends MovieClip {
public function enemy() {
// constructor code
this.addEventListener(Event.ENTER_FRAME, moveEnemy);
}
public function moveEnemy(e:Event):void{
this.x += 5;
if(stage.player.scaleX == 1){
this.scaleX = 1;
}else {
this.scaleX = -1;
}
}
}
}
Now Im trying to adjust the enemies scalex according to the players but I get a error when referencing the player inside the class can anyone help me solve this?
The trick with using Event.ENTER_FRAME listener is that event.currentTarget will hold the link to the object that's processing the event, event.target will hold the link to the object that's received it first, so you can attach the listener not to the stage, but to the MovieClip of your choice, including having more than a single listener across your game. Say, you give your Enemy class a listener that's making it query stage's list of player's bullets and check collisiong against this, a player can do this too. Or, you use a single listener and do ALL the work inside it, using local arrays to store lists of enemies, bullets, player(s) and other objects.
In terms of passing parameter to enter frame listener - your event is automatically dispatched, so you shouldn't bother with this, and it does not accept more than one parameter.
Regarding your code, you should add enemy movement code in testPlayerCollisions() listener below querying for player collision. For this, you already have an enemy you're about to move, so you just have to call its move() function or whatever you have for it.
It looks like your enemy class doesn't have access to the stage; yet you're trying to reference stage.player. You can access the stage from your main class but not from other classes unless you pass it through the constructor.
Try passing the stage to the enemy class and create a class variable to store it. Ie:
private var stageInst:Stage;
public function enemy(s:Stage){
stageInst = s;
}
Then in moveEnemy use stageInst.player to access the player's clip.
When you create enemy's you'll have to pass in an instance of the stage from Main.
ie: var e:enemy = new enemy(stage);

ActionScript 3 Access Manually Added MovieClip Instance From A Class (not Document Class)

If I have an instance named "btnExit" which I added manually to the Main Timeline, I can refer to it from my Document Class simply by typing its name, eg.
package{
public class Engine extends MovieClip{
public function Engine(){
trace(btnExit.x);
}
}
}
But now I have an instance that is nested in another movieclip, and I want to access it from a class (not a Document Class).
Let's say I have "Menu" movieclip on my library.
Then I manually drag a "ButtonExit" button to the "Menu" movieclip timeline, I named the instance "btnExit", so "btnExit" is the child of "Menu" movieclip.
The "Menu" movieclip will be added dynamically by code to the main timeline.
Now I want to access the "btnExit" from "Menu" class file, so I write these codes.
The Document Class:
package{
public class Engine extends MovieClip{
public var menu:Menu;
public function Engine(){
menu = new Menu();
addChild(menu);
}
}
}
The Other Class:
package{
public class Menu extends MovieClip{
public function Menu(){
trace(btnExit.x);
}
}
}
But I got error #1009 (null object reference) for unable to access btnExit;
Can anybody help me, pleaseee?
Make sure that the instance of ButtonExit exists on every frame of the menu timeline and that it's instance named appropriately on every frame or write an if statement to only run your code if the Button is currently on the correct frame.
Every frame when the menu clip animates it's going to re instantiate everything on the frame, so if your second/third/fourth/etc frame doesn't have a clip named btnExit it's going to throw an error when the constructor is called for that frame.

Detecting when an object gets added to a movieclip

Alright, so I'm trying to figure out when a child is added to a movieclip "x", and handling/detouring this operation from within this "x" movieclip.
I tried overriding addChild and addChildAt at with no prevail. The movieclips that are placed on the stage via flash still don't trigger addChild or addChildAt. However, tracing this.numChildren shows '2' correctly.
Any hints?
You can add an event listener for the "added" event for the x movie clip.
x.addEventListener(Event.ADDED, addHandler);
function addHandler(e:Event){
// your code here
}
This link may explain it better:
AS3.0 – Event.ADDED and Event.ADDED_TO_STAGE
The documentation is also a good resource:
flash.events.Event
You can override the default methods of a movieclip by doing the following:
Create a class to extend a movieclip:
package {
import flash.display.*;
public class SuperMovieClip extends MovieClip {
public function SuperMovieClip() {
// constructor code
super();
}
override public function addChild(child:DisplayObject):DisplayObject {
trace("Hello, I am overriding add child");
// still perform the default behavior but you can do what ever you want.
return super.addChild(child);
}
}
}
Then in Flash create a new movieclip, and make sure it is marked as Enable for ActionScript. The Class should be any name you want, but the base class needs to be SuperMovieClip (or the name you chose for your extended class) See image:
Now when any stage clip is created of this base type (regardless if it's in the IDE or through code) it will be of type SuperMovieClip and anytime addChild is called it will override the original function.
For example, I placed an instance of this mc from library onto the stage at design time and compiled it using the following code on the timeline:
import flash.display.Sprite;
stage_mc.addChild(new Sprite());
And it output Hello, I am overriding add child