Comunidad de diseño web y desarrollo en internet online

Incluir classe que extiende a Sprite

Citar            
MensajeEscrito el 16 Ago 2011 06:09 pm
Buenas, tengo una classe (Lucha) que extiende a Sprite, y la quiero cargar desde la classe Control, que extiende de movieclip.

El codigo:

Código ActionScript :

public function PulsaStart(navigationEvent:NavigationEvents):void {

      lucha = new Lucha();
      addChild(lucha);
      

   trace("hola");

   }


El trace es solo para comprobar si hacia bien los dispatch i listeners. Tambien probé de cargar otra classe que extendiera de movieclip, y si que funciona, asi que imagino que la debere cargar de otra forma, pero no se cual.

Por Phyronx

41 de clabLevel



 

chrome
Citar            
MensajeEscrito el 16 Ago 2011 06:26 pm
He encontrado parte del error, lo da la Classe Lucha.

TypeError: Error #1009: No se puede acceder a una propiedad o a un método de una referencia a un objeto nulo.
at Lucha/DibujarObjetos()
at Lucha()

Pero no entiendo porque. Mirad el codigo:

Código ActionScript :

package 
{
   
   import flash.display.Sprite;
   import flash.display.Bitmap;
   import flash.display.BitmapData;
   import flash.display.BlendMode;
   import flash.geom.Point;
   import flash.geom.Rectangle;
   import flash.geom.Matrix;
   import flash.events.Event;
   import flash.events.MouseEvent;
   import flash.events.KeyboardEvent;
   
   
   public class Lucha extends Sprite
   {
      public var terrain_bmpd=new BitmapData(550,200,true,0xFF00FF00);//This is the Bitmap of the terrain
      public var terrain_bmp=new Bitmap(terrain_bmpd);//and this the BitmapData
      public var pj = new Pj();
      public var bala = new Bala();
      public var pj_velocidad:Number=0;
      public var abujero=new Sprite();//That's the hole we need
      public var abujero_matrix:Matrix;//The hole_matrix is used to set the position of the hole
      public var left_key:Boolean;
      public var right_key:Boolean;
      public var space_key:Boolean;
      public var Balavelocidad:int=5;
      public var a_key:Boolean;
      public var disparo:Boolean;
      
      public var jumping:Boolean=true;//When that variable is true, the character is in the air
      public var i:int;//We will use this variable to make a loop
      
      public function Lucha() 
      {
            DibujarObjetos();//This function draws the character, the terrain and the hole.
            
         stage.addEventListener(Event.ENTER_FRAME,mover_pj);
         stage.addEventListener(MouseEvent.MOUSE_UP,mouse_up);
         stage.addEventListener(KeyboardEvent.KEY_DOWN,key_down);
         stage.addEventListener(KeyboardEvent.KEY_UP,key_up);
         
      }
      public function mover_pj(e:Event) {
         if (a_key) {
            bala = new Bala();
            bala.x = pj.x;
            bala.y = pj.y;
            stage.addChild(bala);
            }
            if ( terrain_bmpd.hitTest(new Point(terrain_bmp.x,terrain_bmp.y),0x01,new Rectangle( bala.x-6,bala.y-10,1,17))) {
         
               abujero_matrix=new Matrix();//We need to reset the matrix each new hole
         abujero_matrix.translate(bala.x - terrain_bmp.x, bala.y - terrain_bmp.y);//and setthe coordinates of the hole
      
         terrain_bmpd.draw(abujero, abujero_matrix, null, BlendMode.ERASE);//Then, we can draw the hole in the BitmapData
         
            
         stage.removeChild(bala);
         
      }else{Balavelocidad = 5}
      bala.y += Balavelocidad;
               
            
            
         //If left key is pressed, we'll move the character to the left
         if (left_key) {
            
            for (i=0; i<3; i++) {//Do you remember when we made the character fall? We had to move the character pixel by pixel
               if (! terrain_bmpd.hitTest(new Point(terrain_bmp.x,terrain_bmp.y),0x01,new Rectangle( pj.x-6, pj.y-10,1,17))) {
                  
                  pj.x--;   /*If the character doesn't hit the ground, we can move left. However,
                              the character may be sunk under the ground. We have to lift it*/
                  while (terrain_bmpd.hitTest(new Point(terrain_bmp.x,terrain_bmp.y),0x01,new Rectangle( pj.x-5, pj.y+9,10,1))) {
                      pj.y--;
                  }
               }
            }
         }
         if (right_key) {//Well, that's the same for the right key
            for (i=0; i<3; i++) {
               if (! terrain_bmpd.hitTest(new Point(terrain_bmp.x,terrain_bmp.y),0x01,new Rectangle( pj.x+5, pj.y-10,1,17))) {
                   pj.x++;
                  while (terrain_bmpd.hitTest(new Point(terrain_bmp.x,terrain_bmp.y),0x01,new Rectangle( pj.x-5, pj.y+9,10,1))) {
                      pj.y--;
                  }
               }
            }
         }
         if (space_key&&! jumping) {//That's easy: if he isn't jumping and you press space, his speed will be negative and he'll jump
              pj_velocidad=-10;
            jumping=true;//Now the character can't jump again
         }
 
           pj_velocidad++;//Every frame we will increase character's speed
         if ( pj_velocidad>0) {
            //If the speed is positive, we will check a collision between the terrain and the rectangle below the character
            for (i=0; i< pj_velocidad; i++) {//We check the collision pixel by pixel...
               if (! terrain_bmpd.hitTest(new Point(terrain_bmp.x,terrain_bmp.y),0x01,new Rectangle( pj.x-5, pj.y+9,10,1))) {
                   pj.y++;//If there isn't a collision, the character will fall
               } else {
                  jumping=false;//If there's a collision with the ground, the character isn't jumping
                   pj_velocidad=0;//The speed is 0, because the character hit the ground
               }
            }
         } else {
            for (i=0; i<Math.abs( pj_velocidad); i++) {//If the speed is negative, the for loop won't work. We have to use Math.abs().
            //Now we will check the collision between the terrain and the rectangle above the character
               if (! terrain_bmpd.hitTest(new Point(terrain_bmp.x,terrain_bmp.y),0x01,new Rectangle( pj.x-5, pj.y-10,10,1))) {
                   pj.y--;
               } else {
                   pj_velocidad=0;//Well, that's the same: the character hit the ground
               }
            }
         }
      }
      public function mouse_up(e:MouseEvent) {
         abujero_matrix=new Matrix();//We need to reset the matrix each new hole
         abujero_matrix.translate(e.stageX-terrain_bmp.x,e.stageY-terrain_bmp.y);//and setthe coordinates of the hole
         terrain_bmpd.draw(abujero,abujero_matrix,null,BlendMode.ERASE);//Then, we can draw the hole in the BitmapData
      }
      
      public function key_down(e:KeyboardEvent) {
         if (e.keyCode==37) {
            left_key = true;
            scaleX *= -1;
            pj.play();
         }
         if (e.keyCode==39) {
            right_key = true;
            pj.play();
         }
         if (e.keyCode==32) {
            space_key=true;
         }
         if (e.keyCode==65) {
            a_key=true;
         }
         
         
      }
      public function key_up(e:KeyboardEvent) {
         if (e.keyCode==37) {
            left_key = false;
            pj.stop();
         }
         if (e.keyCode==39) {
            right_key = false;
            pj.stop();
         }
         if (e.keyCode==32) {
            space_key=false;
         }
         if (e.keyCode==65) {
            a_key=false;
         }
      }
      
      
      public function DibujarObjetos() {
         terrain_bmp.y=200;//The terrain shouldn't be at the top of the stage!
         stage.addChild(terrain_bmp);//We can make the terrain visible
         
         pj = new Pj();
         pj.x = 200;
         pj.y = 100;
         pj.width = 10;
         pj.height = 20;
         pj.stop();
         stage.addChild(pj);
         abujero.graphics.beginFill(0x000000);//Now we draw the hole. It doesn't matter the colour.
         abujero.graphics.drawCircle(0,0,30);
      }
      
   }

}


