Código:
He ido probando cosillas,ir borrando funciones haber si fallaba en alguna, y al final en el contructor, si borro: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); } } }
DibujarObjetos();
(Sigue dando error).
Si borro los listeners:
stage.addEventListener(Event.ENTER_FRAME,mover_pj) ;
stage.addEventListener(MouseEvent.MOUSE_UP,mouse_u p);
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.
Haber si me podeis ayudar. El error es el siguiente:
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()
Si borro del constructor DibujarObjetos(); , tambien desaparece del error quedando solo at Lucha().