Colin Brogan
Colin Brogan

Reputation: 748

display lines in AS3

I am baffled by this function, which is called prior to this with parameters 22 and 58 for xVal ad yVal respectively. It doesn't display anything when the swf is compiled and tested, and it's error free. The code is in the document class:

    private function mLine(xVal : int, yVal : int) {
        var rCol = 0x0000FF;
        var incr = Math.round((Math.random() * 20) + 8);
        lns.push(new Shape());
        var i = lns.length - 1;

        this.addChild(lns[i]);
        lns[i].graphics.moveTo(xVal, yVal);
        lns[i].graphics.lineStyle(10, rCol);
        lns[i].graphics.lineTo(xVal, yVal + 20);
        lns[i].name = incr;
        trace("lns[" + i + "] x is " + lns[i].x); // outputs 'lns[0] x is 0'
        trace("xVal is " + xVal); // outputs 'xVal is 22'
        trace("yVal is " + yVal); //outputs 'yVal is 58'
        trace(stage.contains(lns[i])); // outputs 'true'
    }

Upvotes: 1

Views: 138

Answers (2)

Adam Harte
Adam Harte

Reputation: 10500

Your code seems like it should work. I have rewrote it to conform better to ActionScript 3 best practices

private function drawLine(xVal:int, yVal:int):void 
{
    var lineColor:uint = 0x0000FF;

    var lineShape:Shape = new Shape();
    //lineShape.name = String(Math.round((Math.random() * 20) + 8));
    lineShape.graphics.lineStyle(10, lineColor);
    lineShape.graphics.moveTo(xVal, yVal);
    lineShape.graphics.lineTo(xVal, yVal + 20);
    addChild(lineShape);
    lines.push(lineShape);
}

The x and y properties of your shape will both be zero because you never set them. you are just drawing lines inside the shape at the xVal and yVal. You could do the same thing like this:

private function mLine(xVal:int, yVal:int) 
{
    var lineColor:uint = 0x0000FF;

    var lineShape:Shape = new Shape();
    //lineShape.name = String(Math.round((Math.random() * 20) + 8));
    lineShape.graphics.lineStyle(10, lineColor);
    lineShape.graphics.moveTo(0, 0);
    lineShape.graphics.lineTo(0, 20);
    lineShape.x = xVal;
    lineShape.y = yVal;
    addChild(lineShape);
    lines.push(lineShape);
}

Not sure why its not showing up at all for you though.

Upvotes: 0

kapex
kapex

Reputation: 29949

Assuming you have declared private var lns = []; somewhere, it draws a blue line (20px straight down from the given position).

It doesn't display anything

That means you probably don't have an object of that class on the stage. In your document class, you should use addChild to display an instance of the class containing mLine. mLine needs to be called somehow obviously. You could do this in the class' constructor, but you'd need to remove the last trace statement to avoid a null pointer error, because stage would be null then.

Edit: Missed that you said it is in the Document class. So, try and see if drawing anything else works. The problem doesn't seem to be with this function.

Upvotes: 1

Related Questions