[MX] need help adding array to AS

I’ve been playing with a great code Ilyas developed that waves an image. I’m making an underwater scene and removed the controllers so the image waves all the time.

Trouble is, I have three clips I want to wave at the same time and I could use some explanation about how I could use an array to do this and still position each clip independently.

Any help (Ilyas?) would be appreciated.

Thanks,
JC

http://www.juniorastronauts.com/mermaid3.html

Hey jcs :slight_smile:

What do you want to use an array for exactly?

Hey Ilyas :thumb:

Well, I was thinking that you have
[AS]clip = “vache”[/AS]
in your AS (which I changed to “fig1” in mine) and that if I could make an array that contained “fig1”, “fig2”, and “fig3” and had [AS]clip = thatArray[/AS] that I could apply the rest of the code to all 3 clips.

sounds like I’m off track … ?

Can you post the code you’re refering to? All I could find was this one: http://www.kirupaforum.com/forums/showthread.php?s=&threadid=13149&highlight=lifaros and there’s no vache around :stuck_out_tongue:

here 'tis:

[AS]
/*** Nom de linkage du clip ***/
clip=“vache”;

/*** Constantes ***/
startX=65; // position du coin gauche de l’image
startY=60; // position du coin gauche de l’image
amplitude=2; // amplitude des oscillations - géré par les scrollers
angleIncrement=3; // vitesse des oscillations - géré par les scrollers
periode=9; // période des oscillations
clips=[]; // array qui contiendra tous les clips dupliqués

/*** Fonctions
- init: initialise les variables width et height
- drawMask: dessine un masque de longueur et largeur spécifiée
- oscillate: donne le mouvement oscillatoire à une petite tranche d’image
- splitImage: découpe le clip en un nombre spécifié de petites tranches
/
function init(){
this.createEmptyMovieClip(“container”,0);
container.attachMovie(clip,clip,0);
container._x=startX;
container._y=startY;
width=container._width;
height=container._height;
container.removeMovieClip();
}
function drawMask(targ,width,height){
var mask=targ.createEmptyMovieClip(“mask”,1);
mask.beginFill(0,100);
mask.lineTo(width,0);
mask.lineTo(width,height);
mask.lineTo(0,height);
mask.endFill();
return mask;
}
function oscillate(targ){
targ._y=startY+amplitude
Math.sin(targ.angle/periode);
targ.angle+=angleIncrement;
}
function splitImage(slices){
var j=1;
for (var i=0;i < slices;i++){
clips.push(createEmptyMovieClip(“container”+j,j++));
clips
.attachMovie(clip,clip,0);
clips
._x=startX;
clips*._y=startY;
clips*.angle=i;
var index=width/slices;
var mask=drawMask(clips*,index,height);
mask._x=iindex;
mask._y=0;
clips
.setMask(mask);
clips*.onEnterFrame=function(){
oscillate(this);
}
}
}

/*** Exécute les fonctions ***/
init();
splitImage(30);
[/AS]

I replaced vache with fig1 and changed the linkage ID in the movieclip. --that-- works fine, but I want to extend the code to two other clips so 3 are waving onsreen at the same time.

You know I always ask you the craziest things, Ilyas. :azn:

All right, I don’t have Flash here, so it’s kind of hard to modifiy, but basically, you’ll just have to make a big function that will trigger the init() and the split() functions. The only problem you’ll have is that you’ll have to attach all the clips within the container, and keep track of those containers.

If you don’t manage to make it, I’ll try to make an example for you when I get home :slight_smile:

deal. :wink:

I’ll keep hacking away on it.

Here’s my larger issue: I’m at the point where I can understand AS, take examples apart and piece them back together for my purposes, but it’s not my lingua franca - I can’t just start writing it. Anyone know of a good, solid book or Web resource that can help me over that hump?

Ilyas, look forward to your help!

JCS

i was at the same point a little while ago. What i did is after I did all the tutorials, I did them again without ever looking at the steps. It helps you get a feel for how to create code rather than just understand it. Also try making some basic stuff, sliders, music players, a picture or 2 with the drawing api, and some duplicateMovie animations. It will help.

