A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Simple Code Sippets

Hybrid View

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    9

    Lightbulb Simple Code Sippets

    Please help me with this:

    Write code snippets for the following:
    a. create two square of different colours.(Done)
    b. add two buttons(labeled forward and backward) to the stage (Done)
    c. add a listener to the forward button. The listener function should move whichever of the square to the front.
    d. add a listener to the backward button. The listener function should move whichever of the square to the back.

    Code:
    //1st Square
    
    graphics.lineStyle(2,0x000000);
    graphics.beginFill(0x666699)
    graphics.moveTo(10,10);
     
    graphics.lineTo(10,100);
    graphics.lineTo(100,100);
    graphics.lineTo(100,10);
     
    
    
    //2nd Square
    graphics.lineStyle(2, 0x000000); 
    graphics.beginFill(0xDDDCCC); 
    graphics.moveTo(80, 100); 
    
    graphics.lineTo(80, 550);
    graphics.lineTo(400, 500);
    graphics.lineTo(400, 100);
    Help me with C and D Please

    Thank You

  2. #2
    Senior Member
    Join Date
    Jun 2008
    Posts
    549
    Firstly, you need to move the square code into sprite containers. Then use the swapChildren() method to swap the z-index of the containers. More info on the swapChildren() here.

    Code:
    //1st Square
    var s1:Sprite = new Sprite;
    s1.graphics.lineStyle(2,0x000000);
    s1.graphics.beginFill(0x666699)
    s1.graphics.moveTo(10,10);
    s1.graphics.lineTo(10,100);
    s1.graphics.lineTo(100,100);
    s1.graphics.lineTo(100,10);
    s1.graphics.endFill();
    addChild(s1);
     
    
    //2nd Square
    var s2:Sprite = new Sprite();
    s2.graphics.lineStyle(2, 0x000000); 
    s2.graphics.beginFill(0xDDDCCC); 
    s2.graphics.moveTo(80, 50); 
    s2.graphics.lineTo(80, 550);
    s2.graphics.lineTo(400, 500);
    s2.graphics.lineTo(400, 100);
    s2.graphics.endFill();
    addChild(s2);
    
    
    backBtn.addEventListener(MouseEvent.CLICK, backHandler);
    forwardBtn.addEventListener(MouseEvent.CLICK, forwardHandler);
    
    function backHandler(e:MouseEvent):void
    {
    	swapChildren(s1, s2);
    }
    
    function forwardHandler(e:MouseEvent):void
    {
    	swapChildren(s2, s1);
    }

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center