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:
-
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.
Improve the blog rating this post
Tell me what do you think about this post. I'll write better and better entries.
Tell me what do you think about this post. I'll write better and better entries.
» 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.
They can be easily customized to meet the unique requirements of your project.
24 Responses to “Moving an object like in MochiAds header”
Leave a Reply

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