I just want to create a class with two Numbers and a Bitmap. You can't have nested classes in a Timeline script so I figured I'd make .as file, namely "node.as". But I cannot for the life of me figure out how to import this class into the Timeline script so that I can use the class in my Timeline script.
Please help!
You should be able to create a package to accomplish this. The basic format is:
// folder specifies the relative folder of the package
package myfolder {
// whatever other classes you need
import flash.display.*;
import flash.events.*;
// base this on whatever class you need (in this case MovieClip)
public class myclass extends MovieClip {
private var myvariable: String;
// constructor
public function myclass() {
}
// functions only for use within the class
private function myprivatefunction() {
}
// functions for use by the rest of the world
public function mypublicfunction() {
}
}
}
The constructor is called when you create a new instance of the Class-- this is where you would do your initialization. Then, in your timeline, just add something like:
import myclass;
var myclassinstance = new myclass();
myclassinstance.mypublicfunction(); // call a function in the class
See adobe's help for more details.
So I tried this:
package {
import flash.display.*;
public class Node {
public var fX:Number;
public var fY:Number;
public var bmp:Bitmap;
public function Node() { }
}
}
And put it in a file called Node.as (note I needed to add the flash.display import, and the capital "N" in the filename). In a FLA file in the same directory, I added this to the first frame script:
import Node;
var node = new Node();
trace("node="+node);
It compiled and ran without error. There must be something else going on? What version of Flash and of ActionScript are you using?
Related
i am loading in a external AS file directly into the main timeline IDE using the "include" method with some variables and calling another Class within the AS file for example;
include "vars.as"
contents of "vars.as":
var test:* = "test?!";
var foo:FOO = new FOO();
addChild(foo);
contents of "FOO.as":
package {
import flash.display.*;
public class FOO extends MovieClip {
public function FOO() {
trace("test= "+test);
}
}
}
error;
1120: Access of undefined property test.
how can i access the "test" variable inside "vars.as" from the "foo" class, is that possible?
Included actionscript files, during compilation, are treated as though they were right in the body of the actionscript you included them in. You can just access them normally as though you defined them in the same place.
trace(test); //test
ok so i found a solution, hope this helps someone else out there.
this is how you should call it from the main timeline;
var foo:FOO = new FOO(this, stage);
addChild(foo);
and here is the updated FOO class:
package {
import flash.display.*;
public class FOO extends MovieClip {
var _stage:Stage;
var _root:*;
public function FOO(root:*, stage:Stage){
this._stage = stage;
this._root = root;
trace(this._root.test);
}
}
}
I am a beginner of action script. working with button in action script class file not working.
i have created two file one is stream.as and another is main.as
main.as is the main class file of my frame.
i have drawed a button and converted it in button and gave instance name play_btn.
but the compiler giving me 1120: access of undefined property play_btn.
the both codes are given below;
main.as
package {
import flash.display.MovieClip;
import stream.stream;
public class main extends stream {
public function main() {
}
// constructor code
}
}
stream.as
package {
import flash.events.MouseEvent;
import flash.display.MovieClip;
public class stream extends MovieClip {
public function main() {
play_btn.addEventListener(MouseEvent.CLICK, pausevedio);
function pausevedio(event:MouseEvent):void{
play_btn.visible=false;
}
// constructor code
}
}
}
Correct me if I'm wrong but it's because the play_btn belongs solely to your main class and you're trying to access it through the stream class, to do this properly try to instantiate it through code in the class you wish to use it in instead of on the timeline like so:
playBtn: play_btn = new play_btn();
playBtn.x = x;
playBtn.y = y;
addChild(playBtn);
This is my best guess I'm not exactly sure how your classes are structured but this might be your issue. I hope this helps (I'm new too but I figured my two-cents might help!)!
~Cheers!
i'm trying to create a flash game, but i'm having trouble with my classes. i'm importing the classes on the main script. they're imported okay, i've tested it all. but i dont know how to use classes inside another class
this is what i've got so far:
class Class.Player {
public static var self:MovieClip;
public static var bullet:Class.Bullet;
function controls() {
//Shoot
if (Key.isDown(Key.SPACE)) {
bullet = new Bullet(100, 100);
}
}
it loads the class and declares the variable, but it claims "Bullet" is not an existing method. it is an existing method, and it works when i call it from the main script.
also, do i need to declare the class like public static var bullet:Class.Bullet;? it's the only way that works for me but wondered if there's a better way?
It looks like you are having problems with the naming of your classes. You don't have to call your classes Class.Name. Just use Name.
In your case, you could have a file called
Player.as
With this content:
package {
import flash.display.MovieClip;
public class Player extends MovieClip {
public private var bullet:Bullet;
public function Player(){
//constructor
}
function controls() {
//Shoot
if (Key.isDown(Key.SPACE)) {
bullet = new Bullet(100, 100);
}
}
}
}
And then a file called
Bullet.as
With this content:
package {
import flash.display.MovieClip;
public class Bullet extends MovieClip {
public function Bullet(x:Number, y:Number){
//constructor
}
}
}
You can read more information about creating and using custom classes here: http://www.flashandmath.com/bridge/intro/class.html
You're putting your package information in the Class name. I have no idea how this is even compiling for you (especially given that Class is a reserved word.
Your files should look more like:
package somePackage {//somePackage instead of the reserved word Class you were using
import someOtherPackage.Bullet;
public class Player extends MovieClip {
public var bullet:Bullet;
public function Player():void {
super();
//other constructor logic here
}
}
}
I have two questions: Can i write app using just *.as files, and then compile them somehow in SWF? (i am making myself a webpage now)
Secondly, please look at the code, my problem is the same - I can't render text field onto stage, to be visible.
Flash say's 'undefined method addChild.' and 'Access of undefined property tekstuKaste through a reference with static type Class.'
This is a constructor type class inic which kinda serves for initialization, cos all i do is, I make an instance OF THIS class in FLA file by ActionScript, and expect, all application to work.
package {
import pacinas.visuals.*;
import pacinas.visuals.AE_kanva;
public class inic {
public function inic() {
trace("===========");
trace("inicializēt un izsaukt visu no Kanvas klases");
trace("===========");
trace(" ");
var kanvas:AE_kanva = new AE_kanva();
trace(" ");
kanvas.varis();
trace(" ");
trace("===========");
trace("inicializēt un izsaukt visu no Lauki klases");
trace("===========");
trace(" ");
var laukiTxt:BE_tekstaLaukiPrimitive = new BE_tekstaLaukiPrimitive();
trace("");
laukiTxt.simpleText();
addChild(BE_tekstaLaukiPrimitive.tekstuKaste);
}
}
}
There is another EXTERNAL CLASS By whom i hoped to place a rectangles - that does not work too. Example:
package pacinas.visuals
{
import flash.display.Sprite;
public class AE_kanva extends Sprite
{
public function AE_kanva()
{
var kvad:Shape = new Shape();
kvad.graphics.beginFill(0xFF0000);
kvad.graphics.drawRect(0, 0, 100,100);
kvad.graphics.endFill();
addChild(kvad);
trace("konstruktors - zīmē kanvu");
}
public function varis()
{
trace("te glabaas variaabljus");
var ff:int = 4;
var dd:int = 8;
}
}
}
And here is class i hoped will make text box for me (to fill it with XML later)
package pacinas.visuals
{
import flash.text.*;
import flash.display.Sprite;
public class BE_tekstaLaukiPrimitive extends Sprite
{
public var tekstuKaste:TextField = new TextField();
private var kontinents:String = new String ("XML SATURU CMON! a123");
public function BE_tekstaLaukiPrimitive():void
{
trace("teksta rāmis = konstruktora klase");
addChild(tekstuKaste); <--CAN'T GET THIS TO WORK!!!
tekstuKaste.text = kontinents;
}
public function simpleText()
{
trace("nekonstruktora f-cija no Teksta lauki");
}
}}
p.s. I do not use document Class. Ok I will if it's needed. But how?
Can I write app using just *.as files, and then compile them somehow into a SWF?
Yes - using the Flex SDK you can write pure ActionScript and compile it down into a working SWF. FlashDevelop is a good IDE that takes advantage of this.
You will however need to understand how the document class works.
Flash says undefined method addChild. and Access of undefined property tekstuKaste through a reference with static type Class.
In your code, this line is causing your issue:
addChild(BE_tekstaLaukiPrimitive.tekstuKaste);
The first error undefined method addChild is because your class inic does not extend a class that implements the method addChild(). DisplayObjectContainer defines this method, so you'll want to extend that as a minimum, like this:
public class inic extends DisplayObjectContainer
The second error is because you're attempting to access a property of the class BE_tekstaLaukiPrimitive as if it were static. I suspect what you actually wanted to do was this:
addChild(laukiTxt); // laukiTxt is the instance you created.
I am trying to pass a variable to a method in one of my Classes so I can use it to create the correct movieClip (image)
My Class code looks like this:
package {
import flash.display.MovieClip;
import flash.display.Sprite;
public class SlideShow extends MovieClip{
public function SlideShow()
{
//does something on start
}
//This function should take the string and use it as the class name below.
public function addImages(BackGround:String):void
{
trace(BackGround);
var main_bg:BackGround = new BackGround();
addChild(main_bg);
}
}
}
and when I call the method from my maintimeline it looks like this:
var shoeSlide:SlideShow = new SlideShow();
shoeSlide.addImages("menPant");
SO "menPant" is acually the name I assigned to a class of a movieclip that has some images in it.
I am getting the following error:
SlideShow.as, Line 30 1046: Type was not found or was not a compile-time constant: BackGround.
make sure you import getDefinitionByName at the top of your class code if flash doesn't do it for you automatically. This should work.
public function addImages(BackGround:String):void
{
var symbol_class:Class = getDefinitionByName(BackGround);
//EDIT: removed data type :BackGround -- this will give an error.
var main_bg = new symbol_class();
addChild(main_bg);
}