Groups | Blog | Home
all groups > flash actionscript > february 2006 >

flash actionscript : AS2 class contruction question


foutuguy
2/4/2006 9:37:15 PM
Hi,

Here is the following dummy class :

class dummy extends MovieClip{

function dummy(param1, param2, param3){
this.createEmptyMovieClip("blah" + param1, this.getNextHighestDepth());
//call the "next" function
next();
}

function next(){
trace("next has been called");
}
}

With the class written like this, next() never gets called. If I put the
next() function outside in the timeline, then it works but this is not what I
want. What did i miss?

Does someone have an answer to this stupid question please?
Any help will be appreciated

Pierrot
skullnz
2/5/2006 12:00:00 AM
You're looking to call "this.next()".

Classes are basically objects with properties and methods. The properties are
variables that last the lifetime of the object (such as "this.name") and the
methods are functions inside that object.

Here's an example of a tidy class:




class dog extends MovieClip {
var bark_loudness = 'loud!';
var fur_colour = 'brown';
var location;

function dog() {
// This function, named the same as the class, is called automatically
upon construction. It is called a constructor.
this.location = 'by the fireplace at home'; // All dogs start at home
this.bark_for_joy();
}

function bark_for_joy() {
trace ('woof!');
}

}
foutuguy
2/5/2006 3:12:10 PM
Thanks for your answer! I simplified my question a bit too much and it appears
to be a bit more complex so Here it is:

In the class attached to this post, I am trying to load a clickable picture.
Since the class extends MovieClip, I thought the created emptymovieclip
containing the picture would have the MovieClip property and that I could write
on the timeline the following:
stop();
var goofy: dog = new dog(_root, 2);
goofy.onRelease() {
trace ('clicked!');
}

But this does not work. The only thing that works is to put a listener(the one
I put in the constructor but i do not like to put that and also too much in a
constructor) and add the onRelease function in there. From this location, I do
not manage to call the "bark_for_joy" function anymore.

So questions are:
1. How can I put the onRelease function on the timeline
2. Technically, with the current class written, How can I call the
"bark_for_joy" function from inside the current onRelease function?

Thanks in advance for the help

Pierrot

:confused;



class dog extends MovieClip {

function dog(target:MovieClip, index:Number) {
// This function, named the same as the class, is called automatically
upon construction. It is called a constructor.
target.createEmptyMovieClip (("pic" + index), 100 + index);
var mcl:MovieClipLoader = new MovieClipLoader();
var listener:Object = new Object();
listener.onLoadInit = function():Void {
_level0.pic2.onRelease = function(){
trace("pictureclicked");
this.bark_for_joy();
}
}
mcl.addListener(listener);
mcl.loadClip("dog.jpg", eval(target + ".pic" + index));
trace(target + ".pic" + index);
this.bark_for_joy();
}


function bark_for_joy() {
trace ('woof!');
}

}

function bark_for_joy() {
trace ('woof!');
}

}
skullnz
2/6/2006 12:00:00 AM
Hmm tricky, partly because I don't entirely follow your question, partly
because I rarely use listeners, so I am a listener noob. I have used them like
this however:



class dog extends MovieClip {
function dog() {
this.onRelease = function() {
this.got_release();
}
}

function got_release() {
// My code here
}
}

Hopefully this explains it a bit better. other options are onMouseDown,
onRollOver, onRollOut; just type
em out until you get a blue one. I'm not saying this is the best way to do it,
but it's simple and it works
for me.

Another possible hint: don't have more than one single frame in your object
timeline. Put the multi-frame
stuff and animations inside OTHER non-class objects. As so:

dog--+ (object that the controlling class is attached to)
|
+-- walk anim
|
+-- blinking eyes anim

Does this make sense?
skullnz
2/6/2006 12:00:00 AM
foutuguy
2/6/2006 12:00:00 AM
Thanks for your answers.

There is something i did not catch. With the last class example you provided,
I do not understand how you can apply it to a movieclip.

Let me explain what i am trying to achieve. My goal is to create a certain
amount of clickable thumb pictures. To create each of them, I am currently:
1. creating an empty movieclip,
2. then I load a jpeg in it
3. and finally i attach an onrelease on it.

I wanted to put at least steps 1 and 2 in a class and I imagine that in the
timeline, when i create an element of type "dog"(var goofy: dog = new dog(); ),
the result "goofy" will be a MovieClip as it is defined in the class (class dog
extends MovieClip).
Then with this MovieClip "goofy" will be useable as a movieclip and i would be
then able to add an onRelease function on it right in the timeline.

That's why I made the following class. In the contructor, I created an empty
MovieClip, loaded a jpeg. So from the timeline, the created element goofy
should be a MovieClip, no? Apparently it is not because no onRelease function
works on it.
Did I do something wrong?I would like to solve this problem so i can
understand better the class concept that I thought i was understanding.

Thanks again for your help

dog.as
-----------
class dog extends MovieClip {

function dog(target:MovieClip, index:Number) {
// create an empty movieclip.
this.createEmptyMovieClip (("pic" + index), 100 + index);
// load the jpeg in the new empty movieclip
var mcl:MovieClipLoader = new MovieClipLoader();
mcl.loadClip("dog.jpg", eval(target + ".pic" + index));
trace(target + ".pic" + index);
this.bark_for_joy();
}


function bark_for_joy() {
trace ('woof!');
}

}

function bark_for_joy() {
trace ('woof!');
}

}

