View Full Version : Help with ActionScript
leandroaliaj
08-29-2004, 07:38 PM
Hello everybody,
I am trying to make a system that loads a JPEG when a button is pressed. That is simple. But i have 38 buttons, and the problem is that whenever a new JPEG is loaded, it is not aligned as i want it to. Whatever i do it doesn't seem to align right. I want it to be put in the center of the stage. Here is my code:
//create the buttons
pic1.whichPic = 1;
for(i = 1; i <= 37; ++i){
pic1.duplicateMovieClip("pic" + (i + 1), i);
_root["pic" + (i + 1)]._x = _root.pic1._x + (i * 14.3);
_root["pic" + (i + 1)].whichPic = (i + 1);
}
//this function is called whenever one of the buttons is released
function releace(whichPic){
loadMovie("pictures/recentPaintings/picture" + whichPic + ".jpg", "image");
image._x = horizontalMiddle - (image._width / 2);
}
But this is not working, can anyone find a solution?
Thanks a lot,
Leandro
esiegel
08-30-2004, 12:17 PM
a .jpg file should load with it's top left corner at the origin (0,0) point of the movie clip it is loading into.
By default this point is in the CENTER of the movie clip. Move that point to the top left corner of the movie clip and the .jpg should load correctly.
Hi,
This isn't really too difficult, but there's a couple of things to bare in mind:
1) loadMovie executes last in a statement block, regardless of where you place it, so that what you've actually written is:
function releace(whichPic){
// note the switch of position
image._x = horizontalMiddle - (image._width / 2);
loadMovie("pictures/recentPaintings/picture" + whichPic + ".jpg", "image");
}
meaning that your script is evaluating prior to the image being loaded.
2) You need to wait until the image is loaded before calculating the position. Even if you're loading it off your machine, the actionscript will still execute faster.
The two obvious solutions are onLoad and onData for example:
image.onLoad = function(){
//position code here
}
However, this won't work because loadMovie removes everything in the target movie clip. On top of which, you can't place the onLoad after the loadMovie because, as I said further up the post loadMovie ALWAYS executes last in the statement block.
There's three possible solutions to this, 1) Use prototype (illegal in AS2.0 and complex) , 2) Use setInterval or 3) Use onEnetrFrame to keep checking if the jpg is loaded.
_______________________________________________
So...now I've said that here's what I'd suggest you use:
function releace(whichPic){
loadMovie("pictures/recentPaintings/picture" + whichPic + ".jpg", "image");
_root.onEnterFrame = function(){
if(image.getBytesLoaded()>=image.getBytesTotal){
image._x = (Stage.width/2)-(image._width/2);
delete this.onEnterFrame;
}
}
}
Note that this assumes that you don't currently have an onEnterFrame running on the _root if you do, either put it somewhere else (except on the image mc for the same reasons outlined above for the onLoad) or createEmptyMovieClip() and stick it on there.
If you want the thing centered in the browser window instead of the stage take a look at this tutorial:
http://www.actionscripts.co.uk/tutorials/poab/poab04/stage.html
cheers.
leandroaliaj
08-30-2004, 04:55 PM
esiegel i can't do that because not all JPEGs have the same dimensions.
poab i will try what you said. Note that i did try this way too and it still didn't work:
function releace(whichPic){
var clipLoader = new MovieClipLoader();
var myListener = new Object();
clipLoader.addListener(myListener);
clipLoader.loadClip("pictures/recentPaintings/picture" + whichPic + ".jpg", "image");
myListener.onLoadComplete = function(){
image._x = 290 - (image._width / 2);
}
}
can't understand why
thanks for the help guys i will let you know if it worked
leandroaliaj
08-31-2004, 07:08 PM
poab your code worked fine
Thanks!