PDA

View Full Version : Display Objects in custom package?



ThaJock
December 18th, 2008, 01:16 AM
I have used the built in packages/classes until now so I'm pretty new at creating custom packages/classes so please work with me. In an effort to clean up my code I thought creating a custom package would be the answer. Before I get too far into details I would like to know if it is possible to refer to display objects that reside in my fla project from a .as file, assuming both files are in the same working directory?

scottc
December 18th, 2008, 01:47 AM
Right click on it in your library, and you can give them a class name etc.

I'm guessing you can write code on that class, or extend it. (i never use the flash library anymore:()

Most the time i just extend Sprite and embed/import things into it.
(edit: or draw with the graphics api)

wvxvw
December 18th, 2008, 01:53 AM
Yes, if you assign the existing AS class file to your library asset then sure you can write code in that file.

ThaJock
December 18th, 2008, 01:56 AM
How do you do that?

scottc
December 18th, 2008, 02:05 AM
goto libary
right click on movieclip -> propperties
tick export for actionscript
type in a name where it says "class"

create a .as file with the same name you just typed in
create the class and constructor with the same name.

eg. for "Cat.as"

package {

public class Cat{//extends MovieClip? not sure

// Constants:

// Public Properties:

// Private Properties:

// Initialization:
public function Cat(){

}
}
}

ThaJock
December 18th, 2008, 02:44 AM
I created a new movie clip, selected export for actionScript in the properties and gave it a class name of theVariables ScottC but something didn't work. I got these errors:

1116: A user-defined namespace attribute can only be used at the top level of a class definition.
5000: The class 'flash.display.Sprite' must subclass 'flash.display.MovieClip' since it is linked to a library symbol of that type.
5000: The class 'flash.display.Sprite' must subclass 'flash.display.MovieClip' since it is linked to a library symbol of that type.

I thing I'm getting somewhere. Here's a look at my theVariables.as file

package{
publec class theVariables {


var huddleHolder:Sprite = new displayHolder();
var tltHolder:Sprite = new displayHolder();
var vidControls:MovieClip = new videoControls();
var invProgOpen:MovieClip = new invProgramOpen();
var invProgClose:MovieClip = new invProgramClose();
var progMenu:MovieClip = new programMenuDesign();
var about_theHuddle:MovieClip = new aboutTheHuddle();
var about_TLT:MovieClip = new aboutTLT();
var theGlass:Sprite = new glass();
var theHuddle_Btn:Sprite = new theHuddleBtn();
var tlt_Btn:Sprite = new tltBtn();
var FX:MovieClip = new soundFX();
var theCalendar:Sprite = new calendar();
var theFooter:Sprite = new footer();
var tlt_Footer:MovieClip = new tltFooter();


public function theVariables(){


}
}
}

Any ideas of what might be wrong?

wvxvw
December 18th, 2008, 05:11 AM
You should:
p#1. change publec to public (that's the first error)
p#2. extend your class from MovieClip or Sprite as long as you're linking it to the MovieClip or Sprite. (the second error)
p#3. type either MovieClip or Sprite in the base class box when exporting your class (depends on which class you extended in p#2). (the last error).

Also keep in mind that all variables without access modifier / namespace specified are placed into internal namespace and you probably won't like it in most cases.
Also, a good coding stile predicts explicitly calling super() in the constructor, likewise giving your classes names with the first capital letter.

ThaJock
December 18th, 2008, 02:44 PM
You should:
p#1. change publec to public (that's the first error)
p#2. extend your class from MovieClip or Sprite as long as you're linking it to the MovieClip or Sprite. (the second error)
p#3. type either MovieClip or Sprite in the base class box when exporting your class (depends on which class you extended in p#2). (the last error).

Also keep in mind that all variables without access modifier / namespace specified are placed into internal namespace and you probably won't like it in most cases.
Also, a good coding stile predicts explicitly calling super() in the constructor, likewise giving your classes names with the first capital letter.


I changed lines 1 and 2 in my .as to read:
package{
public class theVariables extends MovieClip {


So p#1. is now fixed but now I'm getting a new error.
1017: The definition of base class MovieClip was not found.

wvxvw
December 18th, 2008, 03:31 PM
Do you import flash.display.MovieClip in your class?

ThaJock
December 19th, 2008, 04:50 AM
Do you import flash.display.MovieClip in your class?



You nailed it. You have been Xtremely helpful with helping me understand how to create and work with custom classes. Since 24 hours ago I have create 3 that has really helped me clean up my code inside my fla project. There is just one other thing I wanted to ask pertaining to custom classes.

I have a block of code I use for flash video that works perfectly in my flash project and that I'm going to need to recycle so I tried to create package that holds the class for this code. The following code works fine in a fla project:

var nc:NetConnection = new NetConnection();
nc.connect(null);

var ns:NetStream = new NetStream(nc);
ns.client = this;
ns.play ("Part 1.flv");

function onMetaData(info:Object):void {
trace('metaDate recieved');
}

var vid:Video = new Video();
vid.attachNetStream(ns);
addChild(vid);

vid.width = 720;
vid.height = 480;




Here's my attempt to create a custom class for it:

package ThaJock.theVideo {
public class TheVideo {

public function TheVideo () {
var nc:NetConnection = new NetConnection();
nc.connect(null);

var ns:NetStream = new NetStream(nc);
ns.client = this;
ns.play ("Part 1.flv");

function onMetaData(info:Object):void {

}

var vid:Video = new Video();
vid.attachNetStream(ns);
//addChild(vid);

vid.width = 720;
vid.height = 480;


}
}
}

I tested this by deleting the code from the fla, and putting in the following code:

import ThaJock.theVideo.*;
// at this point everything is without errors so I know I have my classPath set correctly

// after I put in the next line I get errors
var myVideo:TheVideo = new TheVideo();

1046: Type was not found or was not a compile-time constant: NetConnection.
1046: Type was not found or was not a compile-time constant: NetStream.
1046: Type was not found or was not a compile-time constant: Video.
1180: Call to a possibly undefined method NetConnection.
1180: Call to a possibly undefined method NetStream.
1180: Call to a possibly undefined method Video.

if ( you get a chance to take a look at it == ture){
trace( 'I would greatly appreciate it);
}
else { trace('Thank you for getting me in the right direction'); }

My sincerest Thanks.

scottc
December 19th, 2008, 05:42 AM
1046: Type was not found or was not a compile-time constant: NetConnection.
1046: Type was not found or was not a compile-time constant: NetStream.
1046: Type was not found or was not a compile-time constant: Video.
1180: Call to a possibly undefined method NetConnection.
1180: Call to a possibly undefined method NetStream.
1180: Call to a possibly undefined method Video.


import the relevant classes...

you need to import:
NetConnection
NetStream
Video

They should be located in somewhere like...
import flash.net.*;
(not 100% sure)

Just keep in mind whenever you use NetConnection (or any class for that matter), you need to also import it.

(With the exceptions of working on the timeline, classes that are in the root directory of your project folder and those global classes like Math.random()).

ThaJock
December 23rd, 2008, 02:21 AM
Thanks guy for all your help. You got me all fired up. Now I'm going to go back and create custom packages for some other projects that I reuse over and over.

So the next time I need to place a Dynamic Video Playlist or MP3 player in a project, it should be a breeze.

Thanks Again.