Looking for AS3 event when MovieClip is initialised like AS2 onclipevent(load) - actionscript-3

In AS2 there was an event onclipevent(load) that triggered when a movie clip was initialised on the stage. I have searched a lot but the only solution I can find for the same functionality in AS3 is to make a custom class for the movie clip. This is a bit overkill in my case as I only have one line of code to run and I'm trying to avoid lots of extra AS files in my project. I should note that my MovieClips are not dynamically added so events like Event.ADDED_TO_STAGE etc will not work.
Is there really no way to do onclipevent(load) in AS3?

There are no AS2 style clip events in AS3. If you want to avoid making a custom class for your clip, you could put the initialization code in a function and call it immediately when you create the clip, like this:
import flash.display.MovieClip;
function onClipLoad(clip : MovieClip) : void
{
// do onLoadStuff here
clip.x = 100;
clip.y = 10;
}
// call onClipLoad when the clip is created, either in code or via timeline
var myClip : MovieClip = new MyClip();
onClipLoad(myClip);
Even better, you could place this code on frame 1 of your MovieClip's timeline to automatically initialize instances of it:
var initialized : Boolean;
if(!initialized) {
initialized = true;
// do init code here
x = 100;
y = 10;
}

Related

ColorTransform for multiple movie clips

Despite searching both the web and youtube for a solution, it remains difficult to find information on colorTransforming multiple movie clips in AS3. I found a video on youtube about colorTransform which i followed steps to create a fully functional colorTransform for a single clip but i would like to use it for multiple clips and be able to change colors for each on a mouse click.
I have included the code below and perhaps someone knows how i can add more movie clips to it. When i copy and change mc1. EventListener code to mc2, i get a duplicate function error which i don't know how to fix.
import flash.geom.ColorTransform;
import flash.geom.ColorTransform;
import flash.events.MouseEvent;
// this here is the little movieclip where the main clip gets its color from,the clip is made up of two movieclips but can also be one movieclip
// instead of brushColor i have used myColor and instead of brush.tip i have used square.
var myColor:ColorTransform=new ColorTransform();
myColor.color=0xffffff; square.transform.colorTransform=myColor
red.addEventListener(MouseEvent.CLICK,onclick);
green.addEventListener(MouseEvent.CLICK,onclick);
blue.addEventListener(MouseEvent.CLICK,onclick);
orange.addEventListener(MouseEvent.CLICK,onclick);
yellow.addEventListener(MouseEvent.CLICK,onclick);
pink.addEventListener(MouseEvent.CLICK,onclick);
function onclick(event:MouseEvent){
if(event.target==red)
{myColor.color=0xff0000}
else if(event.target==green)
{myColor.color=0x99ff33}
else if(event.target==blue)
{myColor.color=0x00ccff}
else if(event.target==orange)
{myColor.color=0xffcc33}
enter code here
else if(event.target==yellow)
{myColor.color=0xffff66}
else if(event.target==pink)
{myColor.color=0xff99ff}
else
{myColor.color=0x666666}
square.transform.colorTransform=myColor
}
mc1.addEventListener(MouseEvent.CLICK, colorChange);
function colorChange(event:MouseEvent)
{
mc1.transform.colorTransform=myColor;
}
// upto here the code works fine but from below i get a duplicate fuction error which i don't know how to fix.
// the idea is to add more movie clips so i can change their colors just like i can do for mc1.
1021: DUPLICATE FUNCTION DEFINITION "ERROR"
mc2.addEventListener(MouseEvent.CLICK, colorChange);
function colorChange(event:MouseEvent)
{
mc2.transform.colorTransform=myColor;
}
Push all movieclips into array and then add eventlistener to all of them like this:
var mcArray:Array=new Array();
mcArray.push(mc1,mc2);
for (var i:int=0;i<mcArray.length;i++)
{
mcArray[i].addEventListener(MouseEvent.CLICK,colorChange);
}
And add only one colorChange function like this:
function colorChange(event:MouseEvent)
{
e.currentTarget.transform.colorTransform=myColor;
}

Making multiple objects draggable

