Search code examples
actionscript-3flashactionscript

AS3: graphics API not working as expected


I've been working on an AS3 project, but since I usually work in php/javascript, this language is new to me and I have come across a problem that I can't figure out how to solve. I thought i'd ask it here because it's probably super simple and i just missed something (really) basic.

Here's my code in my external AS3 file:

package  {

    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import flash.events.Event;
    import flash.display.Shape;
    import flash.display.Graphics;

    public class lightstage_debug extends MovieClip {
        //create drag functions for tools
        public function dragMirror1(event:MouseEvent):void
        {
            if (stage.contains(mirror1))
            {
                mirror1.startDrag();
            }
        }
        public function releaseMirror1(event:MouseEvent):void
        {
            if (stage.contains(mirror1))
            {
                mirror1.stopDrag();
            }

        }
        /*********************************************************************************************
        TODO: make mirror2, mirror3 etc drag function exist in code even when those mirrors don't exist
        **********************************************************************************************/
        public function reCheck(event:Event):void
        {
            //BEGIN LEVEL 1 RECHECK//
            if (level.number == 1)
            {
                //check if mirror1 is touching line1
                if (mirror1.hitTestObject(line1))
                {
                    //redraw line 1 to stop at mirror1
                    /*************************************************************************************
                    TODO: MAKE LINE1 END **EXACTLY** WHERE MIRROR1 TOUCHES IT
                    **************************************************************************************/
                    line1.graphics.clear();
                    line1.graphics.lineStyle(2,0x000000,1);
                    line1.graphics.moveTo(0,200);
                    line1.graphics.lineTo((mirror1.x + (mirror1.width/2)),200);

                    //redraw line 2 to start at mirror1
                    line2.graphics.clear();
                    line2.graphics.lineStyle(2,0x000000,1);
                    line2.graphics.moveTo((mirror1.x + (mirror1.width/2)),200);
                    line2.graphics.lineTo(0,200);

                    lines.addChild(line2);

                    //show line2
                    //line2.visible = true;
                    //line2.alpha = 100;
                    trace('line didnt appear');

                }
                //run this if line1 is not touching mirror1
                else
                {
                    //redraw line 1 to reach the end of the stage
                    line1.graphics.clear();
                    line1.graphics.lineStyle(2,0x000000,1);
                    line1.graphics.moveTo(0,200);
                    line1.graphics.lineTo(550,200);

                    //hide line2
                    //line2.alpha = 0;
                }
            }
            //END LEVEL 1 RECHECK//
        }
    }

}

Here is the code in my .FLA file:

//import libraries
import flash.display.Shape;
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.Graphics;

//create variables
var globe1:MovieClip = new globe;
var mirror1:MovieClip = new mirror;

//create display containers
var lines:MovieClip = new MovieClip;
var mirrors:MovieClip = new MovieClip;
var concaves:MovieClip = new MovieClip;
var convexes:MovieClip = new MovieClip;
var globes:MovieClip = new MovieClip;

//create shapes for this level
var line1:Shape = new Shape;
var line2:Shape = new Shape;

//add display containers to the stage
addChild(lines);
addChild(mirrors);
addChild(concaves);
addChild(convexes);
addChild(globes);

//create level object to store level properties
var level:Object = new Object;

//create scene properties
level.mirrors = 1;
level.concaves = 0;
level.convexes = 0;
level.globes = 1;
level.number = 1;

//add all lines to the stage
lines.addChild(line1);
lines.addChild(line2);

//draw line1
line1.graphics.clear();
line1.graphics.lineStyle(2, 0x000000, 1);
line1.graphics.moveTo(0,200);
line1.graphics.lineTo(550, 200);

//4DEBUG: testing to see why line2 dosent appear
line2.graphics.clear();
//add mirrors
mirrors.addChild(mirror1);

//add concaves

//add convexes

//add globes
globes.addChild(globe1);

//position mirrors
mirror1.x = 340;
mirror1.y = 300;

//position globes
globe1.x = 125;
globe1.y = 50;

//create listeners
mirror1.addEventListener(MouseEvent.MOUSE_DOWN, dragMirror1);
mirror1.addEventListener(MouseEvent.MOUSE_UP, releaseMirror1);
stage.addEventListener(Event.ENTER_FRAME, reCheck);

My stage is empty and I only have one frame at the moment. The globe and mirror movieclips are in my library with the AS3 linkage names I used to create them in the code.

I'm trying to create something like described at http://raphaelhennessy.com/misc/Explanation.png (ignore everything below the text 'LightStage Explanation') - but my problem is that although i can get the initial line (line1) to 'shrink' and stop at the mirror when the mirror is placed on top of the line, line2 does not appear and start at the mirror, going upwards to the top of the stage like intended.

Thanks in advance,

-Raph


Solution

  • It looks like you want line2.graphics.lineTo(0,200); to be line2.graphics.lineTo((mirror1.x + (mirror1.width/2)), 0);. Right now you're just drawing the lines on top of each other, in opposite directions.