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: 6071 6071 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:
« 12 3 4 »
comenteaza printeaza

Pasul 5. Adaugarea inamicilor

Pentru a realiza acest lucru e nevoie de crearea unei clase Enemy prin realizarea unui fisier extern ActionScript pe care il vei numi "Enemy.as". Apoi adauga urmatorul cod in fisier.

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 Enemy extends Sprite{
  //_root este referinta la radacina documentului
  private var _root:Object;
  // viteza cu care se misca inamicul
  private var speed:Number;
  //-1 indica directia catre stanga, 1 catre dreapta
  private var direction:int;
  
  public function Enemy(){
  //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);
  //definim variabilele de miscare ale inamicului
  speed = 5;
  direction = 1;
  //desenam inamicul ca un cerc rosu
  this.graphics.beginFill(0xFF0000,1);
  this.graphics.drawCircle(12.5,12.5,12.5);
  }
  
  private function eFrame(event:Event):void{
  
  }
  }
  }

Cu acest cod se adauga inamicul pe scena, schitandu-l ca o bila rosie. Inainte de a completa functia enterFrame, trebuie sa introducem in timeline-ul principal un cod care va muta inamicul pe scena, impreuna cu cativa indicatori ce ii vor spune cand sa se intoarca. In functia createLvl , adauga in bucla "for":

if (lvlArray[i] == 5){
  //adaugam inamicul
  newPlacement = new Enemy();
  enemyHolder.addChild(newPlacement);
  } else if (lvlArray[i] == 6){
  //adaugam un indicator care ii va spune cand sa se intoarca
  newPlacement = new Shape();
  newPlacement.graphics.beginFill(0x000000,0);
  newPlacement.graphics.drawRect(0,0,25,25);
  invisMarkerHolder.addChild(newPlacement);
  }

Acum va trebui sa pui pe scena atat invisMarkerHolder, cat si un enemyHolder. Ai mai facut asta cu obiectele precedente, trebuie sa stii. Apoi schimba sirul nivelului astfel:

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,6,0,0,0,0,0,5,0,0,0,0,6,0,0,0,0,0,0,0,0,0,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,4,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,1,1,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
);

Abia acum poti adauga inamicului o functie de programare prin alaturarea urmatorului cod functiei eFrame() din Enemy.as :

private function eFrame(event:Event):void{
    //MISCAREA INAMICULUI
    this.x += speed * direction;
    //verifica daca acesta nu a intalnit vreun indicator
    for(var i:int=0;i<_root.invisMarkerHolder.numChildren;i++){
        //procesul este similar celui folosit in cazul personajului principal
        var targetMarker:DisplayObject = _root.invisMarkerHolder.getChildAt(i);
        if(hitTestObject(targetMarker)){
            direction *= -1;
            this.x += speed * direction;
        }
    }
    //INTALNIREA INAMICULUI CU PERSONAJUL PRINCIPAL
    if(this.hitTestObject(_root.mcMain)){
        //daca se ating in timp ce personajul sare
        //sau daca se afla in cadere (in aer)
        if(_root.mainJumping && _root.jumpSpeed > 0){
            //omoara inamicul
            this.parent.removeChild(this);
            //si sterge functia listener
            this.removeEventListener(Event.ENTER_FRAME, eFrame);
        } else {
            //altfel, omoara personajul principal
            //nu am prevazut ce se intampla daca personajul moare
            //deci pentru moment vom afisa un mesaj
            trace('OH NU! Ai murit!');
        }
    }
}

Daca testezi vei vedea un cerc rosu care se misca deasupra platformei de inceput. Daca il atingi, ar trebui sa vezi un mesaj, iar daca sari pe el, inamicul va disparea. E chiar interesant, nu? In continuare trebuie sa creezi o functie care va reseta jocul de fiecare data cand va muri personajul principal. Adauga acest cod la xxxx principala.

//reseteaza nivelul
function resetLvl():void{
    for(var i:int=0;i<lvlHolder.numChildren;i++){
        //selecteaza containerul curent
        var currentContainer = lvlHolder.getChildAt(i);
        //si sterge-i continutul
        while(currentContainer.numChildren > 0){
            for(var i2:int = 0;i2<currentContainer.numChildren;i2++){
                currentContainer.removeChildAt(i2);
            }
        }
    }
    //apoi resetam lvlHolder si refacem lvl
    lvlHolder.x = 0;
    createLvl();
}

Acum va trebui sa derulezi aceasta functie de fiecara data cand personajul principal iese de pe scena sau atinge inamicul. Daca nu te descurci exista fisierele sursa din care te poti inspira.


Pagina:
« 12 3 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
4170
Tutoriale scrise de kheops
kheops Rang utilizator kheops - Mediu
4084
Tutoriale scrise de ellarichards
ellarichards Rang utilizator ellarichards - Incepator
4080
Tutoriale scrise de mcuemica
mcuemica Rang utilizator mcuemica - Incepator
4055
Tutoriale scrise de emonclercheap
emonclercheap Rang utilizator emonclercheap - Incepator
3870
* Acest top reprezinta punctajele acumulate in ultimele 30 de zile.
Dreamweaver HTML Action Script Verilog Vista Sony Vegas Gimp Word SEO MySQL XML StyleSheet Bridge Outlook Swift 3D Java Illustrator CSS Python PSD SWF RoR Fireworks Fotografie Javascript Flash XHTML Lightroom JSON Powerpoint Ruby on Rails PHP AJAX COREL DRAW Excel Photoshop
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