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.
They can be easily customized to meet the unique requirements of your project.















(3 votes, average: 4.67 out of 5)









This post has 26 comments
Luiz Fernando
Yeah! I have an cool idea for a game!
I may send to you as soon as possible!
Ed
Cosmic Trail 2, a new game coming up, has this code, except that the rotation has ease. :P
Gabriel
very good….
Shiv
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.
Shiv
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()?
styxtwo
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++
}
}
}
styxtwo
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++
}
}
}
styxtwo
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
emanuel
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
RJ
I agree with him
Ciaren Coleman
I was making a game like this based on the mochiads header. But it was in AS2
RJ
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…
Shiv
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?
Emanuele Feronato
Made more than 100,000 impressions during the w/e at an almost 0.70 cpm
RJ
100,000 impressions!!
Can I ask which page gave you the majority of those impressions?
Thanks
Emanuele Feronato
mindjolt.com
RJ
Thanks a lot again
styxtwo
mindjolt rules :D
Jerry
Love it. Already used it and am about half way through a new game. Thanks!
Chrispy
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?
EagleVision
Yes, the Grow series is great!
Also, make more things like this, its great!
Vagroth
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.
Vagroth
oh snap i said with not will XD
el nino
How can you add ease to the rotation?
kara
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!
Dragonphase
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.