|
-
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
-
up to my .as in code
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
Last edited by Chris_Seahorn; 07-02-2009 at 09:58 AM.
-
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|