PDA

View Full Version : How do you control loadMovie placement? Or to inherit..


WizyWyg
07-12-2005, 10:09 PM
How do you control loadMovie placement? Or to inherit the placement of the Movie Clip its loaded into?


Need help on Random placement and loadMovie into a MC

I have 5 Movie Clips on the Stage with this code on each:
onClipEvent (load) {
this._x = random(665);
this._y = random(400);
}

so it basically places each MC randomly on the stage.

In each of these movie clips there is a a "loadMovie" command in the first frame:
loadMovie("shell_03.swf", 3);

But as you know, this will only place the swf in the top corner of the stage, not following the directions of the random placement of the movie clip its loaded into.

how can I control the loadMovie to follow the random placement of the Movie Clip its being loaded into?

pixelwit
07-12-2005, 10:31 PM
Change your code on frame1 to something like this:this.loadMovie("shell_03.swf");-PiXELWiT
http://www.pixelwit.com

WizyWyg
07-12-2005, 10:49 PM
Hi thanks for your prompt reply, however, Im running into a problem when I do use that.

In the SWF file that is being loaded via the LoadMovie command, I have a Movie Clip with the instance name of "shell2" that has a mask on it, so that when someone runs their mouse over it, it will slowly reveal itself ( like scratching away to reveal an item).
The code is as follows:

onClipEvent (load) {
_root.createEmptyMovieClip('squareu', 2);
function drawSpace() {
x = _root._xmouse;
y = _root._ymouse;
with (_root.squareu) {
//clear();
moveTo(x-15, y-15);
beginFill(0x000088);
lineTo(x+15, y-15);
lineTo(x+15, y+15);
lineTo(x-15, y+15);
endFill();
}
}
_root.shell2.setMask(_root.squareu);
}
onClipEvent (mouseMove) {
drawSpace();
updateAfterEvent();
}


When I do use the
this.loadMovie ("file.swf")

The "blue" mask is showing up which is wrong.

When I use the:
loadMovie ("file.swf", 1)

Then it works correctly.

WizyWyg
07-12-2005, 11:31 PM
And also, how do you prevent overlapping of the random placed movieClips?

