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: 6766 6766 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:
1 234 »
comenteaza printeaza

Pasul 4. Adaugarea elementelor nivelului.

In aceasta lectie vom anexa elemente nivelului, cum ar fi obstacole si scari. Iata o lista cu ce vei adauga:

  • Scari
  • Obstacole
  • Trambuline

Adaugarea scarilor

Scarile vor fi patrate ca si blocurile, numai ca vor avea alta culoare,si anume galben, astfel va fi usor sa le identifici pe scena. Trebuie sa creezi si un obiect ca va contine intreg nivelul, inclusiv blockHolder-ul, iar in cadrul acestuia, un obiect Sprite care va include toate scarile. Inlocuieste partea in care adaugi blockHolder-ul inaintea functiei cu asta:

//acest obiect va include toate scarile
  var ladderHolder:Sprite = new Sprite();
  //il adaugam pe scena
  addChild(ladderHolder);

Acum, trebuie sa completam anumite lucruri functiei createLvl(). Numarul care va simboliza scara este "2", iar bucla "for" va fi inlocuita cu:

for(var i:int = 0;i<lvlArray.length;i++){
    // verificam daca trebuie sa trecem la randul urmator 
    // daca i este divizibil la numarul de coloane
    if(i/lvlColumns == int(i/lvlColumns)){
        row ++;
    }
    //definim o tinta
    var newPlacement;
    if(lvlArray[i] == 1){
        // cream un nou bloc
        newPlacement = new Block();
        // desenam blocul
        newPlacement.graphics.beginFill(0xFFFFFF/* Culoarea formei */,
        1/* Transparenta formei - 1 pentru opac */);
        // desenam un patrat
        newPlacement.graphics.drawRect(0,0,25,25);
        // punem blocul pe scena
        blockHolder.addChild(newPlacement);
    } else if (lvlArray[i] == 'MAIN'){
        newPlacement = mcMain;
    } else if (lvlArray[i] == 2){
        newPlacement = new Sprite();
        //desenam scara
        newPlacement.graphics.beginFill(0xFFFF00,1);
        //desenam un patrat
        newPlacement.graphics.drawRect(0,0,25,25);
        //si il adaugam pe scena
        ladderHolder.addChild(newPlacement);
    }
 
    if(lvlArray[i] != 0){
        //schimbam coordonatele blocului
        newPlacement.x = (i-(row-1)*lvlColumns)*newPlacement.width;
        newPlacement.y = (row-1)*newPlacement.height;
    }
}

Acest nou cod doar adauga scarile si refoloseste o anumita variabila, astfel incat nu devine repetitiv. Urmatorul lucru pe care il vei face este mutarea lui lvlHolder in locul lui blockHolder in momentul cand o tasta sageata e apasata. Sper ca stii cum sa faci asta. Ca un indiciu, uita-te in functia moveChar(). Totusi, trebuie ca aceste scari sa si functioneze. Pentru asta trebuie sa inserezi o variabila care ne va spune daca jucatorul atinge scara:

//variabila booleana care ne spune daca personajul se afla pe o scara
var mainOnLadder:Boolean = false;

Apoi vei adauga o bucla care va testa daca personajul atinge vreo scara. Adauga acest cod in functia moveChar() :

//verificare daca personajul atinge vreo scara
for(i=0;i<ladderHolder.numChildren;i++){
    //preia referinta la scara curenta
    var hitLadder:DisplayObject = ladderHolder.getChildAt(i);
    //verifica daca scara e atinsa
    if(mcMain.hitTestObject(hitLadder)){
        //verifica daca personajul se afla suficient de aproape de scara
        if(mcMain.x >= hitLadder.x + lvlHolder.x - 10 && mcMain.x 
            <= hitLadder.x + lvlHolder.x + 35){
            mainOnLadder = true;
            //reseteaza viteza sariturii astfel incat as nu sara la infinit 
            jumpSpeed = jumpSpeedLimit;
            break;
        }
    }
    mainOnLadder = false;
}

Acum nu-i poti permite personajului sa sara daca e pe o scara si trebuie sa il controlezi pentru a se misca in sus si in jos, in functie de ce tasta e apasata. Iata codul ce va controla tasta sus:

if(upKeyDown || mainJumping){
    if(!mainOnLadder){
        mainJump();
    }
}
if(upKeyDown && mainOnLadder){
    mcMain.y -= mainSpeed;
}
if(downKeyDown && mainOnLadder && !mainOnGround){
    mcMain.y += mainSpeed;
}

Abia acum poti schimba codul nivelului pentru a te asigura ca scarile merg.

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

Daca il testezi va merge destul de bine. Singura problema e ca user-ul e mereu sub scara, dar acest lucru poate fi usor reparat prin adaugarea urmatoarei instructiuni la sfarsitul functiei:

//punem personajul deasupra scarii
MovieClip(root).setChildIndex(mcMain,MovieClip(root).numChildren-1);

Acum ca am reusit cu crearea scarilor, putem trece la urmatorul punct de pe lista - atasarea obstacolelor sau a obiectelor de care se va lovi personajul. Ele vor actiona ca un fel de perete care il va face pe user sa ricoseze atunci cand se va lovi de el. Din nou obiectul va avea forma unui patrat, dar de data aceasta va fi verde. Ca de fiecare data, vei avea nevoie de un suport pentru acest perete:

//acest obiect la contine toate obstacolele
var bumperHolder:Sprite = new Sprite();
lvlHolder.addChild(bumperHolder);
Pagina:
1 234 »
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
5460
Tutoriale scrise de mcuemica
mcuemica Rang utilizator mcuemica - Incepator
5460
Tutoriale scrise de emonclercheap
emonclercheap Rang utilizator emonclercheap - Incepator
5285
Tutoriale scrise de ellarichards
ellarichards Rang utilizator ellarichards - Incepator
5270
Tutoriale scrise de beacherrosa
beacherrosa Rang utilizator beacherrosa - Incepator
5015
* Acest top reprezinta punctajele acumulate in ultimele 30 de zile.
PHP Javascript XML Flash Fotografie Bridge Dreamweaver Lightroom XHTML PSD HTML Python JSON Photoshop SWF Ruby on Rails Sony Vegas AJAX MySQL Illustrator Excel Verilog StyleSheet Action Script Powerpoint Fireworks Java Gimp SEO COREL DRAW Outlook Word RoR Swift 3D CSS Vista
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