PDA

View Full Version : How to control MovieClips on events?



justin.c.rounds
April 30th, 2008, 04:03 PM
Forgive me if this has been answered elsewhere -- I've been Googling like mad but can't find a solution to my problem. :(

Here's my code:

package {

import flash.display.*;
import flash.events.*;

public class Gate extends MovieClip {

private var gateButton:MovieClip;

public function Gate() {

var gateButton = addChild(new Hotspot());
gateButton.x = 907;
gateButton.y = 905;
gateButton.width = 235;
gateButton.height = 350;
gateButton.addEventListener(MouseEvent.CLICK, gateButtonClickHandler);

}

private function gateButtonClickHandler(event:MouseEvent):void {

trace("clicked " + this);
this.nextFrame();

}
}
}

The problem is it doesn't go to the next frame if I click the button I created. I can put this.nextFrame() in the main Gate() function and it will go to the next frame, but for some reason it doesn't work in the event handler function, although the trace("clicked") displays "clicked [object Gate]" as expected and I don't get any errors. What am I missing here? TIA

magcius
April 30th, 2008, 04:12 PM
I don't think this is it, but it may be that you are defining gateButton both as a local variable in the constructor, and in the class instance.

Try removing the "var " in front of gateButton in the constructor.

justin.c.rounds
April 30th, 2008, 04:25 PM
Well that gave me an error, I think because of a type mismatch, so I had to change the var type to DisplayObject as well. So now I have:

package {
import flash.display.*;
import flash.events.*;
public class Gate extends MovieClip {
private var gateButton:DisplayObject;
public function Gate() {
gateButton = addChild(new Hotspot());
gateButton.x = 907;
gateButton.y = 905;
gateButton.width = 235;
gateButton.height = 350;
gateButton.addEventListener(MouseEvent.CLICK, gateButtonClickHandler);
}
private function gateButtonClickHandler(event:MouseEvent):void {
trace("clicked" + this);
this.nextFrame();
}
}
}

But it still doesn't work. BTW, "Hotspot" is just a named MovieClip in my library.

magcius
April 30th, 2008, 04:29 PM
Well, to solve the mismatch problem, you need to mess with the code a little:


package {

import flash.display.*;
import flash.events.*;

public class Gate extends MovieClip {

private var gateButton:MovieClip;

public function Gate() {
gateButton = new Hotspot();
addChild(gateButton);
gateButton.x = 907;
gateButton.y = 905;
gateButton.width = 235;
gateButton.height = 350;
gateButton.addEventListener(MouseEvent.CLICK, gateButtonClickHandler);
}

private function gateButtonClickHandler(event:MouseEvent):void {
trace("clicked" + this);
this.nextFrame();
}
}
}


(use codes around ActionScript.)

Also, try turning on strict mode and see if that helps.

agraddy
April 30th, 2008, 04:34 PM
If you are trying to advance the Gate class, why not try dropping this and just make it:
nextFrame();

Alex Lexcuk
April 30th, 2008, 04:35 PM
incomprehension
it's all should works mega good


//My_button_mc.as
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFieldAutoSize;

dynamic public class My_button_mc extends MovieClip
{
var my_text:TextField;
var format:TextFormat;
var MTY:int = -25;//текст по Y
var button_func:Function;
public function My_button_m(my_caption:String,my_func:Function)
{
gotoAndStop(1);
up_mc.buttonMode=true;
up_mc.addEventListener(MouseEvent.ROLL_OVER, on_roll);
up_mc.addEventListener(MouseEvent.ROLL_OUT, on_roll_out);
up_mc.addEventListener(MouseEvent.MOUSE_DOWN, on_down);
up_mc.addEventListener(MouseEvent.MOUSE_UP, on_up);

my_text=new TextField();
mid_mc.addChild(my_text);//
format = new TextFormat();
format.color = 0xffffff;
format.size = 35;
format.font = "Trebuchet MS";

my_text.autoSize = TextFieldAutoSize.CENTER;
my_text.defaultTextFormat = format;
my_text.selectable = false;
my_text.mouseEnabled = true;
my_text.text = my_caption;
my_text.x=-my_text.width/2;
my_text.y=MTY;
button_func=my_func;
}

private function on_roll(evt:MouseEvent):void
{
trace("on");
my_text.y=MTY;
gotoAndStop(2);
}
private function on_roll_out(evt:MouseEvent):void
{
trace("loss");
my_text.y=MTY;
gotoAndStop(0);
}
private function on_down(evt:MouseEvent):void
{
trace("press");
my_text.y=MTY+10;
gotoAndStop(3);
}
private function on_up(evt:MouseEvent):void
{
trace("up");
my_text.y=MTY;
gotoAndStop(0);//works mega good
button_func();
}
}
}


in example

http://kind-armadillo.pochta.ru/FlaAC3/cube_mod.swf
http://kind-armadillo.pochta.ru/FlaAC3/cube_mod.rar

magcius
April 30th, 2008, 04:35 PM
I believe that would make it use the gateButton. Besides, it was shown through the trace that this was resolving to the Gate.

Alex: are you responding to the right thread?

Alex Lexcuk
May 1st, 2008, 03:43 AM
Sorry.

justin.c.rounds
May 1st, 2008, 09:46 AM
Thanks, but still no luck. Changing to/from Strict Mode has no effect, and dropping "this" from "this.nextFrame()" doesn't make a difference either.

Any other thoughts?

justin.c.rounds
May 1st, 2008, 04:06 PM
Oh man... shame on me. It *does* work, except I'm not seeing any change because it's a MovieMaterial on a Papervision object and I didn't update the bitmap.

Oops.

Thanks anyway!