A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: [RESOLVED] Floating ball through AS?

  1. #1
    yeah yeah yeah sandyrivers's Avatar
    Join Date
    Jul 2002
    Location
    Jibblies!
    Posts
    480

    resolved [RESOLVED] Floating ball through AS?

    Hi, I don't know why I can't seem to get my head around this, but I'm stuck before I've even begun...

    What I want to do is have a MC of a ball smoothly and randomly float up,down,left and right within a 5 pixel radius of it's original x and y.

    Can someone please help get me started?

    Thanks!
    Sigs R4 Suckers! Wait... I mean.... nevermind...

  2. #2
    |-'|-'|
    Join Date
    Jan 2006
    Posts
    273
    I can make it jitter. Here.
    Code:
    ball_mc.orgx = ball_mc._x;
    ball_mc.orgy = ball_mc._y; // yeah , orgy...
    ball_mc.onEnterFrame = function(){
         this._x = this.orgx + (5 - Math.random()*10);
         this._y = this.orgy + (5 - Math.random()*10);
    }

  3. #3
    yeah yeah yeah sandyrivers's Avatar
    Join Date
    Jul 2002
    Location
    Jibblies!
    Posts
    480
    Quote Originally Posted by Gu35s View Post
    ball_mc.orgy = ball_mc._y; // yeah , orgy...
    lol

    Thanks for the code! How do I make it more relaxed and smooth and less hepped up on caffine?


    Also, can you please explain this line:

    (5 - Math.random()*10);


    I don't understand how 5 minus a random number multiplied by 10 gives me a jittery ball. If I understood how the parameters worked, I'm sure I could tweak them to suit my needs.

    Thanks!
    Sigs R4 Suckers! Wait... I mean.... nevermind...

  4. #4
    Member
    Join Date
    Mar 2007
    Posts
    79
    This isn't my code but could get you started
    Add a MC and call it ball (or change to whatever)
    Put this in frame on main timeline

    var clip:MovieClip = ball;
    var angle:Number = 90;
    var distance:Number = 1;
    var speed:Number = 0.05;

    ball.addEventListener(Event.ENTER_FRAME, motion);

    function motion(event:Event):void {
    ball.y = ball.y + Math.sin(angle) * distance;
    angle += speed;
    }

  5. #5
    |-'|-'|
    Join Date
    Jan 2006
    Posts
    273
    (5 - Math.random()*10); is just randomizing the x and y. Math.random()*10 is randomize a number up to 10 then substract by 5. meaning it should span between -5 to 5 and add that to original _x or _y then set that number as _x or _y. not moving it, but diretly setting it. that's why it's jittery. if you want movement, it's best if you do _x+= something. or just use the code vinchenzison provided. but that code is as3 so you'd have to dumb it down to as2.

  6. #6
    Member
    Join Date
    Mar 2007
    Posts
    79
    Just as another tip, slightly unrelated but has saved me a lot of work in the past.

    http://laco.wz.cz/tween/?page=docs/slideTo

    Using these Tweening prototypes means you only have to write

    mc.slideTo('xvalue', 'yvalue') to get your ball moving.

    It's so simple. You could then create and array of positions and replace the x and y value with a random position from your array.

    Hope that helps. I'm sure the Tweening prototypes will be of use to your furture projects if your not already using them!

  7. #7
    yeah yeah yeah sandyrivers's Avatar
    Join Date
    Jul 2002
    Location
    Jibblies!
    Posts
    480
    Thanks to all the replied! Once I dumbed down vinchenzison code to AS2 it worked like a charm.

    To anyone who does not code in AS3, here is the AS2 version:

    Code:
    var angle:Number = 90;
    var distance:Number = 1;
    var speed:Number = 0.05;
    motion = function () {
    	ball._y = ball._y+Math.sin(angle)*distance;
    	angle += speed;
    };
    ball.onEnterFrame = function() {
    	motion();
    };
    Thanks everyone!
    Sigs R4 Suckers! Wait... I mean.... nevermind...

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