Allez je suis d'humeur genereuse ce soir....
Il y a longtemps, environ 8 mois j'ai commencé flash par un jeu de tir...
C'etait un jeu de char que j'ai jamais fini......
Alors le heros etait un char qui rencontrait d'autre chars....
Le char du joueur est un clip qui se deplace de gauche a droite et de haut en bas.... Par contre il tire du haut vers le bas...
Voici le code du clip (char) du joueur :
onClipEvent (load) {
//vitesse de deplacement sur l'ecran
speed=8;
n=0;
}
//appui sur fleche droite
onClipEvent (enterFrame) {
if (key.isDown(key.RIGHT)) {
_x+=speed;
if (_x>800) {
_x = 800;
}
//appui sur fleche gauche
} else if (key.isDown(key.LEFT)) {
_x-=speed;
if (_x<0) {
_x = 0;
}
}
//appui sur fleche haut
if (key.isDown(key.UP)) {
_y-=speed;
if (_y<300) {
_y = 300;
}
//appui sur fleche bas
} else if (key.isDown(key.DOWN)) {
_y+=speed;
if (_y>450) {
_y = 450;
}
}
}
Maintenant cela se complique....
Parceque je redecouvre le script en meme temps que toi...
Voici la sequence de tir de l'ennemi, le tir s'oriente directement vers le joueur au moment ou il est lancé....
// ----------------------------------------------
//
// game gun
//
// ----------------------------------------------
shootchance = 3;
shotspeed = 5;
gunx = getProperty("", _x);
guny = getProperty("", _y);
sx = getProperty("_root.ship", _x);
sy = getProperty("_root.ship", _y);
mouse_x = getProperty("_root.ship", _x)-gunx;
mouse_y = getProperty("_root.ship", _y)-guny;
// ----------------------------------------------
// calculate absolute values:
// ----------------------------------------------
if (Number(mouse_x)<0) {
abs_mouse_x = Number(mouse_x)+Number((mouse_x*-2));
xsign = -1;
} else {
abs_mouse_x = mouse_x;
xsign = +1;
}
if (Number(mouse_y)<0) {
abs_mouse_y = Number(mouse_y)+Number((mouse_y*-2));
ysign = -1;
} else {
abs_mouse_y = mouse_y;
ysign = +1;
}
// ----------------------------------------------
// determine hypotenuse length
// ----------------------------------------------
// a squared plus b squared = c squared... (c=hypotenuese) so what is c?
input = Number((abs_mouse_x*abs_mouse_x))+Number((abs_mouse_y*abs_mouse_y));
call("sqrt");
hyp = output;
// ----------------------------------------------
// determine sin(theta)
// ----------------------------------------------
// now we know "c" (the hypotenuese) how do we find the angle? Sine(Theta) is Opposite over Hypotenuese, then lookup the Sine value in the lookup table.
sin_theta = abs_mouse_y/hyp;
// ----------------------------------------------
// lookup angle in table
// ----------------------------------------------
counter = 0;
found = 0;
while (not (found)) {
if (Number(substring(..:sine_lookup_table, Number((counter*6))+1, 5))<=Number(sin_theta)) {
// this counter will only increase if the value we are looking for is LESS than or EXACTLY the angle match in the lookup table. This way, if the sine value has no exact match then the counter will point to the angle closest (less than) the correct answer. How do we know the angle when it's not in the table? There are 90 values in the table. Counter represents which value we are currently comparing. Thus, counter IS the angle.
angle = counter;
} else {
found = 1;
}
counter = Number(counter)+1;
}
// ----------------------------------------------
// add back full circle values
// (because in making all the values absolute (required for some math calculations) we limit the angles to only one quadrant... but there are three more quadrants the angle could be in. This is where I fix this.)
// ----------------------------------------------
if (Number(sx)<Number(gunx)) {
angle = Number((90-angle))+90;
} else {
}
if (Number(sy)>Number(guny)) {
angle = Number(angle)+180;
} else {
angle = Number((90-angle))-90;
}
// ----------------------------------------------
// set rotation
// ----------------------------------------------
setProperty("tourelle", _rotation, angle+90);
// ----------------------------------------------
// shoot at user?
// ----------------------------------------------
// the variable "shootchance" represents a percentage of 100. Since I use a random number, there is (in theory) a 5 percent chance that the random number will be between 1 and 100. Since Random ACTUALLY returns a value between 0 and 99 I add +1.
shootnow = Number(random(100))+1;
if (Number(shootnow)<=Number(shootchance)) {
//if (Number(shootnow)<=100) {
// keep increasing "shotname" so there are no shots with the same name ever.
shotname = Number(shotname)+100;
if (Number(shotname)>=10000) {
shotname = 1000;
}
attachMovie ("star", "shot" add shotname, shotname);
// ----------------------------------------------
// determine shot travel values
// ----------------------------------------------
if (Number(abs_mouse_x)>=Number(abs_mouse_y)) {
// shot is more horizontal than vertical
set("shot" add shotname add ":xmov", shotspeed*xsign);
set("shot" add shotname add ":ymov", shotspeed*(abs_mouse_y/abs_mouse_x)*ysign);
} else {
// shot is more vertical than horizontal
set("shot" add shotname add ":ymov", shotspeed*ysign);
set("shot" add shotname add ":xmov", shotspeed*(abs_mouse_x/abs_mouse_y)*xsign);
}
shotx = getProperty("base_char", _x);
shoty = getProperty("base_char", _y);
setProperty("shot" add shotname, _rotation, angle);
setProperty("shot" add shotname, _x, shotx);
setProperty("shot" add shotname, _y, shoty);
}
Cette partie de code n'est pas de moi j'ai du l'adapter...
En gros tu recherches les coordonnées et tu orientes ton tir
Bref je vais voir si je peux te mettre la source dans le site, jette y un oeuil...
Tu apprendras plein de trucs... Sinon ben je te conseille de faire un tour dans des sites comme celui ci et de jeter un oeuil dans les sources accessibles section jeux, tu trouveras des jeux de tir et tu pourras comparer les scripts...
@+
Cadoudal56 La Bretagne ca vous gagne !!
|