Parte del codigo esta copiado de un buen tutorial para crear el terreno y poderlo destruir tipo Worms.

Por Phyronx

41 de clabLevel



 

chrome
Citar            
MensajeEscrito el 16 Ago 2011 07:50 pm
He ido probando cosillas y en el contructor, si borro:

DibujarObjetos(); (Sigue dando error).

Si borro los listeners:


stage.addEventListener(Event.ENTER_FRAME,mover_pj);

stage.addEventListener(MouseEvent.MOUSE_UP,mouse_up);

stage.addEventListener(KeyboardEvent.KEY_DOWN,key_down);

stage.addEventListener(KeyboardEvent.KEY_UP,key_up);

(tambien sigue dando error.)

Pero si borro los 2 a la vez deja de darme error.

Por Phyronx

41 de clabLevel



 

chrome
Citar            
MensajeEscrito el 16 Ago 2011 11:47 pm
He ido probando...

Saque parte de la classe de un tutorial, asi que copie el codigo exacto del tutorial, y daba el mismo error, pero luego he abierto el swf y el fla del tutorial y funciona. Asi que cree una classe nueva con el codigo del tutorial, y he cambiado la classe principal del juego por esta otra, al probarlo... Tampoco funciona!!!

Puede fallar mi archivo fla?

Por Phyronx

41 de clabLevel



 

chrome

 

Cristalab BabyBlue v4 + V4 © 2011 Cristalab
Powered by ClabEngines v4, HTML5, love and ponies.