that’s a good tip. I like it. Thanks grandsp5(-:

anytime

I noticed the movie in the link you posted made my processor usage jump to 68% (AMD 2600). That’s just with one pic.

Nocturn,
thanks for the tip. Maybe I should find a less cumbersome approach …

All right, here you go:

function makeWave (_mc, slices) {
	i++ ;
	var container = init (_mc, i) ;
	trace (container.width);
	splitImage(container, _mc, slices);
}
function init(cl, dep) {
	var container = this.createEmptyMovieClip("container"+dep, dep);
	container.startX = 65;
	container.startY = 60;
	container.amplitude = 10;		
	container.angleIncrement = 3;	
	container.periode = 9;		
	container.clips = [];
	var clip = container.attachMovie(cl, cl, 0);
	container._x = container.startX;
	container._y = container.startY;
	container.width = container._width;
	container.height = container._height;
	clip.removeMovieClip();
	return container ;
}
function drawMask(targ, width, height){
        var mask = targ.createEmptyMovieClip("mask", 1);
        mask.beginFill(0,100);
        mask.lineTo(width,0);
        mask.lineTo(width,height);
        mask.lineTo(0,height);
        mask.endFill();
        return mask;
}
function oscillate(){
	var t = this._parent ;
	this._y = t.startY + t.amplitude * Math.sin(this.angle / t.periode);
	this.angle += t.angleIncrement;
}
function splitImage(cont, targ, slices){
	var j=1;
	for (var i=0;i < slices;i++){
		cont.clips.push(cont.createEmptyMovieClip("container"+j,j++));
		trace (cont.clips*)
		cont.clips*.attachMovie(targ, targ, 0);
		cont.clips*._x = cont.startX;
		cont.clips*._y = cont.startY;
		cont.clips*.angle = i;
		var index = cont.width/slices;
		var mask = drawMask(cont.clips*,index,cont.height);
		mask._x = i * index;
		mask._y = 0;
		cont.clips*.setMask(mask);
		cont.clips*.onEnterFrame = oscillate ;
	}
}
makeWave("vache", 20) ;
makeWave("lait", 20) ;
//... And whatever clip you like...

pom :slight_smile:

Ilyas, you are an animal!

So I was mistaken - I really wanted a function I could apply to several clips instead of an array of clips I dumped into one fcn.

Thanks - I’m going to be able to play with this later today. If it doesn’t toast my processor like Nocturn mentioned, I’ll pass around the link when it’s up.

JCS

=)

I’d love to see the outcome of your work :beam:

it should be pretty funny. gimmie a day or so - work’s jamming me up … :wink:

one more question :}

Ilyas, used your AS and it worked, but all three clips appeared in the same location. I am trying to add to the code so I can control the _x and _y of each clip.

I think I need to control it by adding properties to the imageWave fcn. but I can’t square it quite right. Here’s what I’m thinking:

[AS]
function makeWave (_mc, slices, controlX, controlY) {
i++ ;
var container = init (_mc, i) ;
trace (container.width);
splitImage(container, _mc, slices);
}
function init(cl, dep) {
var container = this.createEmptyMovieClip(“container”+dep, dep);
container.startX = controlX;
container.startY = controlY;
container.amplitude = 5;
container.angleIncrement = 3;
container.periode = 9;
container.clips = [];
var clip = container.attachMovie(cl, cl, 0);
container._x = container.startX;
container._y = container.startY;
container.width = container._width;
container.height = container._height;
clip.removeMovieClip();
return container ;
}
function drawMask(targ, width, height){
var mask = targ.createEmptyMovieClip(“mask”, 1);
mask.beginFill(0,100);
mask.lineTo(width,0);
mask.lineTo(width,height);
mask.lineTo(0,height);
mask.endFill();
return mask;
}
function oscillate(){
var t = this._parent ;
this._y = t.startY + t.amplitude * Math.sin(this.angle / t.periode);
this.angle += t.angleIncrement;
}
function splitImage(cont, targ, slices){
var j=1;
for (var i=0;i < slices;i++){
cont.clips.push(cont.createEmptyMovieClip(“container”+j,j++));
trace (cont.clips*)
cont.clips*.attachMovie(targ, targ, 0);
cont.clips*._x = cont.startX;
cont.clips*._y = cont.startY;
cont.clips*.angle = i;
var index = cont.width/slices;
var mask = drawMask(cont.clips*,index,cont.height);
mask._x = i * index;
mask._y = 0;
cont.clips*.setMask(mask);
cont.clips*.onEnterFrame = oscillate ;
}
}
makeWave(“fig1”, 20, 20, 60) ;
makeWave(“fig2”, 20, 50, 50) ;
makeWave(“fig3”, 20, 100, 60) ;
//… And whatever clip you like…
[/AS]

But that doesn’t work. What am i doing wrong?
JCS

Hi,

I noticed that you pass the control values to the makeWave function but it doesn’t then pass them on to the init function.

Change your call to init to be

var container = init (_mc, i, controlX, controlY) ;

and change init to accept 4 parameters.

function init(cl, dep, ctrlX, ctrlY) {
.
.

    container.startX = ctrlX;
    container.startY = ctrlY;
  .
  .
  .

}

Hope this helps,

Liz

:slight_smile: :slight_smile: :slight_smile: :beam:

thanks liz. worked great. will post the final product tomorrow

I love this board …

JCS

No problem :nerd:

Liz