I'm trying to play a movie clip when I mouse_over it. I can do it fine by doing:
mc1.addEventListener(MouseEvent.MOUSE_OVER,mover);
function mover(e:MouseEvent):void {
mc1.play();
}
But, I want to use the same function for other movie clips, for example, to play movieclip2, movieclip3 etc.
How would I achieve this?
mc1.addEventListener(MouseEvent.MOUSE_OVER,mover);
mc2.addEventListener(MouseEvent.MOUSE_OVER,mover);
mc3.addEventListener(MouseEvent.MOUSE_OVER,mover);
function mover(e:MouseEvent):void {
e.currentTarget.play();
}
You can make a class to encapsulate your logic for example, to access the MovieClip from the calling function, use the property of the Event object
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class PlayMovieClip {
// register the mouse over event with whatever MovieClip you want
public static function register(mc:MovieClip):void{
mc.addEventListener(MouseEvent.MOUSE_OVER,mover);
}
// unregister the event when you dont need it anymore
public static function unregister(mc:MovieClip):void{
mc.removeEventListener(MouseEvent.MOUSE_OVER, mover);
}
// the MouseEvent will be throw whenever the mouse pass over the registered MovieClip
// and in the MouseEvent property you have the targeted object
// so use it
public static function mover(e:MouseEvent):void{
// check if we have really a MovieClip
var mc:MovieClip=e.currentTarget as MovieClip;
if (mc!==null) {
// we have a MovieClip so we can run the function play on it
mc.play();
}
}
}
usage:
PlayMovieClip.register(mc1);
...
PlayMovieClip.register(mcX);
and to remove the event:
PlayMovieClip.unregister(mc1);
...
PlayMovieClip.unregister(mcX);
Related
I'm having a problem with my app. I've 3 frame in the timeline, in the 2nd frame I've a MC with its own class: at the end of the animation of the MC I call a function from the main class.
THE CODE OF THE MC CLASS IS THIS
public function frame134() {
stop();
var vMainTimeline: MainTimeline = new MainTimeline();
vMainTimeline.gotoFrame3();
}
THE FUNCTION IN THE MAIN CLASS IS THIS
public function gotoFrame3() {
trace("gotoFrame3");
this.gotoAndStop(3);
trace("DONE");
}
The output in the console is gotoFrame3 and DONE but gotoAndStop(3); doesn't work.
Any suggestion or help?
Thanks in advance
Rather then creating instance of MainTimeline you need to get reference by using parent property or by referring it by stage directly. Replace your frame134() method with below code.
public function frame134() {
var stageRef = this.parent;
stageRef.gotoAndStop(3);
}
or with
public function frame134() {
stage.gotoAndStop(3)
}
Hope it will work for you.
I have loaded movieClip to stage and was performing some events on that movieClip. Movie clip has own public functions and variables, and those are NOT accessible through currentTarget object in events.
Here is sample class:
package {
import flash.display.MovieClip;
import flash.display.Shape;
public class SampleClass extends MovieClip {
var str:String;
public function SampleClass() {
str="Some string";
/* draw just a sample rectangle to click on it */
var rectangle:Shape=new Shape ;
rectangle.graphics.beginFill(0x000000);
rectangle.graphics.drawRect(0,0,100,100);
rectangle.graphics.endFill();
}
public function getStr():String {
return str;
}
}
}
And here is loading on the stage and creating event:
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class MainClass extends MovieClip {
var a:SampleClass;
public function MainClass() {
a=new SampleClass();
addChild(a);
a.addEventListener(MouseEvent.CLICK,clickEvent);
}
function clickEvent(evt:MouseEvent):void {
var Obj=evt.currentTarget;
trace (Obj.getStr());
}
}
}
Tracing will return null instead of string value cause currentTarget is an Object, not a Class (movieClip). Any idea how to solve this?
//use this code it will work
function clickEvent(evt:MouseEvent):void {
var Obj:SampleClass = evt.currentTarget as SampleClass;
trace (Obj.getStr());
}
I dont know if your problem is solved now but the code you posted in your question worked okay for me..
What I did to test it..
In a new blank document, open Library (ctrl+L) and right-clicked to make symbol (MovieClip)
In linkage section, tick Export for Actionscript and call it SampleClass
Right click symbol item now added in Library list and choose Edit Class option
In that SampleClass I replace all with a paste of your code BUT NOTE: after that line rectangle.graphics.endFill();.. I also added the line addChild(rectangle);
Now when I test (Debug: Ctrl+Shift+Enter).. I see a black square that traces "Some string" everytime I click it..
Your MainClass.as was attached to the FLA as the Document Class (see Properties with Ctrl+F3)
I hope this is useful to you or anyone else trying this kind of code. Any issues just add a comment. Thanks.
I got problem when I am loading external swf which contains added_to_stage method. It execute two times. I don t understand why it is doing ? I am also interesting if both swfs have their own stage ?
Thank you for any answer
I am using greensock library to hanle loading.
Flash preloader:
package {
import flash.display.MovieClip;
import com.greensock.loading.SWFLoader;
public class Preloader extends MovieClip
{
public function Preloader()
{
var loader : SWFLoader = new SWFLoader("Prototype1.swf");
loader.load();
addChild(loader.content);
}
}
}
External SWF:
public class Prototype1 extends MySprite
{
public function Prototype1()
{
addEventListener(Event.ADDED_TO_STAGE, onAdded);
}
private function onAdded(e:Event):void
{
trace("onAdded");
}
}
OUTPUT: onAdded onAdded
The greensock documentation says:
suppressInitReparentEvents: Boolean - If true, the SWFLoader will suppress the REMOVED_FROM_STAGE and ADDED_TO_STAGE events that are normally dispatched when the subloaded swf is reparented into the ContentDisplay (this always happens in Flash when any DisplayObject that's in the display list gets reparented - SWFLoader just circumvents it by default initially to avoid common problems that could arise if the child swf is coded a certain way).
So you can change your code in Preloader.as to:
var loader:SWFLoader = new SWFLoader("Prototype1.swf", {suppressInitReparentEvents: true});
And you'll only get one call to onAdded.
I'm trying to learn AS3 and have run into a small problem.
I have a Bitmap class to which I add a MouseEvent.CLICK listener, but the event doesn't seem to be dispatched.
I use Flashdevelop to write AS3 code and Flex to compile.
I have two classes, Enemy.as and Player.as
The Player.as looks like this:
package Player
{
import flash.display.Sprite;
import flash.events.MouseEvent;
[Embed(source="../../assets/leek.swf", symbol="Leek")]
public class Player extends Sprite
{
public function Player()
{
trace("Player constructed");
addEventListener(MouseEvent.CLICK, handleClick);
}
private function handleClick(e:MouseEvent):void
{
trace("Clicked Player");
}
}
}
The Enemy.as looks like this:
package enemies
{
import flash.display.Bitmap;
import flash.events.MouseEvent
[Embed(source="../../assets/gardengnome.png")]
public class Enemy extends Bitmap
{
public function Enemy()
{
trace("enemy constructed");
addEventListener(MouseEvent.CLICK, handleClick);
}
private function handleClick(e:MouseEvent):void
{
trace("Clicked Enemy");
}
}
}
The two classes are pretty much identical except that one is a Sprite and I embedded a symbol from a swf file that I got from a tutorial, and the other is a Bitmap and I embedd a png file into that.
The Player class (the one that's a sprite and uses a symbol) fires off the MouseEvent.CLICK when I run the project and click on the Player image, but the Enemy class does not.
There are no compile warnings or errors, so I'm having a hard time understanding what is the issue exactly. Is it because one is a Sprite and the other a Bitmap, or is it because one uses a prepared symbol from a swf, while the other is just a png?
How can I make a Bitmap class respond to MouseEvent?
Thanks for any help!
From ActionScript® 3.0 Reference for the Adobe® Flash® Platform:
The Bitmap class is not a subclass of the InteractiveObject class, so
it cannot dispatch mouse events. However, you can use the
addEventListener() method of the display object container that
contains the Bitmap object.
Unfortunately Bitmap class doesn't dispatch mouse events you will have to wrap it inside a Sprite class.
In some of the level MovieClips I have for my Flash game, there is a certain MovieClip that controls a custom-built camera that I've created. Both the camera and the MovieClip function correctly and smoothly. However, whenever a level is completed and removed from the game, I get an Error #1009 not recognizing the checkCameraZoom function. Also, this MovieClip is not added dynamically with code, but rather placed in the specified level MovieClips from the Library before run-time. Is there any possible way to fix this error?
ZoomOutArea Class:
package com.engine.assetHolders
{
import com.engine.documentClass.*;
import flash.display.*;
import flash.events.*;
public class ZoomOutArea extends MovieClip
{
public function ZoomOutArea():void
{
this.visible = false;
this.addEventListener(Event.ADDED_TO_STAGE, initZoomOutArea);
// constructor code
}
public function initZoomOutArea(event:Event):void
{
this.addEventListener(Event.ENTER_FRAME, checkCameraZoom);
}
public function checkCameraZoom(event:Event):void
{
if (Document.getInstance != null)
{
if (this.hitTestObject(MovieClip(parent.parent).player.playerHitArea))
{
this.hitTestZoom(0.6);
}
if (! this.hitTestObject(MovieClip(parent.parent).player.playerHitArea))
{
this.hitTestZoom(1);
}
}
}
public function hitTestZoom(zoomLevel):Number
{
MovieClip(parent.parent).cameraScale = zoomLevel;
return zoomLevel;
}
}
}
You register the class for ENTER_FRAME events when it's added to the stage, but you never unregister it. So that's why it keeps going even after it has been removed from the stage, and has no parent anymore.
You could add another listener for Event.REMOVED_FROM_STAGE and then remove the checkCameraZoom listener:
public function initZoomOutArea(event:Event):void
{
this.addEventListener(Event.ENTER_FRAME, checkCameraZoom);
this.addEventListener(Event.REMOVED_FROM_STAGE, onRemoved);
}
private function onRemoved(event:Event):void
{
this.removeEventListener(Event.ENTER_FRAME, checkCameraZoom);
}