E-learn.ro
Panou utilizatori
Utilizator Parola
Creeaza cont nou    Recupereaza parola
Login
Newsletter
Introdu adresa ta de email
Inscrie-te
Inchide panoul de utilizatori
Add to Google

Tutoriale Adobe Flash

Descarca toolbar

Toolbar E-learn.ro Facebook Twitter

GRAFICA 2D  /  Adobe Flash  /  ActionScript (14)

Implementarea unui joc tip platforma in AS3 (partea a II-a)

03.11.2008
Implementarea unui joc tip platforma in AS3 (partea a II-a)

In aceasta parte vei invata cum sa adaugi nivelului scari si alte obstacole.

Total vizualizari: 6295 6295 afisari   |   Comentarii  1   |   Rating   |   (3 voturi)   |   Timp necesar: 1 h 1 h   |   Nivel de cunostiinte necesar: Avansat  Avansat

Sursa:  Mrsunstudios.com   
Autor:  Mr. Sun
Adauga la tutoriale favorit Adauga la tutoriale favorite
Pagina:
« 123 4
comenteaza printeaza

Pasul 6. Crearea obiectivelor si terminarea nivelului

Acum ca ai adaugat componentele nivelului si inamicii, poti sa si termini un nivel. In acest joc personajul principal poate trece la urmatorul nivel doar daca atinge un obiect. Pe parcurs vei adauga si cateva monede ce vor aduce puncte. Primul lucru pe care trebuie sa-l faci este sa adaugi o variabila care sa retina scorul:

//VARIABILA SCOR
var mainScore:int = 0;

Apoi vei adauga monede la fel cum ai facut si cu celelalte elementele, doar ca de data aceasta le vei insira intr-o clasa. Prima data vei defini o clasa numita "Coin" intr-un fisier extern ActionScript numit "Coin.as", caruia ii vei adauga urmatorul cod:

package{
    import flash.display.Sprite;
    import flash.display.MovieClip;
    import flash.display.Shape;
    import flash.display.DisplayObject;
    import flash.events.*;
    //sprites reprezinta movie clip-uri fara nici un frame
    public class Coin extends Sprite{
        //_root este referinta la radacina documentului
        private var _root:Object;
 
        public function Coin(){
            //acest cod va fi executat o singura data
            addEventListener(Event.ADDED, beginClass);
            //acest cod va fi executat o singura continuu
            addEventListener(Event.ENTER_FRAME, eFrame);
        }
 
        private function beginClass(event:Event):void{
            //definim referinta la radacina documentului
            _root = MovieClip(root);
            //cream un obiect invizibil care ne va ajuta la pozitionare
            this.graphics.beginFill(0x000000,0);
            this.graphics.drawRect(0,0,25,25);
            //desenam o forma care va reprezenta o moneda
            this.graphics.beginFill(0xFFFFFF,1);
            this.graphics.drawCircle(12.5,12.5,2.5);
        }
        private function eFrame(event:Event):void{
            //verificam daca personajul a atins moneda
            if(_root.mcMain.x <= this.x + _root.lvlHolder.x + 10
               && _root.mcMain.x >= this.x + _root.lvlHolder.x - 10
               && _root.mcMain.y <= this.y + 10
               && _root.mcMain.y >= this.y - 10){
                this.parent.removeChild(this);
                removeEventListener(Event.ENTER_FRAME, eFrame);
                //si incrementam scorul
                _root.mainScore += 100;
            }
        }
    }
}

Urmatorul pas consta in updatarea scorului de fiecare data cand un inamic e omorat. Cred ca 500 de puncte reprezinta o recompensa corecta pentru distrugerea unui dusman. Acum poti adauga si un obiect tinta, dar va trebui sa creezi o noua clasa:

package{
    import flash.display.Sprite;
    import flash.display.MovieClip;
    import flash.display.Shape;
    import flash.display.DisplayObject;
    import flash.events.*;
    //sprites reprezinta movie clip-uri fara nici un frame
    public class Goal extends Sprite{
        //_root este referinta la radacina documentului
        private var _root:Object;
 
        public function Goal(){
            //acest cod va fi executat o singura data
            addEventListener(Event.ADDED, beginClass);
            //acest cod va fi executat continuu
            addEventListener(Event.ENTER_FRAME, eFrame);
        }
 
        private function beginClass(event:Event):void{
            //definim referinta la radacina documentului
            _root = MovieClip(root);
            //cream un obiect invizibil care ne va ajuta la pozitionare
            this.graphics.beginFill(0x00FFFF);
            this.graphics.drawRect(0,0,25,25);
        }
        private function eFrame(event:Event):void{
            if(hitTestObject(_root.mcMain)){
                //trecem la nivelul urmator
                _root.lvlCurrent ++;
                _root.resetLvl();
            }
        }
    }
}

Ca de obicei, vei adauga acest cod buclei "for" din functia makeLvl():

 
if (lvlArray[i] == 8){
    //am atins tinta
    newPlacement = new Goal();
    enemyHolder.addChild(newPlacement);
}

In cele din urma, modifica lvlArray1 si adauga lvlArray2:

var lvlArray1:Array = new Array(
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0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
);


Pasul 7. Tusele finale

