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 ...
If you found this post useful, please consider a small donation.
» 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.

25 Responses

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

  2. Ed says:

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

  3. Gabriel says:

    very good….

  4. Shiv says:

    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 says:

    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 says:

    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 says:

    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 says:

    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 says:

    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 says:

    I agree with him

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

  12. RJ says:

    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 says:

    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 says:

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

  15. RJ says:

    100,000 impressions!!

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

    Thanks

  16. Emanuele Feronato says:

    mindjolt.com

  17. RJ says:

    Thanks a lot again

  18. styxtwo says:

    mindjolt rules :D

  19. Jerry says:

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

  20. Chrispy says:

    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 says:

    Yes, the Grow series is great!

    Also, make more things like this, its great!

  22. Vagroth says:

    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 says:

    oh snap i said with not will XD

  24. el nino says:

    How can you add ease to the rotation?

  25. kara says:

    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!

Leave a Reply