Moving an object like in MochiAds header
Talking about Flash and Game development.
Learn cross platform HTML5 game development
Check my Gumroad page for commented source code, games and books.
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
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.
Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.