Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

createEmptyMovieClip 1

Status
Not open for further replies.

mmmbob

Programmer
Jul 23, 2003
29
US
Hi,
I'm trying to create a button dynamically. I'm trying to use a createEmptyMovieClip method to create a "movie clip" and then giving it an onRelease event handler. But this doesn't seem to work.
But when i make a movie, add it to the library and then attach it through attachMovie method, then the onRelease method seems to work.
Any ideas on why i can't do this?
Also, when i create a movie using createEmptyMovieClip and hten loadMovie(jpg) it loads correctly. But when I createEmptyMovieClip and hten specify it's width and hten try the loadMovie method, it doesn't seem to load it....
Any help in either of these issues will be great.
Thanks,
 
an createmptymovie clip is dimensionless by definition so there is nothing 'to click'

only when you use attachmovie to put something in the clip does it has have size and become something that can be clicked

insert>new symbol>movie clip go back to main timeline open library and drag a copy of the empty clip you just made onto the stage...look at its properties 0x0...thats what createemptyclip does in effect....so of course you can then load jpgs., swf into it.

any clearer?
 
Thanks...that makes sense....But how then can I load a picture using loadMovie and make it clickable?
 
like this

myclip = _root.createEmptyMovieClip("bill", 100);
myclip.loadMovie("some.jpg");
_root.onEnterFrame = function() {
if (myclip.getBytesLoaded() &&
myclip.getBytesLoaded()>=myclip.getBytesTotal()) {
myclip._x = 20;
myclip._y = 20;
myclip.onRelease = function() {
trace("clicked");
};
delete this.onEnterFrame;
}
};
 
Great Thanks Bill....
But there is yet anotehr problem :-(
So i tried doing this for N images. SO I had a loop within the onEnterFrame created an onRelease for all the image buttons. But what happended was that the results were really sporadic. Sometiems the images were clickable, sometimes one was clickable and sometimes the other....
Is there any way to make this more reliable?.
Thanks.
 
Also, if i remove the delete statement, it works. But the thing is onEnterFrame gets called over and over and over again. Is there any way to delete it at a later time? BTW, do you have any articles on Flash's garbage collection/ memory management
Thanks
 
you can only attach a function once its all loaded...this a common pain.

if you want a copy ive got a script that extends the movie clip class to allow .onload ( same as loadvars) which neatly gets around this problem.

if you want a copy let me know
 
sure. I'd like to take a look.
I didn't know some of the classes could be extended in flash.
Even if I don't use it I'll atleast learn something cool.
Thanks for all your help .
 
here it is


to use leave in the same folder as the swf

in frame 1 of the flash file make the first command

#include preload.as


anywhere in the flash movie you can now use

myclip.load(..........)
myclip.onLoad = function(){
//do something
}
myclip.onLoading = function () {
var percentLoaded = this.getBytesLoaded() / this.getBytesTotal()
trace ("myclip" + "%" + percentLoaded")
}

makes things so much simpler
 
hey Bill,
I'm unable to go to the URL. btw, do you want me to toss whatever I wrote in the on enter frame(see your 3rd reply) into the on load method?
Thanks.
 
i followed the exact instructions and it doesn't seem to work. I created a movieclip. did a movieclip.load(url);(which didn't work..then i did a load movie and that actually went into the as file)
and then wrote the onload and onloading eventhandlers ( that didn't get called).....but it doesn't seem to work.
First, I don't see a method in your include file called onload or onloading. If you could clarify some more of maybe give an example of an fla, I would really really really appreciate it......I appreciate all the effort to this point as well....thank you very much.
 
quick example

Code:
#include "preload.as"

for(var i =1;i<4;i++){
createEmptyMovieClip(&quot;testMov&quot; + i, i);
clip = eval(&quot;testMov&quot; + i)
with (clip){
	_x = 150 *i
	_y = 100
  lineStyle(1, 0xFF0000, 100);
  beginFill(0xFF0000, 100);
  lineTo(100, 0);
  lineTo(100, 100);
  lineTo(0, 100);
  lineTo(0,0);
  endFill();
};

clip.onLoad = clip.onRelease = function(){ 
  trace(&quot;hello world from -->&quot; + this._name);
}
}
 
Hi mmmBob and Bill,

Sorry to butt into your post but I have been following this interesting thread.

I would like to use this idea to construct a navigation system using only actionscript.

Can I use createtextfield to add text to the clips so they look and act like buttons? If so I think I could use this concept to dynamically recreate my menu depending on what the user does.

My current menu system is cumbersome and adds considerably to the file size. If I try to do this what pitfalls might lie in my way?

Thanks.
 
yes you can easily use a menu

try this

Code:
#include &quot;preload.as&quot;
menuNames = [&quot;Home&quot;,&quot;Contact&quot;,&quot;Sales&quot;]
for(var i =1;i<4;i++){
createEmptyMovieClip(menuNames[i-1], i);
clip = eval(menuNames[i-1])
createTextField(&quot;mytext&quot; + i,100-i,105*i,5,50,15)
myformat = new TextFormat();
myformat.color = 0x000000;
myformat.selectable = false;
_root[&quot;mytext&quot; + i ].text = menuNames[i-1];
_root[&quot;mytext&quot; + i ].setTextFormat(myformat);
with (clip){
    _x = 100 *i
    _y = 0
  lineStyle(1, 0xFF0000, 100);
  beginFill(0xFF0000, 100);
  lineTo(100, 0);
  lineTo(100, 25);
  lineTo(0, 25);
  lineTo(0,0);
  endFill();
};

clip.onLoad = clip.onRelease = function(){ 
  trace(this._name + &quot; button clicked&quot;);
}
}
 
Wonderfull Bill. A menu with a few lines of actionscript.

I hope you don't mind but I have emailed you a file and I will be very grateful if you could give me a few more pointers.

Please don't do it all as I want to use this as both a practical system and a learning exercise.
 
Hi BillWatson & mmmbob,

I would like to ask a big favor. I also need the same kind of functionality with the topic. Can you send me a sample .fla please.

I have a data that is produced by php. I would like to make it a button or movieclip so I can click it after it has been generated.

Please help.

Neil
 
pul the data in with sendandload then in the onload function populate an array with the names (menunames above) and the rest of the code is as is.
 
I'm just coming across this post now, but at this later time the link to the onload extension is invalid. Does anyone have an updated link or can post a new copy? Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top