Salut les loulous, en me baladant sur le net, je suis tombé sur une source vraiment sympa qui vous permettra d'ajouter de l'inertie avec détection de collision facilement paramétrable. Le rendu est vraiment très impressionnant, voici donc le résultat.
Appercut:
inertialDrop
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Utilisation:
Il vous suffit de créer un clip placé seulement dans la bibliothéque, faite une liaison>>exporter pour actionscript et de le nommer "balle". Si vous souhaitez modifier le nom de votre clip, il vous suffira de changer cette ligne
modifiez comme ceci:
Le code:
Appercut:
inertialDrop
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Utilisation:
Il vous suffit de créer un clip placé seulement dans la bibliothéque, faite une liaison>>exporter pour actionscript et de le nommer "balle". Si vous souhaitez modifier le nom de votre clip, il vous suffira de changer cette ligne
- Code:
this.attachMovie("balle","ball1",0,{_x:200,_y:200})
modifiez comme ceci:
- Code:
this.attachMovie("nouveauNomDeClip","ball1",0,{_x:200,_y:200})
Le code:
- Code:
//------------------------prototype------------------------------------------------
MovieClip.prototype.setInertialDrop=function(b:Boolean){
if(b){
this._$startDrag=this.startDrag;
this._$stopDrag=this.stopDrag;
//-----------------------------------------------------------------------------------
this._$newLoc=new Object();
this._$oldLoc=new Object();
this._$newSpd=new Object();
this._$oldTime=0;
this._$newTime=0;
this._$laps=30;//1000/24;
this._$k=-2;//default value, see stopDrag arguments to overwrite
this._$spd=new Object();
this._$loc=new Object();
this._$backup=function(){
this._$oldLoc.x=this._$newLoc.x;
this._$oldLoc.y=this._$newLoc.y;
this._$oldTime=this._$newTime;
}
this._$actualize=function(){
this._$newLoc.x=this._x;
this._$newLoc.y=this._y;
this._$newTime=getTimer();
}
this._$scanDrag = function () {
this._$backup();
this._$actualize();
}
this._$slipDrop=function(){
this._$time+=this._$laps/1000;
var newx:Number=(k==0)?this._$spd.x*this._$time+this._$loc.x:(this._$spd.x/this._$k)*Math.exp(this._$k*this._$time)+this._$loc.x-this._$spd.x/this._$k;
var newy:Number=(k==0)?this._$spd.y*this._$time+this._$loc.y:(this._$spd.y/this._$k)*Math.exp(this._$k*this._$time)+this._$loc.y-this._$spd.y/this._$k;
if(this._$boundary){
if(newx<this._$minX) newx=2*this._$minX-newx;
if(newy<this._$minY) newy=2*this._$minY-newy;
var mx:Number=Math.floor((newx-this._$minX)/(this._$maxX-this._$minX));
if(mx%2==0) newx=newx-mx*this._$maxX+mx*this._$minX;
else newx=(mx+1)*this._$maxX-(mx-1)*this._$minX-newx;
var my:Number=Math.floor((newy-this._$minY)/(this._$maxY-this._$minY));
if(my%2==0) newy=newy-my*this._$maxY+my*this._$minY;
else newy=(my+1)*this._$maxY-(my-1)*this._$minY-newy;
}
this._x=newx;
this._y=newy;
if(Math.pow(newx-this._x,2)+Math.pow(newy-this._y,2)<Math.pow(10,-5)) clearInterval(this._$intID2);
}
this.startDrag=function(){
//in fact, same signature than native overwrited function
clearInterval(this._$intID2);
this._$newLoc.x=this._x;
this._$newLoc.y=this._y;
this._$oldLoc.x=this._$newLoc.x;
this._$oldLoc.y=this._$newLoc.y;
this._$minX=arguments[1];
this._$minY=arguments[2];
this._$maxX=arguments[3];
this._$maxY=arguments[4];
this._$boundary=(this._$minX==undefined || this._$maxX==undefined || this._$minY==undefined || this._$maxY==undefined)?false:true;
this._$intID=setInterval(this,"_$scanDrag",this._$laps);
this._$startDrag.apply(this,arguments);
}
this.stopDrag=function(k:Number){
//this signature is reacher than native overwrited function
if(k!=undefined && !isNaN(k)) this._$k=-k;
clearInterval(this._$intID);
this._$stopDrag();//pas d'argument en natif
this._$actualize();
var lps:Number=(this._$newTime-this._$oldTime)/1000;
this._$newSpd.x=(this._$newLoc.x-this._$oldLoc.x)/lps;
this._$newSpd.y=(this._$newLoc.y-this._$oldLoc.y)/lps;
this._$time=0;
this._$spd.x=this._$newSpd.x;
this._$spd.y=this._$newSpd.y;
this._$loc.x=this._$newLoc.x;
this._$loc.y=this._$newLoc.y;
this._$intID2=setInterval(this,"_$slipDrop",this._$laps);
}
}
else if(this._$startDrag && this._$stopDrag){
clearInterval(this._$intID);
clearInterval(this._$intID2);
this.startDrag=this._$startDrag;
this.stopDrag=this._$stopDrag;
for(var p:String in this) if(p.indexOf('_$')==0) delete this[p];
}
}
//---------------------------------utilisation------------------------------------------------
this.attachMovie("balle","ball1",0,{_x:200,_y:200});//should be in startDrag allowed area (if any)!
this.ball1.setInertialDrop(true);
this.ball1.onPress=function(){
//this.startDrag(false,100,100,600,400);//Stage should be equal or greater
this.startDrag(false,16,16,Stage.width-16,Stage.height-16);
}
this.ball1.onRelease=this.ball1.onReleaseOutside=function(){
this.stopDrag(5);//0: never stops, 0.01: very slippery, 30: almost stuck, null: no move ie normal drop
}