In aceasta parte a tutorialului iti voi spune ce mai ai de facut, dar nu si cum trebuie facut.
Primul lucru pe care trebuie sa-l faci este afisarea scorului. Creaza un camp text dinamic undeva in partea de sus a scenei si adauga codul care il va initializa cu valoarea "Score: 0". Bineinteles, 0 va fi inlocuit cu scorul propriu-zis al jucatorului. Iti sugerez sa il atasezi undeva in functia moveChar() pentru a nu fi nevoit sa creezi una noua.
Urmatorul pas reprezinta resetarea scorului de fiecare data cand user-ul moare. Aceasta optiune poate parea un pic dificila pentru ca in momentul cand personajul moare sau cand el trece la urmatorul nivel se foloseste aceeasi functie resetLvl(), dar poti sa o indeplinesti destul de usor. Mai intai, seteaza scorul la 0 la sfarsitul functiei resetLvl(). Acum cauta codul folosit atunci cand se reseteaza nivelul pentru ca jucatorul a castigat. Ar trebui sa fie in jurul randului 30 din fisierul "Goal.as"si modifica-l astfel:

//trecerea la nivelul urmator
_root.lvlCurrent ++;
var lastScore:int = _root.mainScore;
_root.resetLvl();
_root.mainScore = lastScore;

In acest mod se salveaza scorul avut inainte de a avansa cu un nivel si se reinitializeaza dupa o resetare.
In continuare, vei adauga un fundal jocului. El va fi mai negru pentru a se putea distinge de joc si se va misca mai incet decat cel al jocului pentru a crea iluzia de indepartare. Primul lucru pe care trebuie sa-l faci este sa creezi un nou container pentru particulele din cadrul nivelului. Te vei ajuta de faptul ca totul se realizeaza la fel ca si cu trambulinele si obiectele care ricoseaza. Il poti numi bgHolder, initializandu-l inaintea tututor celorlalte elemente pentru a fi dedesubt. Mai departe, adauga acest cod la sfarsitul functiei createLvl():

//cream particule random pentru fundal
//vom crea cate o particula pentru fiecare bloc
var newPart:Shape = new Shape();
newPart.graphics.beginFill(0x222222);
newPart.graphics.drawRect(0,0,int(Math.random()*10)+1,int(Math.random()*10)+1);
newPart.x = int(Math.random()*lvlColumns*50)-550;
newPart.y = (row-1)*25;
bgHolder.addChild(newPart);

Acum trebuie sa faci in asa fel incat particulele sa se miste mai incet decat fundalul jocului si pentru asta te poti folosi de functia moveChar. Gaseste acest cod:

//daca anumite taste sunt apasate, misca personajul
if(leftKeyDown){
    lvlHolder.x += mainSpeed;
}
if(rightKeyDown){
    lvlHolder.x -= mainSpeed;
}
 
Si inlocuieste-l cu acesta:
 
// daca anumite taste sunt apasate, misca personajul
if(leftKeyDown){
    lvlHolder.x += mainSpeed;
    bgHolder.x -= mainSpeed*.5;
}
if(rightKeyDown){
    lvlHolder.x -= mainSpeed;
    bgHolder.x += mainSpeed*.5;
}

Ultimul lucru pe care trebuie sa-l faci pentru fundal este sa ii resetezi pozitia pe axa x de fiecare data cand nivelul este la randul sau resetat. Seteaza-i coordonata pe x fie in functia createLvl() , fie in resetLvl(), dupa cum doresti.
Am terminat si cu ultimele indicatii. Acum te poti bucura de noul tau joc!


Pagina:
« 123 4
comenteaza printeaza
Alte tutoriale Adobe Flash:
Noteaza acest tutorial
Rating tutorial
 
(3 voturi)
Pentru a nota acest tutorial, trebuie sa fii logat!
COMENTARII (1) spune-ti parerea
andrey andrey , Sambata, 28 August 2010, ora 21:34
#1

super!

dar se poate face cu programul siun joc simplu multyplayer?

Raporteaza acest comentariu ca injurios!
Posteaza un comentariu
Pentru a posta un comentariu, trebuie sa fii logat!
0 TOP UTILIZATORI* 0 0
Tutoriale scrise de claibornelara
claibornelara Rang utilizator claibornelara - Incepator
4550
Tutoriale scrise de mcuemica
mcuemica Rang utilizator mcuemica - Incepator
4460
Tutoriale scrise de ellarichards
ellarichards Rang utilizator ellarichards - Incepator
4415
Tutoriale scrise de emonclercheap
emonclercheap Rang utilizator emonclercheap - Incepator
4285
Tutoriale scrise de beacherrosa
beacherrosa Rang utilizator beacherrosa - Incepator
4165
* Acest top reprezinta punctajele acumulate in ultimele 30 de zile.
Word Photoshop Python SWF Verilog XHTML Dreamweaver Sony Vegas Fireworks Excel Outlook Lightroom RoR Ruby on Rails XML Java Flash CSS Illustrator PSD Swift 3D COREL DRAW Bridge Vista Action Script AJAX JSON MySQL PHP Fotografie SEO Gimp Powerpoint StyleSheet HTML Javascript
Promovare:
Daca faci parte din comunitatea E-learn.ro si doresti promovarea acesteia, poti accesa pagina de promovare.
Arhiva newsletter:
Daca ai ratat un numar mai vechi, sau vrei sa revezi care au fost noutatile E-learn.ro la un moment dat, poti accesa arhiva de newslettere.
  Copyright © 2008-2013 E-LEARN.ro. Toate drepturile rezervate. Termeni si conditii.
Conceput si realizat de Neokinetics Software