Hi All,
I´m stucked with “Cannot access a property or method of a null object reference”
I did a small test inside the IDE and it works fine
var _container:Sprite;
var holder:Sprite = new Sprite();
addChild( holder );
PainelNumeros( holder );
function PainelNumeros(mc:Sprite)
{
_container = new Sprite();
draw();
mc.addChild(_container);
}
function draw():void
{
var origX = 23;
var origY = 126.5;
var deltaX = 24.5;
var deltaY = 17.9;
var larguraBox = 17.8;
var alturaBox = 9.4;
var posX = origX;
var posY = origY;
var elemento = 0;
for (var i = 1; i <= 10; i++)
{
for (var j = 1; j <= 10; j++)
{
var nome = "numero" + elemento ;
_container.addChild( new SpriteBox() ).name = nome;
_container.getChildByName( nome ).x = posX;
_container.getChildByName( nome ).y = posY;
_container.getChildByName( nome ).width = larguraBox;
_container.getChildByName( nome ).height = alturaBox;
_container.getChildByName( nome ).alpha = 0.5;
_container.getChildByName( nome ).addEventListener(MouseEvent.CLICK, mouseClickHandler);
posX += deltaX;
elemento++;
}
posX = origX;
posY += deltaY;
}
}
function mouseClickHandler(event:MouseEvent)
{
trace( event.target.parent.name );
}
................................................................
package
{
import flash.display.*;
import flash.events.*;
public class SpriteBox extends Sprite
{
private var size:uint = 50 ;
private var status:Boolean;
private var bgColor:uint = 0xFFCC00;
public function SpriteBox()
{
var child:Sprite = new Sprite();
status = false;
draw(child);
addChild(child);
}
private function draw(sprite:Sprite):void
{
sprite.graphics.beginFill(bgColor);
sprite.graphics.drawRect(0, 0, size, size);
sprite.graphics.endFill();
}
}
}
Then I tryed to translate what I got through the IDE to a new class
package
{
import flash.display.*;
import flash.events.*;
public class PainelNumeros
{
private var _container:Sprite;
public function PainelNumeros(mc:Sprite)
{
var _container:Sprite = new Sprite();
draw();
mc.addChild(_container);
}
private function draw():void
{
var origX = 23;
var origY = 126.5;
var deltaX = 24.5;
var deltaY = 17.9;
var larguraBox = 17.8;
var alturaBox = 9.4;
var posX = origX;
var posY = origY;
var elemento = 0;
for (var i = 1; i <= 10; i++)
{
for (var j = 1; j <= 10; j++)
{
var marca:SpriteBox = new SpriteBox();
var nome = "numero" + elemento;
_container.addChild( new SpriteBox() ).name = nome;
_container.getChildByName( nome ).x = posX;
_container.getChildByName( nome ).y = posY;
_container.getChildByName( nome ).width = larguraBox;
_container.getChildByName( nome ).height = alturaBox;
_container.getChildByName( nome ).alpha = 0.5;
_container.getChildByName(nome).addEventListener(MouseEvent.CLICK, mouseClickHandler);
posX += deltaX;
elemento++;
}
posX = origX;
posY += deltaY;
}
}
private function mouseClickHandler(event:MouseEvent)
{
trace( event.target.parent.name );
}
}
}
and tryed to instantiate it in the IDE
var holder:Sprite = new Sprite();
addChild( holder ) ;
var painel1:PainelNumeros = new PainelNumeros ( holder ) ;
Where I get the error, what am I doing wrong ?
thanks in advance
jcarlos