Moving an object like in MochiAds header

In the post Wall Dodge - game made from Metro Siberia Underground tutorial I was asked

Emanuele, do you know how to make the follow mouse function that that the sprite uses in mochiads header?

Here it is... it's so simple I won't comment it, just play with the delay variable to adjust the spring factor

ACTIONSCRIPT:
  1. Mouse.hide();
  2. delay=10;
  3. _root.attachMovie("mousep","mousep",_root.getNextHighestDepth());
  4. _root.attachMovie("mochithing","mochithing",_root.getNextHighestDepth());
  5. mousep.onEnterFrame = function() {
  6.     this._x = _root._xmouse;
  7.     this._y = _root._ymouse;
  8. };
  9. mochithing.onEnterFrame = function() {
  10.     dist_x = mousep._x-this._x;
  11.     dist_y = mousep._y-this._y;
  12.     distance = Math.sqrt(dist_x*dist_x+dist_y*dist_y);
  13.     angle = Math.atan2(dist_y, dist_x);
  14.     speed = distance/delay;
  15.     xspeed = speed*Math.cos(angle);
  16.     yspeed = speed*Math.sin(angle);
  17.     this._x += xspeed;
  18.     this._y += yspeed;
  19.     this._rotation = angle*180/Math.PI
  20. };

Here it is the result

Can you make a game starting from this code? Download the source.

Improve the blog rating this post
Tell me what do you think about this post. I'll write better and better entries.
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

» Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.

24 Responses to “Moving an object like in MochiAds header”

  1. Luiz Fernando on January 26th, 2008 5:13 pm

    Yeah! I have an cool idea for a game!
    I may send to you as soon as possible!

  2. Ed on January 26th, 2008 8:02 pm

    Cosmic Trail 2, a new game coming up, has this code, except that the rotation has ease. :P

  3. Gabriel on January 26th, 2008 10:27 pm

    very good….

  4. Shiv on January 27th, 2008 8:19 am

    Hey great work Emanuele, but I figured it out before you and My one involves less trigonometry.

    *****************************************
    delay = 15
    sprite..onEnterFrame = function() {
    this._x -= dist_x/delay;
    this._y -= dist_y/delay;
    dist_x = this._x-_root._xmouse-4;
    dist_y = this._y-_root._ymouse-4;
    angle = -Math.atan2(dist_x, dist_y);
    this._rotation = angle/(Math.PI/180);
    }
    ******************************************

    Works just the same.

  5. Shiv on January 27th, 2008 8:25 am

    Can anyone tell me how to calculate the no. of objects when they enter a specific area in the movie?
    Do we have to use getBounds()?

  6. styxtwo on January 27th, 2008 3:44 pm

    Can anyone tell me how to calculate the no. of objects when they enter a specific area in the movie?
    Do we have to use getBounds()?

    you can use something like this:

    onClipEvent(enterFrame){
    if(this._x_root.left_x_bound){
    if(this._y_root.up_y_bound){
    _root.amountofobjectsinbound++
    }
    }
    }

  7. styxtwo on January 27th, 2008 3:46 pm

    wow the code got messed up there i meant this code:

    onClipEvent(enterFrame){
    if(this._x _root.left_x_bound){
    if( this._y > _root.up_y_bound &&
    this._y< _root.up_y_bound){
    _root.amountofobjectsinbound++
    }
    }
    }

  8. styxtwo on January 27th, 2008 3:48 pm

    wow something weard is going on, i’m sure i typed the right code :S it seems to mess up or something :S.

    sorry for the triple post xD

  9. emanuel on January 28th, 2008 2:14 am

    hi emanuele can u make a tut like Flash game creation tutorial but with AS 3.0 very basic beacuse i dont know were to learn

  10. RJ on January 28th, 2008 11:16 am

    I agree with him

  11. Ciaren Coleman on January 28th, 2008 11:28 am

    I was making a game like this based on the mochiads header. But it was in AS2

  12. RJ on January 28th, 2008 11:50 am

    Emanuele, how did Glomb manage to earn $90 in a few days?

    On thursday or friday, its earnings were around $2, and now suddenly, $92…

  13. Shiv on January 28th, 2008 12:08 pm

    Thanx Styxtoo, but I solved the problem using 4 hitTests. If 4 hits happen then flag is true and counter++.

    Yeah, I agree with Rj that how did Stack reach $92?

  14. Emanuele Feronato on January 28th, 2008 12:34 pm

    Made more than 100,000 impressions during the w/e at an almost 0.70 cpm

  15. RJ on January 28th, 2008 12:39 pm

    100,000 impressions!!

    Can I ask which page gave you the majority of those impressions?

    Thanks

  16. Emanuele Feronato on January 28th, 2008 1:02 pm

    mindjolt.com

  17. RJ on January 28th, 2008 3:39 pm

    Thanks a lot again

  18. styxtwo on January 28th, 2008 4:53 pm

    mindjolt rules :D

  19. Jerry on January 28th, 2008 5:54 pm

    Love it. Already used it and am about half way through a new game. Thanks!

  20. Chrispy on January 28th, 2008 10:02 pm

    Hi, i’m a new visitor, and i wondered if you had any idea of how to make games of the ‘Grow’ series from Eyezmaze. If so, could you make a tutorial about it?

  21. EagleVision on February 15th, 2008 12:06 am

    Yes, the Grow series is great!

    Also, make more things like this, its great!

  22. Vagroth on April 26th, 2008 6:51 pm

    Is it possible to use this code for a hand, only in a way that the hand cant leave an area, those who have played the Thing Thing flash game series with understand what I mean.

  23. Vagroth on April 26th, 2008 6:53 pm

    oh snap i said with not will XD

  24. el nino on May 6th, 2008 9:38 am

    How can you add ease to the rotation?

Leave a Reply