pixelwit
07-13-2005, 12:02 AM
As to why your clip stops working when you load it into another clip, check this thread (http://www.actionscript.org/forums/showthread.php3?t=78095).

And also, how do you prevent overlapping of the random placed movieClips?Don't place them randomly. :D Or you can use "hitTest".

-PiXELWiT
http://www.pixelwit.com

WizyWyg
07-13-2005, 12:28 AM
Thanks for that reference to the other thread; its now working out.

Now, my stage is 765 x 500 and each of the SWF files are 250w x 200 h.
Some of my 'random' placements is being put off the edges, so of course this is not working correctly. How do i get it so that it knows not to place the randomp laced movie clips outside of the stage area?

WizyWyg
07-13-2005, 01:33 AM
Or you can use "hitTest".


do you have an example of how this works? I can't find anything useful in the tutorial section.

creynders
07-13-2005, 12:24 PM
http://livedocs.macromedia.com/flash/mx2004/main_7_2/00001516.html

WizyWyg
07-13-2005, 06:44 PM
thanks, but could someone provide somehting in English maybe?
that talks about dragging an object/movie clip. Just need something that would detect the "edges" of my stage (so the mc's do not go over it) and how to detect each other so they dont overlap.

red penguin
07-13-2005, 06:57 PM
place the movies anywhere to the left of 300 (_x) and you won't have that problem.

WizyWyg
07-13-2005, 07:07 PM
Huh? Im not following you.

red penguin
07-13-2005, 09:20 PM
my bad...with those dims, you can have a max of 6 clips on the Stage. Is this what you want? If not, how many *do* you want at any given time?

It's pretty easy...

Load a movie..once loaded, check to see if the x position plus the width are greater than the Stage width. If so move it over to the left whatever the difference is...

Place another clip. Check to see if it is 'hitting' something on the Stage. If so, try again...

continue...

Not sure what the overall effect is...

WizyWyg
07-13-2005, 09:55 PM
Basically there will be a total of 5 movie clips
Instances of shells0 - shells4.

Randomly placed on the stage, do not overlap each other and stay within the stage boundaries.

Sorry, Im bad with actionscript from scratch, if you could provide example it will be easier for me to understand.

red penguin
07-13-2005, 09:59 PM
Sorry dude...EOD and personally I despise working with hitTest....

However, given that there are 5 and the max is six...doesn't leave much room for hte 'randomness', does it?

WizyWyg
07-13-2005, 10:01 PM
doesn't matter about room

red penguin
07-13-2005, 10:05 PM
uhm...sure it does....

Basic math tells us that we can only have a max of them clips on the stage at any one given time. If you'd like 5 of them, then I would place them in a 'grid' but jig them slightly....

Yeah...grid code would do nicely for that....layout but move them slightly...

WizyWyg
07-13-2005, 10:12 PM
it really doesn't matter. no need for grid. the size of each movie clip may change to be smaller later. Im just trying to understand how hitTest can work.

red penguin
07-13-2005, 10:18 PM
ok..then do this:hitTest Tutorial (http://actionscript.org/tutorials/beginner/Movement_and_simple_hittest/index.shtml) to get a grip on how to work with hitTest...

WizyWyg
07-13-2005, 10:21 PM
ok..then do this:hitTest Tutorial (http://actionscript.org/tutorials/beginner/Movement_and_simple_hittest/index.shtml) to get a grip on how to work with hitTest...


Im not trying to move an object. its remaining "static" on the stage. I want the script to randomon place items "statically" but not overlap or go over the stage boundaries.

red penguin
07-13-2005, 10:29 PM
I understand that you are not trying to move an object. But you are trying to place an object and see if it is hitting something. There is code there that you can use...namely, learning how to use hitTest. I've been there. I've asked the same questions. Have you done the tute?

Learn how to use hitTest and then post some code when things go awry...

That's how it works...youknow...learning....try, fail, try, fail....try again...then ask for help...

WizyWyg
07-13-2005, 10:32 PM
That's how it works...youknow...learning....try, fail, try, fail....try again...then ask for help...

I AM at this point. Its not working, I dont know how hitTest is supposed to work, and my random placement is the only thing that is working? i've done all the tutes on hitTest and I still dont understand it how it supposed to work.

Where do I put the hitTest code? On a Frame? on the movieClip?
How do I tell it that there are 5 movie clips on the stage so they dont overlap?
How do i tell it to detect the edges of the stage?

These are questions that I can't find answers to.

red penguin
07-13-2005, 10:39 PM
Good. You are asking questions...that's GOOD!

It depends on how you are attacking this..
a) I am doing a file and my thinking is to place the hitTest on the clip itself...
b) hitTest will do that
c) attaching the clip and placing the _x anywhere between 0 and the Stage.width minus the clip._width

WizyWyg
07-13-2005, 10:55 PM
so again how? Example? I can't do actionscripting from scratch (i can't visualize the code. i pretty much learn "backwards" by seeing the code and then breaking it down to see why it does what it does. That's just the way I work things out).

Right now the 5 MovieClips (instances shells0-shells4) are just "sitting" on the stage.
Need to randomly place them anywhere on the stage, without overlapping and going across the stage boundaries.
each MC is 250 x 200 pixels

What code do i use? Where does it get placed?

red penguin
07-14-2005, 02:12 AM
Show me some code that randomly places clips on the stage. I'm not going to just "do" this for you without any thinking or effort on your part, my fine friend. (Please don't take this the wrong way...)

