How best to trigger function that is inside a movie clip - actionscript-3

I am tying to trigger a function that is inside a Movie Clip
my_mc.addEventListener(MouseEvent.CLICK,myfuntion);
function myfuntion(myEvent:MouseEvent):void
{
MovieClip(this.root).gotoAndStop(44);
tvscreen_mc.function("byby");
}
the byby function is inside tvscreen_mc
function byby(myEvent:MouseEvent):void {
my_player.destroy();
}

Replace
my_mc.addEventListener(MouseEvent.CLICK,myfuntion);
function myfuntion(myEvent:MouseEvent):void
{
MovieClip(this.root).gotoAndStop(44);
tvscreen_mc.function("byby");
}
with
MovieClip(root).my_mc.addEventListener(MouseEvent.CLICK, MovieClip(root).tvscreen_mc.byby);
this will also work, if you're at the root level (which I assume you are, but incase you put it somewhere else):
my_mc.addEventListener(MouseEvent.CLICK, tvscreen_mc.byby);

Related

Event does not happend on moving symbol in Animate CC (HTMLCanvas)

Hi this code is working well on moving symbol(classic tween)
var frequency = 3;
stage.enableMouseOver(frequency);
this.movieClip_1.addEventListener("mouseover", fl_MouseOverHandler_9);
function fl_MouseOverHandler_9()
{
alert("Moused over");
// this.gotoAndStop(41);
}
but if i replaced with this.gotoAndStop(41); it does not work
the event-target is accessible (will be passed within the event-handler-parameter-object) like this:
this.movieClip_1.addEventListener("mouseover", fl_MouseOverHandler_9);
then in the handler function:
function fl_MouseOverHandler_9(evt)
{
// "evt.currentTarget" represents the event-trigger
evt.currentTarget.gotoAndStop(41);
}
or you can pass the scope of the desired MC to the event handler:
this.movieClip_1.addEventListener("mouseover", fl_MouseOverHandler_9.bind(this.movieClip_1));
then in the handler function:
function fl_MouseOverHandler_9(evt)
{
// "evt.currentTarget" still represents the event-trigger
// evt.currentTarget.gotoAndStop(41);
//but now you can access the referenced scope with "this"
this.gotoAndStop(41);
}
cheers
mike

How do I give a function to only the second frame of a movie-clip? (AS3)

I want to create a function that only allows the second frame of a movie clip to allow navigation of SWF. The "2button" is the original movie-clip while the "unlock2" is a different movie-clip on the second frame of "2button"
I tried:
//test
public function open(){
2button.unlock2.addEventListener(MouseEvent.CLICK,clickjump2);
function clickjump2(event:MouseEvent) {
gotoAndStop("play2");
}
}
Any suggestions or improvements to this would be greatly appreciated :D!
try this:
function open():void
{
2button.unlock2.addEventListener(MouseEvent.CLICK,clickjump2);
}
function clickjump2(event:MouseEvent):void
{
//check if 2button is at frame 2
if (2button.currentframe==2)
{
gotoAndStop("play2");
}
}
ps: as Versper pointed out, dont start names with numbers

Startdrag is not a function

this.addEventListener(MouseEvent.MOUSE_DOWN,function(e:MouseEvent){this.startDrag(false,null);});
Hi I was wondering why the above doesnt work? Im trying to drag a sprite around screen.
create a sprite on stage, add instance name box, add code to frame one:
box.addEventListener(MouseEvent.MOUSE_DOWN, startMove);
function startMove(evt:MouseEvent):void {
box.startDrag();
}
box.addEventListener(MouseEvent.MOUSE_UP, stopMove);
function stopMove(e:MouseEvent):void {
box.stopDrag();
}
I think your example doesn't work because of the scope of "this" in the event listener handler.
If you remove this.; it will work. It's a scope issue since you use an anonymous function.
You could use the currentTarget of the event, this allows you to make other boxes draggable too, if you add the same listeners.
Note: It is hard to remove an anonymous function as event listener and could cause memory leaks, so the best way is to use a reference to a named function:
box.addEventListener(MouseEvent.MOUSE_DOWN, handleMouseEvent);
box.addEventListener(MouseEvent.MOUSE_UP, handleMouseEvent);
function handleMouseEvent(event:MouseEvent):void
{
switch(event.type)
{
case MouseEvent.MOUSE_DOWN:
{
DisplayObject(event.currentTarget).startDrag();
break;
}
case MouseEvent.MOUSE_UP:
{
DisplayObject(event.currentTarget).stopDrag();
break;
}
}
}

Calling a function from one class, from another

I'm having a slight issue with ActionScript 3 and I have come here to ask for some help.
I have two classes. One called Sledge and one called Sock, there is also the document class called Main.
My issues are as follows:
Inside of Sledge, I call a function that is defined inside of the Main document class. How would I go about telling the class to go to the document class and run that function? Would this also be the same for other classes or just for the document class?
Inside Sledge, I have the following statement: if(hitTestObject(sock.myHitArea)) { /* somecode*/ }
sock is an instance of another seperate class, and by this point has already been created. However when I try and run this I am told it is not defined. How would i go about solving this?
There's some ambiguity issues with how you expressed your question. It would help if you posted a short form of the code for the problem.
However, I'll try to answer the first question:
Inside of Sledge, I call a function that is defined inside of the Main document class. How would I go about telling the class to go to the document class and run that function?
You would want to pass the Main class to the Sledge class or use events which is preferable. If pass the class it will look like this...
class Sledge {
private var main:Main;
function Sledge(main:Main) {
this.main = main;
}
function doSomething():void {
main.runSomeFunction();
}
}
Or if using events:
class Main {
private var sledge:Sledge;
function Main() {
sledge = new Sledge();
sledge.addEventListener("mainDoSomething", doSomething);
}
private function doSomething(e:Event):void {
// .... do stuff
}
}
class Sledge extends EventDispacter {
function Sledge() {
}
public function doSomething():void {
dispatchEvent(new Event("mainDoSomething"));
}
}

Is this the correct way to remove a timer?

I'm not sure if the way I did makes the garbage collector remove the timer. Here are my two functions:
public function newWave() {
var callTimer:Timer = new Timer(800);
callTimer.start();
leftToSpawn = 4;
callTimer.addEventListener(TimerEvent.TIMER,waveCall);
}
public function waveCall(e:TimerEvent) {
leftToSpawn--;
if(leftToSpawn <= 0){
e.target.stop();
e.target.removeEventListener(TimerEvent.TIMER_COMPLETE,waveCall);
}
spawnEnemy();
}
Thanks
To remove an event listener you need to remove it with the exact same signature.
If you do:
.addEventListener(TimerEvent.TIMER,waveCall);
Then you need to use the same event type and function to remove it:
.removeEventListener(TimerEvent.TIMER,waveCall);
Using TimerEvent.TIMER_COMPLETE here will try to remove a listener that doesn't exist, which is silently ignored.
Using target here is ok, for other listener types you may need to use currentTarget, which is always the object the listener got added to. For example in a mouse click event, target could be a child of a MovieClip and without any listeners.
Moreover, timer already has ability to repeat specified number of times and the correct code will be like this:
public function newWave() {
var callTimer:Timer = new Timer(800, 4); //repeat 4 times
callTimer.start();
callTimer.addEventListener(TimerEvent.TIMER,waveCall);
}
public function waveCall(e:TimerEvent) {
spawnEnemy();
}
just change:
e.target.removeEventListener(TimerEvent.TIMER_COMPLETE,waveCall);
to
e.target.removeEventListener(TimerEvent.TIMER,waveCall);