AS3 example Positioning: Difference between revisions

The educational technology and digital learning wiki
Jump to navigation Jump to search
(→‎Variants: added variant example)
Line 236: Line 236:
== Variants ==
== Variants ==


... (need to find another drawing to be done with the 3 rectangles, replace all "transformIntoCart" elements with this new "showXXX" one). ...
After ''addChild(button2)'', add:
 
          var button3:Sprite;
          button3 = simpleButton("computer");
          button3.addEventListener(MouseEvent.MOUSE_DOWN, transformIntoComputer);
          button3.x = 260;
          addChild(button3)
 
After the closing bracket of the ''transformIntoFlag()'' function, add:
 
        private function transformIntoComputer(event:MouseEvent):void {
          redRect.x = 0
          whiteHoriz.x = 0
         
          redRect.x = 0
          whiteHoriz.y = 70
         
          whiteVert.visible = false
          whiteHoriz.visible = true
          redRect.visible = true
        }


== Challenge ==
== Challenge ==

Revision as of 14:40, 3 November 2007

Program

Copy this text and paste it into a new text file. Save the text file as "Transformations.as".

package {
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.events.MouseEvent;

    public class Transformations extends Sprite {

        private var sprite:Sprite;
        private var redRect:Sprite;
        private var whiteVert:Sprite;
        private var whiteHoriz:Sprite;

        public function Transformations() {

          redRect = filledRectangle(150,100,0xFF0000)  // width, height, red color
          whiteVert = filledRectangle(20,65,0xFFFFFF)  // width, height, white color
          whiteHoriz = filledRectangle(65,20,0xFFFFFF)  // width, height, white color
           
          sprite = new Sprite();
          sprite.addChild(redRect)
          sprite.addChild(whiteVert)
          sprite.addChild(whiteHoriz)
           
          sprite.x = 100
          sprite.y = 80
          addChild(sprite)
           
          var button1:Sprite;
          button1 = simpleButton("cart from above");
          button1.addEventListener(MouseEvent.MOUSE_DOWN, transformIntoCart);
          addChild(button1)

          var button2:Sprite;
          button2 = simpleButton("swiss flag");
          button2.addEventListener(MouseEvent.MOUSE_DOWN, transformIntoFlag);
          button2.x = 130;
          addChild(button2)

        }

        private function transformIntoCart(event:MouseEvent):void {
          whiteVert.x = 0
          whiteHoriz.x = 25
          redRect.x = 125

          whiteVert.y = 0
          whiteHoriz.y = 0
          redRect.y = 0
 
          whiteVert.visible = true
          whiteHoriz.visible = true
          redRect.visible = true
 
       }

        private function transformIntoFlag(event:MouseEvent):void {
          whiteVert.x = 0
          whiteHoriz.x = 0
          redRect.x = 0

          whiteVert.y = 0
          whiteHoriz.y = 0
          redRect.y = 0

          whiteVert.visible = true
          whiteHoriz.visible = true
          redRect.visible = true
        }

        private function filledRectangle(w:uint, h:uint, color:uint):Sprite {
          var r:Sprite = new Sprite()
          r.graphics.beginFill(color);  
          r.graphics.drawRect(-w/2, -h/2, w, h);
          r.graphics.endFill();
          return r;
        }

      private function simpleButton(label:String):Sprite {
        var sprite:Sprite = new Sprite();
        var textLabel:TextField = new TextField()
        sprite.graphics.clear();
        sprite.graphics.beginFill(0xD4D4D4); // grey color
        sprite.graphics.drawRoundRect(0, 0, 105, 25, 10, 10); // x, y, width, height, ellipseW, ellipseH
        sprite.graphics.endFill();
        textLabel.text = label;
        textLabel.x = 10;
        textLabel.y = 5;
        textLabel.selectable = false;
        sprite.addChild(textLabel)
        return sprite; 
       }         
    }
}

Walkthrough

---

