Accueil > Test collisions entre objets

version v2

21/11/2006

Test de collision entre plusieurs objets.
Saisissez le carré rouge avec la souris et touchez la boule bleue.
Utilisation de la fonction 'hitTest'
Les groupes:
le carré rouge; la boule bleue;
les 4 murs sont des groupes : mur_h ; mur_b ; mur_g ; mur_d.
a chaque test hitTest,Mtasc met une variable a TRUE,et eanim récupère cette infos.
A la Réception,E-anim test et efface l'info, et démarre un groupe 'boing' qui affiche pendant quelques secondes un texte (touché).
Exemple pour un test:
//--si boule touche mur haut ------
if(_root.var_h==true) {                       
 // info venant MTASC
_root.var_h=false;                             
  // RAZ de l'info Mtasc
_root.boing_h._x=_root.boule._x;       
// déplace le groupe 'boing' avec la position de la souris
_root.boing_h._y=_root.boule._y;
gotoGroup( _root.boing_h, 2 );           
// groupe bulle 3D
PlayGroup(_root.boing_h );
}

Accès aux Téléchargements

le fichier .phi:
Des groupes pour les murs,carré,boule,et text 'bulle_3d)
un groupe "permanent":
// ----------- initialisation -------------------
if ( init != 11) { init =11;
_root.deplaceX = 8;
// vitesse déplacement X
_root.deplaceY = 8;
// vitesse déplacement X
max_x=400; max_y=400;
// cote de déplacement
//----- position aléatoire -------------------
_root.cible1._x =RandomNumber(max_x);
_root.cible1._y =RandomNumber(max_y);
}
//---- déplacement en X
_root.boule._x= _root.boule._x+_root.deplaceX;
var1=_root.boule._x;
if(var1<20 || var1>=378) {
_root.deplaceX = -_root.deplaceX; }
//---- déplacement en y
_root.boule._y= _root.boule._y+_root.deplaceY;
var2=_root.boule._y;
if(var2<44 || var2>=376)
_root.deplaceY = -_root.deplaceY;
//--------------------------------------------------------
//--si boule touche mur haut ------

if(_root.var_h==true) { //
info venant MTASC
_root.var_h=false;
// RAZ de l'info Mtasc
_root.boing_h._x=_root.boule._x;
_root.boing_h._y=_root.boule._y;
gotoGroup( _root.boing_h, 2 );/
/ groupe bulle 3D
PlayGroup(_root.boing_h );
}
/
/--si boule touche mur bas------
if(_root.var_b==true) {
// info venant MTASC
_root.var_b=false;
// RAZ de l'info Mtasc
_root.boing_b._x=_root.boule._x;
_root.boing_b._y=_root.boule._y;
gotoGroup( _root.boing_b, 2 );/
/ groupe bulle 3D
PlayGroup(_root.boing_b );
}
/
/--si boule touche mur gauche------
if(_root.var_g==true) { /
/ info venant MTASC
_root.var_g=false;
// RAZ de l'info Mtasc
_root.boing_g._x=_root.boule._x;
_root.boing_g._y=_root.boule._y;
gotoGroup( _root.boing_g, 2 );
// groupe bulle 3D
PlayGroup(_root.boing_g );
}
//--si boule touche mur droit------
if(_root.var_d==true) {
// info venant MTASC
_root.var_d=false; /
/ RAZ de l'info Mtasc
_root.boing_d._x=_root.boule._x;
_root.boing_d._y=_root.boule._y;
gotoGroup( _root.boing_d, 2 );/
/ groupe bulle 3D
PlayGroup(_root.boing_d );
}
//--si CARRE touche CIBLE------
if(_root.var_t==true) {
// info venant MTASC
_root.var_t=false;
// RAZ de l'info Mtasc
_root.boing_t._x=_root.boule._x;
_root.boing_t._y=_root.boule._y;
gotoGroup( _root.boing_t, 2 );
// groupe bulle 3D
PlayGroup(_root.boing_t );
}
//------- appel de la fonction mtac ------------
cible1=_root.boule;
->test:collision(carre,cible1);

-programme mtasc : collision.as :
// --prog test de collision- V2------
class test{
static function collision(mc:Object,cible1:Object) {
// ---- si appui souris sur draggable du carre ----------
mc.onPress = function() {
this.startDrag();
};
_root.message0_text="Rien";
/
/ ---- si relache souris arret carre -
mc.onRelease = function() {
this.stopDrag();
}
/
/ ---- test si pixels boule(cible1) ---
if (mc.
hitTest(cible1)) { //test si carré touche 'cible1'
_root.message0_text="Touche_cible1";
//Affiche touché
_root.var_t=true;
}
/
/ ---- test si pixels mur haut----
if (_root.boule.
hitTest(_root.mur_h)) { //test si boule touche le mur en haut
_root.message0_text="Touche_HAUT";
//Affiche touché
_root.var_h=true;
}
// ---- test si pixels mur bas----
if (_root.boule.hitTest(_root.mur_b)) {
//test si boule touche le mur en bas
_root.message0_text="Touche_BAS";
//Affiche touché
_root.var_b=true;
}
// ---- test si pixels mur gauche----
if (_root.boule.hitTest(_root.mur_g)) { /
/test si boule touche le mur a gauche
_root.message0_text="Touche_GAUCHE";
//Affiche touché
_root.var_g=true;
}
// ---- test si pixels mur droit----
if (_root.boule.hitTest(_root.mur_d)) {
//test si boule touche le mur a droite
_root.message0_text="Touche_DROIT";
//Affiche touché
_root.var_d=true;
}
}
// fin de la static fonction
/
/-------------------------------------
static function main() { }
}
// fin de la class

==============================================================

un fichier batch :  le fichier collision.bat
E:\mtasc\mtasc -v -swf E:\mtasc\collision\collision.swf -main collision.as
pause
vous pouvez lancer votre fichier.bat depuis windows en double cliquant dessus.