A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: [help]centering random rocks

  1. #1
    Senior Member zervell's Avatar
    Join Date
    May 2004
    Posts
    259

    [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

  2. #2
    n00b LeechmasterB's Avatar
    Join Date
    May 2004
    Location
    Switzerland
    Posts
    1,067
    Simply add a minus before the half wid pos .
    I do stuff that does stuff...

    J-Force

  3. #3
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,377
    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

  4. #4
    Senior Member zervell's Avatar
    Join Date
    May 2004
    Posts
    259
    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

  5. #5
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    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.
    lather yourself up with soap - soap arcade

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center