Interestingly enough...this is a *very* difficult thing to do. So difficult in fact that there are competitions (http://www.kuro5hin.org/story/2002/5/24/171054/160) dedicated to it as well as papers written about algorithms to do this very task.

Therefore, I have decided to give this a whirl. I have even brought in other people in on it and they, too, find it interesting. Hell. I say we (or rather you since this is your thread) should start a Flash competition for this! In fact, with your permission, I would like to start another thread with which I will link to this as well as post some code as a starting point!

It is an interesting thing to try and figure out!!!

My File
To sum (as it's 10 pm here): With 100 iterations, the number of clips runs from 1 - 4 randomly placed and never touching. The average is 3 however 4 isn't uncommon. Discovering the 'available' space with one clip on the stage randomply placed...fine...then finding the area left with 2? Then 3?

So separate algorithms are needed as well as recursiveness....

I have to say, thanks for opening this up. As stated...interesting (in that dorky kinda way).

WizyWyg
07-14-2005, 03:26 AM
^_^. after moving away from this, and coming back to it, I sat with a friend who helped me figure it out some. The size of each MC is now only 100 x 100 pixels, so that eliminates any problems that would have been generated with larger mc's.

competition? :p If you like, you can if you want to.

However, if its "dynamically" placed items through action scripting that one would write code to do so, then I can see the worth in trying to come up with something to handle that.

Unfortunately, my MC's were placed on the stage and are then controlled by AS for placement as well as preventing of overlapping and making sure they dont go off the stage. As it is, this also means that I will have to modify "more" if I should add more items to the stage/library.

This is what my friend an I came up with.
On each movieClip ( instances of shells0-shells5)
we have this code:

onClipEvent (load) {
var width = 765; //width of stage
var height = 500; // height of stage
var halfw = this._width / 2;
var halfh = this._height / 2;

do {
do {
this._x = random(width);
} while (this._x < halfw || this._x > width - halfw);
do {
this._y = random(height);
} while (this._y < halfh || this._y > height - halfh);
} while (_root.overlapTest(this));

this._loaded = true;
}

In the first frame of the file:

stop();

_global.shellTotal = 5;

function overlapTest(movieClip)
{
var i;
var mc;
var overlaps;
var nitems;

overlaps = false;
nitems = _global.shellTotal + 1;
for (i=0; i<nitems; i++)
{
mc = _root["shells"+i];
if (mc._loaded || i > _global.shellTotal - 1)
{
//trace("mc: " + mc + " this: " + this);
if (mc != movieClip && movieClip.hitTest(mc))
{
overlaps = true;
}
}
}

return overlaps;
}


So now we have random placement as well as no overlapping of the items and not going over the boundaries of the stage. However, if I should add more items, that will add up to being larger than the stage, it goes into an endless loop.

Interesting enough, I needed one of the items to remain static, so the movieClip with the instance of shells5 has this action script on it:

onClipEvent (load) {
this._loaded = true;
}



If someone can come up with a more efficient way of doing the same thing, Im all "eyes" .

This is all in combination with first to scratch and reveal an item, clicking on it, and once all items are revealed, will then be hidden again, going to start of the movie and randomly "placing" the items on the stage again.

red penguin
07-14-2005, 03:16 PM
MC's were placed on the stage and are then controlled by AS for placement
Why place them on the stage at all if you are then moving them around randomly?

I think that solution above is interesting. Not the way I've approached this problem however if it works we are proving (once again) that "there is more than one way to skin a cat."

Yes. I think I will take this as a launch pad for "something". Thanks for sparking my interest. Once I get something solid, I will post a link in this thread.

<ramble>
What makes this so interesting (in my mind) is that I think we can all agree that using brute force to do this will work....Adding 100 clips and seeing how many remain from those 100 (as I've built a test file to show this) usually yields 3-4 clips. Sometimes 2 and even one single clip remains!

One is not a solution (given the surface area is large enough for the polygon). Two *could be a solution* but again, is rare. In all the times I've hit CTRL + ENTER, only ONCE did I populate the screen with 5 instances. And never did I see 6. This *is* of course a solution but the probability is rare.
</ramble>

WizyWyg
07-14-2005, 07:11 PM
Why place them on the stage at all if you are then moving them around randomly?

because I dont know how to do it any other way?

If you know how to pull mc's (with mc's within them) please let me know, otherwise, im more of a "wysiwyg" type of person (meaning if its there, I know i can do something with it' if its not there, then i cant use it)

red penguin
07-14-2005, 07:32 PM
You don't know how to attach something to the stage dynamically?

for(var i:Number = 0; i < 10; i++)
{
var b:MovieClip = this.attachMovie("box", "b_" add String(i), i);
b._x = Math.floor(Math.random() * 1024);
b._y = Math.floor(Math.random() * 768);
}

In the most basic sense, the above attaches a movie ("box") to the stage and places it randomly. No boundary check...

WizyWyg
07-14-2005, 08:10 PM
You don't know how to attach something to the stage dynamically?

for(var i:Number = 0; i < 10; i++)
{
var b:MovieClip = this.attachMovie("box", "b_" add String(i), i);
b._x = Math.floor(Math.random() * 1024);
b._y = Math.floor(Math.random() * 768);
}

In the most basic sense, the above attaches a movie ("box") to the stage and places it randomly. No boundary check...


okay, but I have a mc within each of the mc on the stage. And thos mc's within have AS on them to control the masking so they appear "invisible" and then are revealed as a mouse is slowly 'scratched' over them.

the above doesn't however account for "placement" of a one of the mc's needing to be statically placed in one location.

red penguin
07-14-2005, 08:16 PM
No boundary check...
That's correct. I thought you'd be able to figure that one out.

Again....if your solution works, then use it. I am merely pointing out how to use attachMovie()...

WizyWyg
07-14-2005, 08:21 PM
^^ um, that wasn't me. you just answered yourself there ^_^

however, attachMovie is nice, but within MC's within MC"s needing their own script based on the levels they are loaded into ( if two mc's occupy the same level, the script doesn't work). how would one accomodate for that?

I really dont like doing things to AS because of the inability "to see" what is happening with the items. Relying totally on AS to control everything, make me confused, which is why, im just rely on basic, if i see it, then i do this, and it works, great.