package {
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.events.MouseEvent;

    public class Transformations extends Sprite {

Okay, we start to get used to this. Package definition, importing the class definitions for all objects that we will use in this program, definition for the Transformations class that follows.

---

        private var sprite:Sprite;
        private var redRect:Sprite;
        private var whiteVert:Sprite;
        private var whiteHoriz:Sprite;

Variables that will be used in multiple places in the program need to be declared right after the class definition, with a private var syntax.

---

        public function Transformations() {

Class constructor. The enclosed code gets executed each time the class gets instantiated.

---

          redRect = filledRectangle(150,100,0xFF0000)  // width, height, red color
          whiteVert = filledRectangle(20,65,0xFFFFFF)  // width, height, white color
          whiteHoriz = filledRectangle(65,20,0xFFFFFF)  // width, height, white color

The default graphic is a swiss flag. We need three filled rectangles to make up a swiss flag. Three times, we need to create a filled rectangle. If we were to repeat the code to make one filled rectangle three times, this would clutter our program, making it difficult to read. We delegate the creation of a filled rectangle (stored within a sprite object) to a function written in the same class.

---

          sprite = new Sprite();
          sprite.addChild(redRect)
          sprite.addChild(whiteVert)
          sprite.addChild(whiteHoriz)

we put the graphic elements within a sprite. This way, we have the ability to treat the flag graphic as one entity that can be manipulated on its own. ---

          sprite.x = 100
          sprite.y = 80

We reposition the graphic somewhere in the middle of the screen. All childs of the sprite objects get moved with the sprite object.

---

          addChild(sprite)

We don't forget to use the addChild() construct to make sure that the flag get drawn on the screen.

---

          var button1:Sprite;
          button1 = simpleButton("cart from above");
          button1.addEventListener(MouseEvent.MOUSE_DOWN, transformIntoCart);
          addChild(button1)

          var button2:Sprite;
          button2 = simpleButton("swiss flag");
          button2.addEventListener(MouseEvent.MOUSE_DOWN, transformIntoFlag);
          button2.x = 130;
          addChild(button2)

We define two buttons. Each button will call a given function, either transformIntoCart or transformIntoFlag. It goes the same for the buttons as for the rectangles. We delegate the task of creating a button-like graphic to a function.

---

        }

End of the constructor function

---

        private function transformIntoCart(event:MouseEvent):void {
          whiteVert.x = 0
          whiteHoriz.x = 25
          redRect.x = 125

          whiteVert.y = 0
          whiteHoriz.y = 0
          redRect.y = 0

         whiteVert.visible = true
         whiteHoriz.visible = true
         redRect.visible = true
        }

This function gets executed whenever the button "show cart" gets pressed. The function does nothing but change the position of the 3 rectangles. ---

        private function transformIntoFlag(event:MouseEvent):void {
          whiteVert.x = 0
          whiteHoriz.x = 0
          redRect.x = 0

          whiteVert.y = 0
          whiteHoriz.y = 0
          redRect.y = 0

         whiteVert.visible = true
         whiteHoriz.visible = true
         redRect.visible = true
        }

This function gets executed whenever the button "show flag" gets pressed. The function does nothing but change the position of the 3 rectangles.


---

        private function filledRectangle(w:uint, h:uint, color:uint):Sprite {
          var r:Sprite = new Sprite()
          r.graphics.beginFill(color);  
          r.graphics.drawRect(-w/2, -h/2, w, h);
          r.graphics.endFill();
          return r;
        }

By using r.graphics.drawRect(-w/2, -h/2, w, h);, all rectangles get drawn with their center as point of origin (0,0)

---

      private function simpleButton(label:String):Sprite {
        var sprite:Sprite = new Sprite();
        var textLabel:TextField = new TextField()
        sprite.graphics.clear();
        sprite.graphics.beginFill(0xD4D4D4); // grey color
        sprite.graphics.drawRoundRect(0, 0, 105, 25, 10, 10); // x, y, width, height, ellipseW, ellipseH
        sprite.graphics.endFill();
        textLabel.text = label;
        textLabel.x = 10;
        textLabel.y = 5;
        textLabel.selectable = false;
        sprite.addChild(textLabel)
        return sprite; 
       }         

---

    }
}

Closing brackets that have been opened (class definition and package).

Variants

After addChild(button2), add:

         var button3:Sprite;
         button3 = simpleButton("computer");
         button3.addEventListener(MouseEvent.MOUSE_DOWN, transformIntoComputer);
         button3.x = 260;
         addChild(button3)

After the closing bracket of the transformIntoFlag() function, add:

       private function transformIntoComputer(event:MouseEvent):void {
         redRect.x = 0
         whiteHoriz.x = 0
         
         redRect.x = 0
         whiteHoriz.y = 70
         
         whiteVert.visible = false
         whiteHoriz.visible = true
         redRect.visible = true
       }

Challenge

When I was a kid, there were some popular jokes around circles on a sheet. Circles are not supposed to be any funny, I agree. What was funny about that was that a drawing that looked like nothing but circles toke a completely different meaning when given an interpretation.

Google search and here are some smiley versions of it.

-O-     A Mexican on a bicycle 
-O-O-   Two Mexicans on a tandem
O-o     A mexican cooking a pancake
=O=     A Mexican on skis
^O,     A mexican swimming crawl style  (frontal view)		

And many more that I have forgotten.

Try to come up with graphs that involve simple shapes like circles and rectangles and that correspond to object "viewed from the sky".

If you come up with some cool stuff, why not upload a photo on the AS3edu Flicker group. As always when you put material on the web, avoid any material of a nature to shock an underage age kid.