timeline
------------
var goofy:dog = new dog(_root, 1);
goofy.onRelease = function(){
trace("clicked");
}
LuigiL
2/6/2006 12:00:00 AM
Only extend the MovieClip Class when you need all or most of the functionality
of the MovieClip Class. Have a look at the attached code.



// save as Dog.as
class Dog {

//class definition
private var target_mc:MovieClip;
private var depth:Number;
private var dog_mc:MovieClip;
private var mcl:MovieClipLoader;

//class body
//constructor
public function Dog(target:MovieClip,index:Number){
target_mc=target;
depth=index;
mcl=new MovieClipLoader();
//register a listener for this instance of the class
//to listen for the MovieClipLoader events
mcl.addListener(this);
}

//public methods
public function loadPicture(pic:String):Void{
dog_mc=target_mc.createEmptyMovieClip ("pic"+depth,100+depth);
mcl.loadClip(pic,dog_mc);
}

//private methods
private function onLoadInit(target:MovieClip):Void{
dog_mc.onRelease=bark_for_joy;
}

private function bark_for_joy():Void{
trace('woof!');
trace(this);//here the keyword 'this' refers to the dog_mc movieclip
}
}

//in the fla
var test:Dog=new Dog(this,10);
test.loadPicture("dog.jpg");
James Fee
2/6/2006 9:34:41 AM
I am not sure this will work, but you could give it a try.

in your dog.as file at the top, import mx.events.EventDispatcher and
initialize the event dispatcher in your object constructor. In your class
catch the onRelease event and dispatch it out again. It might work.



dog.as
-----------
import mx.events.EventDispatcher;
class dog extends MovieClip {

// Methods used by the EventDispatcher
public var addEventListener:Function;
public var removeEventListener:Function;
// method to trigger events
private var dispatchEvent:Function;


function dog(target:MovieClip, index:Number) {
// initialize the EventDispatcher
EventDispatcher.initialize(this);
// create an empty movieclip.
this.createEmptyMovieClip (("pic" + index), 100 + index);
// load the jpeg in the new empty movieclip
var mcl:MovieClipLoader = new MovieClipLoader();
mcl.loadClip("dog.jpg", eval(target + ".pic" + index));
trace(target + ".pic" + index);
this.bark_for_joy();
}

function onRelease(){
var EventObj:Object = new Object();
EventObj.type="onRelease";
EventObj.target=this;
dispatchEvent(EventObj);
}

function bark_for_joy() {
trace ('woof!');
}

}



timeline
------------
var goofy:dog = new dog(_root, 1);
goofy.onRelease = function(){
trace("clicked");
}



--
Jim Fee
Viking Electronic Services
jfee (at) vikinges (dot) com

foutuguy
2/6/2006 10:28:53 PM
LuigiL,
This is exactly what i was looking for. Now I understand how to do do this.
Now i can split my constructor and clear up my code. Thank you very much for
your help! I really appreciate it.

Jim,
I am quite interested in your code too as I tryed a few wekks ago to make the
same working with evendispatcher and I got so bothered that I abandoned. I
tried your code. It compiled well with just a few minor changes. However, the
pic does not load. i guess i need to look at it again after a good sleep but
this helps too. Thanks.

Thank y'all so much for your help!

Pierrot :)
LuigiL
2/7/2006 12:00:00 AM
An onRelease already is an event so why use EventDispatcher. This class is used
to create custom events. See attached example in the Dog class.



import mx.events.EventDispatcher;

class Dog {

//class definition
private var target_mc:MovieClip;
private var depth:Number;
private var dog_mc:MovieClip;
private var mcl:MovieClipLoader;

public var addEventListener:Function;
public var removeEventListener:Function;
private var dispatchEvent:Function;

//class body
//constructor
public function Dog(target:MovieClip,index:Number){
EventDispatcher.initialize(this);
target_mc=target;
depth=index;
mcl=new MovieClipLoader();
//register a listener for this instance of the class
//to listen for the MovieClipLoader events
mcl.addListener(this);
}

//public methods
public function loadPicture(pic:String):Void{
dog_mc=target_mc.createEmptyMovieClip ("pic"+depth,100+depth);
mcl.loadClip(pic,dog_mc);
}

public function BarkNow():Void{
trace("Woof from EventDispatcher");
}

//private methods
private function onLoadInit(target:MovieClip):Void{
dog_mc.onRelease=bark_for_joy;
//dispatch an event now that the picture is fully loaded
dispatchEvent({target:dog_mc,type:"Bark"});
}

private function bark_for_joy():Void{
trace('woof!');
trace(this);//here the keyword 'this' refers to the dog_mc movieclip
}
}

//In the fla:
var test:Dog=new Dog(this,10);
//new listener object to catch our event 'Bark'
var woof_listener:Object=new Object();
woof_listener.Bark=function(evtObj:Object):Void{
//call a method in the class
test.BarkNow();
}
//register the listener
test.addEventListener("Bark",woof_listener);
//load the picture and trigger the event
test.loadPicture("dog.jpg");
foutuguy
2/7/2006 12:58:47 PM
Thanks!

I did not want to use the eventdispatcher to replace the onrelease but to
dispatch in the timeline an event once a thumb is loaded(i now have a better
solution) and also to do the following. On the site i am making, I have a
country flag icon on which I click to change the language. Because I have
multiple swf files loaded in the main, i wanted to put a listener in all those
swf. the 'changeLanguage' event would have triggerred a language update in
every single swf instead of calling separately every single update function.
Thanks again for the example, i'll look into it to understand better what that
works

AddThis Social Bookmark Button