Creare gioco auto (parte 9 multiplayer)
Posted on 01. May, 2006 by Administrator in flash, tutorials
In questo articolo tratteremo come creare un gioco di auto in flash.Nelle parti
precedenti abbiamo visto come sia possibile creare il gioco ora vedremo come
poter sfidare un secondo giocatore in multiplayer.
Abbiamo gia visto in due articoli precedenti come funzionano i socket in flash e
come fare il server in java per cui basta dare un'occhiata a quelli.
Per il
gioco dobbiamo fare alcune modifiche a quello gia creato.Creeremo stavolta due
movieclip auto invece che uno solo peechè il secondo rappresenta l'avversario.
Aggiungiamo questo nuovo movieclip allo stage dandogli nome d'istanza
"avversario".Addesso aggiungiamo un nuovo layer "socket" e mettiamo come codice:
stop();
//crea oggetto socket
_root.mysocket = new XMLSocket();
//si connette all'indirizzo 127.0.0.1 e alla porta 2001
_root.mysocket.connect("127.0.0.1", 2001);
//vede se è tutto ok
_root.mysocket.onConnect = function (success) {
if (success) {
stato = "connesso";
} else {
stato = "non connesso";
}
}
//chiudi la connessione
_root.mysocket.onClose = function(success){
_root.mysocket.close
stato = "connessione chiusa";
}
//riceve i dati dal giocatoreB
_root.mysocket.onData = function(data) {
datArr= new Array();
datArr= data.split(",");
angolo = datArr[0];
rotazione = datArr[1];
quanto = (rotazione/180)*Math.PI;
_root.avversario._x += angolo*Math.sin(quanto);
_root.avversario._y += -angolo*Math.cos(quanto);
_root.avversario._rotation = rotazione;
updateAfterEvent();
}
Selezioniamo il movieclip "auto" e al posto del vecchio codice mettiamo:
onClipEvent (load) {
//Edit these parameters, max speed forward, and max speed backwards
maxspeed = 4;
maxnegspeed = -4;
// Init vars
speed = 0;
// Definition of the move function, it moves the car around the screen
function move(angolo) {
quanto = (_rotation/180)*Math.PI;
_x += angolo*Math.sin(quanto);
_y += -angolo*Math.cos(quanto);
return angolo;
}
}
onClipEvent (enterFrame) {
// If UP key pressed, raise speed by 0.1
if (Key.isDown(Key.UP))
{
if (speed<maxspeed)
{
speed += 0.1;
}
} else {
// If up key not pressed nor the down key... slowly lower speed
if (Key.isDown(Key.DOWN) == false)
{
speed *= 0.98;
if (speed<0.1 and speed>-0.1)
{
speed = 0;
}
}
}
// If DOWN key pressed; brake or reverse te car
if (Key.isDown(Key.DOWN))
{
if (speed>maxnegspeed)
{
speed -= 0.1;
}
}
// Steer left
if (Key.isDown(Key.LEFT))
{
_rotation -= speed/1; //era /2
}
// Steer right
if (Key.isDown(Key.RIGHT))
{
_rotation += speed/1; //era /2
}
move(speed);
if(speed>0)GO(speed,_rotation);
_root.speed = Math.round(speed);
//barra velocita
_root.circvel.gui_needle._rotation = speed*35;
// IS CAR ON TRACK?
if (!_root.tracciato.hittest(this._x,this._y,true)){
speed = 0.5; // offroad!
}
//manda informazioni al server
function GO(rotazione,ascissa,ordinata){
_root.mysocket.send(rotazione+","+ascissa+","+ordinata);
updateAfterEvent();
}
}
Non ci resta da fare piu nulla, se vogliamo giocare in multiplayer, possiamo
avere anche solo un client in flash, altrimenti ne creiamo un
secondo con lo stesso codice del primo.






Leave a reply