1
votes

I'm creating a dynamic blocked terrain in flash (AS3), and everything goes fine with it, the terrain is correctly placed. But I need to include collisions and I want the blocks to be within a movieclip (sprite), so I can test the collision with the terrain itself.

Ps: I don't know if it would be good to test the collisions with each block individually because I'll use a enterframe function and the block generation is dynamic.

The problem I'm facing is that I have a sprite called blockHolder, but I can't addChild the blocks to it.

Here's the code (I simplified it so we have the blocks being created in cascade if you addChild them into the stage directly, like addChild(clonedSquare).

The error I'm receiving: TypeError: Error #1009: Can't access property or method of a null object reference.

var blockHolder:Sprite = new Sprite();

var clonedSquare = new square();

var lowestPoint:int = 10;
var highestPoint:int = 20;
var areaLenght:int = 10;

function createLvl():void
{
    for (var i:Number = 0; i<(areaLenght); i++)
    {
        clonedSquare = new square();
        clonedSquare.x = i * clonedSquare.width;
        //sets the height of the first block
        if (i == 0)
        {
            var firstY:Number = Math.ceil(Math.random()*((lowestPoint-highestPoint))+highestPoint)*clonedSquare.height;
            clonedSquare.y = firstY;
            trace("terrain begins " + firstY + " px down");
        }
        else
        {
                var previousId:Number = i - 1;
                clonedSquare.y = getChildByName("newSquare"+previousId).y + clonedSquare.height;
        }
        //sets the entity (block) name based on the iteration
        clonedSquare.name = "newSquare" + i;
        //adds the cloned square
        blockHolder.addChild(clonedSquare);
    }
    addChild(blockHolder);
}

createLvl();
1
are you asking why you are getting that error?Ronnie
Before blockHolder.addChild(clonedSquare), what do you get if you trace(blockHolder) ?Simon Germain

1 Answers

0
votes

Well I fixed the error. I am still not clear as to what you're asking for. Basically I add each block to an array and reference the block that way. Your clonedSquare.y = getChildByName("newSquare"+previousId).y + clonedSquare.height; was throwing the error. Also your firstY was placing the first block way off my stage so I just set it to 0 as firstY

var blockHolder:Sprite  = new Sprite();
var squares:Array       = [];
var lowestPoint:int     = 10;
var highestPoint:int    = 20;
var areaLenght:int      = 10;

function createLvl():void
{
    for (var i:Number = 0; i<(areaLenght); i++)
    {
        var clonedSquare = new square();
        clonedSquare.x = i * clonedSquare.width;
        if (i == 0)
        {
            var firstY:Number = Math.ceil(Math.random()*((lowestPoint-highestPoint))+highestPoint)*clonedSquare.height;
            //clonedSquare.y = firstY;
            clonedSquare.y = 0;
            trace("terrain begins " + firstY + " px down");
        }
        else
        {
            clonedSquare.y = squares[i - 1].y + clonedSquare.height;
        }
        blockHolder.addChild(clonedSquare);
        squares.push(clonedSquare);
    }
    addChild(blockHolder);
}
createLvl();