I have about 50 symbols that I want to make draggable. Nothing fancy, just the ability to click it and drag it to a different location.
I found as3 code for doing so but when I paste it into my file it gives me errors:
**Error** Scene=Scene 1, layer=Units, frame=1:Line 9: The class or interface 'MouseEvent' could not be loaded.
function mouseDownHandler(evt:MouseEvent):void {
That code is:
// Register mouse event functions
fighter_uk.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
fighter_uk.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
fighter_uk.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
fighter_uk.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
// Define a mouse down handler (user is dragging)
function mouseDownHandler(evt:MouseEvent):void {
var object = evt.target;
// we should limit dragging to the area inside the canvas
object.startDrag();
}
function mouseUpHandler(evt:MouseEvent):void {
var obj = evt.target;
obj.stopDrag();
}
I'm using flash pro 8, so I tried finding as2 code but couldn't find it.
Also, is there an 'easy' way to code all 50 objects?
I think you're trying to compile AS3 code with AS2 compiler. Try changing your compilation settings to target AS3.
Also you may need to include the class import at the top of your code:
import flash.events.MouseEvent;
To drag 50 objects, add them all on the same container sprite and add the listener to the container sprite only:
var holder:Sprite = new Sprite();
for ( var i:int = 0, l:int = 50; i < l; i++ ) {
var dragee:YOUR_CUSTOM_OBJECT = new YOUR_CUSTOM_OBJECT();
holder.addChild(dragee);
}
addChild(holder);
holder.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
holder.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
holder.addEventListener(Event.MOUSE_LEAVE, mouseUpHandler);
var currentDragee:YOUR_CUSTOM_OBJECT = null;
function mouseDownHandler(evt:MouseEvent):void {
currentDragee = evt.target as YOUR_CUSTOM_OBJECT;
if ( currentDragee !== null ) {
currentDragee.startDrag();
holder.addChild(currentDragee); // bring current to front position
}
}
function mouseUpHandler(evt:Event):void {
if ( currentDragee !== null ) currentDragee.stopDrag();
currentDragee = null;
}
YOUR_CUSTOM_OBJECT being the object class you need to drag. Hope it helps!
This page seems to have the answers you are looking for (AS2 drag and drop). If you've already seen it, you'll need to explain why it's not good enough for your needs.
If you want to drag/drop multiple instances in AS2, you can still add the code to the movieClip symbol, export it from the library and load the instances up using attachMovie (all 50 of them). If they are all different, then attach the code as necessary to the clips themselves, or to some function elsewhere that will capture all the clicks and decide what was clicked. This is all very doable in AS2.
Remember you can use your onClipEvent(load) function to set up a lot of the initial lifting.
Here's a sample I made in AS2 for making a node tree. It's all draggable (mouse drag) and zoomable (with mouse Wheel). You can add nodes by clicking on the little down arrow in the node box. Each node is listening for the mouse.
You'll want to look at this section for the most part:
// Enable drag on button press
on (press)
{
startDrag(this);
}
// Stop the drag on release of mouse button
on (release)
{
stopDrag();
}
Besides this, I'm not really sure how your setup looks, so I hope this helps get the ball rolling. (Check the link, there's lots of little gems in there).
Flash Professional 8 only supports ActionScript 2 & 1
You can follow this official URL and learn how to do that in ActionScript 2, but I extremely recommend you to work with ActionScript 3.

AS3 Getting to Control Stage

I am building a Flash Game, I am doing this:
MainStage, with 11 frames
Mainstage Loads > Lobby.swf (1 frame)
Lobby Loads > Arrows.swf (1 frame)
I just want to control the main Stage from Arrows.swf , get the current frame, right arrow mainStage.current frame + 1, left arrow mainStage.current frame -1
I have tried the parent/child method
this.parent.parent
parent.parent
this.root
parent.root
every combination, I am also tracing and when I finally hit the mainStage it is saying cannot convert Stage to Display Object which I understand.
but how can I just get current frame of the "mainstage" needle and control it with gotoAndPlay or other such methods ?
I have solved it.
I didn't control the main stage what I did was, I reduced the main stage layer in the lobby layer when I load the left and right arrow I attach an event listener for a custom event.
In the arrows movie I have written a function to dispatch the event here are the lines of code I used
in the "main" stage of the movie you want to control:
//ball loader prep and load
var ballLoader:Loader = new Loader();
var ballRequest:URLRequest = new URLRequest("lobbyBalls.swf");
ballLoader.load(ballRequest);
ballLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, ballsLoaded);
function ballsLoaded(event:Event):void{
trace("ball Loader complete");
ballLoader.x = 50;
ballLoader.y = 200;
stage.addChild(ballLoader);
ballLoader.content.addEventListener("Previous", ctrlPrevFrame);
ballLoader.content.addEventListener("Next", ctrlNextFrame);
}
In the movie from which you want to control
import flash.events.Event;
leftArrow.addEventListener(MouseEvent.CLICK, prevEvent);
rightArrow.addEventListener(MouseEvent.CLICK, nextEvent);
function prevEvent(event:Event):void{
trace("prev");
dispatchEvent(new Event("Previous", true));
}
function nextEvent(event:Event):void{
trace("next");
dispatchEvent(new Event("Next", true));
}
Thanks anyways....

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);

MovieClip doesn't dispatch mouse event after nesting MovieClip loaded from external SWF

Mostly, problem is described in the title... I tried to load an external SWF file that contains some named MovieClip instances (exporting and naming is done by Flash CS5 software) and to add some of externaly loaded (named) MovieClip-s in MovieClip object which is created in my code. Problem appears when i add MOUSE_CLICK listener to parent MovieClip. Simply, it does not dispatch event when i click on it at the stage...
private var loader:Loader;
public function Example(){
loader = new Loader();
var request:URLRequest = ... // URL to external SWF
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadingCompleted)
loader.load(request);
}
private function loadingCompleted(event:Event):void{
var mc:MovieClip = loader.content as MovieClip;
var myMovie:MovieClip = new MovieClip();
myMovie.addChild(mc.getChildByName("object_name"));
myMovie.addEventListener(MouseEvent.CLICK, myMovieClicked);
addChild(myMovie); // myMovie (with nested mc) appears on the stage
}
private function myMovieClicked(evt:Event):void{
//never reached
}
EDIT: I didn't mention that i'm working in Flex using FlashBuilder 4.5 where i created ActionScript project. Code above is body of Example class, which is main SWF class.
EDIT AFTER ANSWER: myMovie.mouseChildren = false solves the problem. Earlier i tried to set mouseEnabled = true, and it didn't fix the problem. But i'm confused about event flow now... Even if child is target node, why mouse listener on parent MovieClip doesn't recieve event (in capture phase) when parent is still on event flow? Moreover, when i create another movie clip in my code (whit some simple shape inside) and add it to myMovie, everything works fine. What is so special when i obtain movie clip from externaly loaded SWF?
Have you tried doing myMovie.mouseEnabled = true and myMovie.mouseChildren = false ?