|
-
Senior Member
[help]centering random rocks
I'm having problems with my ramdom rock generater. It makes all the rocks i need but, when I want to center them to get an even rotation it goes out of wack. how can i center the rocks to get the even rotation?
just copy past code in frame to work it
Code:
//total rocks at one time
totalrock = 10;
//rock id number
ir = 0;
//interval that puts out rocks
rocky = setInterval(this, "rockgen", 1000);
function rockgen() {
var done = false;
if (ir>=10) {
ir = 1;
}
while (ir<totalrock && !done) {
ir++;
if (this["rock"+ir] == null) {
done = true;
//create rock
this.createEmptyMovieClip("rock"+ir, ir);
//create rock drawing
this["rock"+ir].createEmptyMovieClip("p", 1);
//Rock drawing
this["rock"+ir].p.beginFill(0x333333, 100);
this["rock"+ir].p.lineStyle(3, 0x666666, 100);
//point x
this["rock"+ir].p.lineTo(0, 0);
//point 1
this["rock"+ir].p.lineTo(ran()+20, ran()+10);
//point 2
this["rock"+ir].p.lineTo(ran()+30, ran()+30);
//point 3
this["rock"+ir].p.lineTo(ran()+20, ran()+50);
//point 4
this["rock"+ir].p.lineTo(0, ran()+60);
//point 5
this["rock"+ir].p.lineTo((ran()+20)*-1, ran()+50);
//point 6 middle
this["rock"+ir].p.lineTo((ran()+30)*-1, ran()+30);
//point 7
this["rock"+ir].p.lineTo((ran()+20)*-1, ran()+10);
//point x
this["rock"+ir].p.lineTo(0, 0);
//Rock drawing end
//center code
this["rock"+ir].p._x = this["rock"+ir].p._width/2;
this["rock"+ir].p._y = this["rock"+ir].p._height/2;
this["rock"+ir]._x = int(Math.random()*400);
//rotation cw or ccw
var chosse = int(Math.random()*2);
switch (chosse) {
case 0 :
this["rock"+ir].rospeed = int(Math.random()*20);
break;
case 1 :
this["rock"+ir].rospeed = int(Math.random()*20)*-1;
break;
}
this["rock"+ir].onEnterFrame = function() {
var t = this;
t._y += 20;
t._rotation += t.rospeed;
if (t._y>Stage.width) {
removeMovieClip(t);
}
};
}
}
}
//retruns a random number
function ran() {
return int(Math.random()*5);
}
this is the code i use to center the rock
Code:
//center code
this["rock"+ir].p._x = this["rock"+ir].p._width/2;
this["rock"+ir].p._y = this["rock"+ir].p._height/2;
CEO OF
-
n00b
Simply add a minus before the half wid pos .
I do stuff that does stuff...
J-Force
-
Pumpkin Carving 2008
Argh, beat me to it... Anything attached or duplicated gets placed at 0,0 so in effect:
this["rock"+ir].p._width/2
is even farther away from 0,0. You need to go negative 1/2 the width for the center of the rock to be at 0,0.
The 'Boose':
ASUS Sabertooth P67 TUF
Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
8GB G.Skill Ripjaws 1600 DDR3
ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
New addition: OCZ Vertex 240GB SATA III SSD
WEI Score: 7.6
-
Senior Member
thanks a lot for the help and for responding so quickly. They don't rotate perfectly but there not doing the orbit thing anymore. thanks to both of you.
CEO OF
-
M.D.
so you want the rocls to rotate around a common center.
first add up all the positions in different components, then divide by the total number of rocks. Record the distance and angle of each rock from this center. Find the angle between the center and the mouse, add the rocks inital angle. Use sin and cos with the distance and angle to move the rocks.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|