A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Learning AS3 with KM - simple Question

  1. #1
    Junior Member
    Join Date
    Jan 2007
    Location
    Blue Mountains, Australia
    Posts
    21

    Learning AS3 with KM - simple Question

    Hi

    I'm new to AS3 and I still haven't got the basics yet, so I need a point in the right direction ...

    I was following the instructions on how to create classes and re-using them, but I still don't understand how to import the class.

    For example I have this code as per example ....

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

    public class ButtonInteractivity extends Sprite {

    var button:Sprite = new Sprite();

    public function ButtonInteractivity() {
    drawButton()
    button.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    addChild(button);
    }

    function drawButton():void {
    var textLabel:TextField = new TextField()
    button.graphics.clear();
    button.graphics.beginFill(0xD4D4D4); // grey color
    button.graphics.drawRoundRect(0, 0, 80, 25, 10, 10); // x, y, width, height, ellipseW, ellipseH
    button.graphics.endFill();
    textLabel.text = "Click Me!";
    textLabel.x = 10;
    textLabel.y = 5;
    textLabel.selectable = false;
    button.addChild(textLabel)
    }

    function mouseDownHandler(event:MouseEvent):void {
    button.x += 20
    if (button.x > 400) { button.x = 0}
    }
    }
    }

    saved as ButtonInteractivity.as

    I then create a new KM file and added the following code as per example ..
    but added ...

    import flash.ButtonInteractivity.*;

    in the first line ...

    var b=new Array();
    var h=30;
    for (var x:integer;x<10;x++){
    b[x]=new ButtonInteractivity();
    b[x].y=h;
    addChild(b[x]);
    h+=30;
    }

    KM Fle saved in the same folder as ButtonInteractivity.as

    When I run the program I get the following error message ..


    Class ButtonInteractivity
    Line 36: stop();
    expecting a package

    Did I leave something out or is my import statement incorrect ?

    Thanks for assisting.
    Cheers

  2. #2
    up to my .as in code Chris_Seahorn's Avatar
    Join Date
    Dec 2004
    Posts
    4,389
    First, your class has an error and should be:

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

    public class ButtonInteractivity extends Sprite {

    var button:Sprite = new Sprite();

    public function ButtonInteractivity() {
    drawButton()
    button.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    addChild(button);
    }

    private function drawButton():void {
    var textLabel:TextField = new TextField()
    button.graphics.clear();
    button.graphics.beginFill(0xD4D4D4); // grey color
    button.graphics.drawRoundRect(0, 0, 80, 25, 10, 10); // x, y, width, height, ellipseW, ellipseH
    button.graphics.endFill();
    textLabel.text = "Click Me!";
    textLabel.x = 10;
    textLabel.y = 5;
    textLabel.selectable = false;
    button.addChild(textLabel)
    }

    function mouseDownHandler(event:MouseEvent):void {
    button.x += 20
    if (button.x > 400) { button.x = 0}
    }
    }
    }

    If you are saving the class in the same folder, you do not need an import statement, Koolmoves will look in the root folder automatically. That said your KM source would go like this (notice the changes in bold):

    var b=new Array();
    var h=30;
    for (var x:int=0;x<10;x++){
    b[x]=new ButtonInteractivity();
    b[x].y=h;
    addChild(b[x]);
    h+=30;
    }

    Already tested here and confirmed

  3. #3
    Junior Member
    Join Date
    Jan 2007
    Location
    Blue Mountains, Australia
    Posts
    21

    Smile v7-AS3 Learning AS3 with KM - simple Question

    Thanks for your help, that worked.
    Cheers

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