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

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

Here it is the result

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

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 4.67 out of 5)
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.
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 26 comments

  1. Luiz Fernando

    on January 26, 2008 at 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 26, 2008 at 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 26, 2008 at 10:27 pm

    very good….

  4. Shiv

    on January 27, 2008 at 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 27, 2008 at 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 27, 2008 at 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 27, 2008 at 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 27, 2008 at 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 28, 2008 at 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 28, 2008 at 11:16 am

    I agree with him

  11. Ciaren Coleman

    on January 28, 2008 at 11:28 am

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

  12. RJ

    on January 28, 2008 at 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 28, 2008 at 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 28, 2008 at 12:34 pm

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

  15. RJ

    on January 28, 2008 at 12:39 pm

    100,000 impressions!!

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

    Thanks

  16. Emanuele Feronato

    on January 28, 2008 at 1:02 pm

    mindjolt.com

  17. RJ

    on January 28, 2008 at 3:39 pm

    Thanks a lot again

  18. styxtwo

    on January 28, 2008 at 4:53 pm

    mindjolt rules :D

  19. Jerry

    on January 28, 2008 at 5:54 pm

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

  20. Chrispy

    on January 28, 2008 at 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 15, 2008 at 12:06 am

    Yes, the Grow series is great!

    Also, make more things like this, its great!

  22. Vagroth

    on April 26, 2008 at 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 26, 2008 at 6:53 pm

    oh snap i said with not will XD

  24. el nino

    on May 6, 2008 at 9:38 am

    How can you add ease to the rotation?

  25. kara

    on March 23, 2009 at 11:39 pm

    I REALLY NEED HELP!
    this is exactly what i have been looking for for acouple months now,and i dont know hwo to do it/: ill do anything for the info!,please!

  26. Dragonphase

    on December 17, 2011 at 5:09 pm

    Hi Emanuele – I am sure this attempt and tutorial is quite old, but this is possible with only a few lines of codes:

    Mouse.hide();
    attachMovie(“mousep”,”mousep”,_root.getNextHighestDepth());
    attachMovie(“mochithing”,”mochithing”,_root.getNextHighestDepth());
    var Delay = .1;

    mochithing.onEnterFrame = function () {
    this._x += (mousep._x – this._x) * Delay;
    this._y += (mousep._y – this._y) * Delay;
    this._rotation = Math.atan2(mousep._y-this._y, mousep._x-this._x)*180/Math.PI;
    }

    Delay is returned slower when the value of it is decremented by 0.01.