PDA

View Full Version : Confusion on Importing/Using Classes



lonerhino
June 8th, 2007, 07:20 PM
I have a function that adds a dropshadow and blur to a MovieClip onstage. I tweaked it to AS3 and it works fine when the code is on the timeline.

However, If I create a simple class, copy the script and paste into the new class document , cannot get the thing to work. This should be so simple and instead it's so frustrating. I've looked all over the place and it just confirms I have some serious holes in my knowledge.

If I want to apply filters to certain objects onstage, do I import the class or set it as the document class for the fla?
If I put both fla and as files on my desktop just to get basics down - then packages and classpaths are not an issue - correct?
When the class is associated to the Fla file, does it automatically instantiate or must I use a constructor?In summary, lets say my class is called MyFilters.as and within that I have a setShadow function that includes both a blurFliter constructor and shadowfilter constructor. My setShadow function is called within the class file itself. How do I use MyFilters.as with a fla file? The target is set within the function inside the class.

mcOuterClip.mcInnerClip.filters = [shadowFilter, blurFilter];

Thanks for your patience in reading through this - clues appreciated!

DilutedImage
June 8th, 2007, 07:53 PM
If I recall correctly, the main timeline inherits many core display object classes, which would need to be imported if used in separate AS files. So, you might just need to import some of the display-related classes (perhaps flash.filters.* ?). .. I've only been working with AS3 for a couple days now though, so I may be completely wrong.


TheCanadian
June 8th, 2007, 10:53 PM
If I want to apply filters to certain objects onstage, do I import the class or set it as the document class for the fla?
If I put both fla and as files on my desktop just to get basics down - then packages and classpaths are not an issue - correct?
When the class is associated to the Fla file, does it automatically instantiate or must I use a constructor?

You would import the appropriate filter class into your class
Correct
If it is set as the document class then yes, it is instantiated when the SWF is run.

lonerhino
June 9th, 2007, 03:30 PM
Thanks guys for your replies.

Unfortunately, I'm still a bit stuck. I'm now of the decision that it does not make sense for my custom filter class to be set as the document class for the fla.


package {

//imports
import flash.filters.*;
import flash.display.*;

class LogoFilter{

public function LogoFilter(){

trace("Im the constructor");

setShadow();

}

//props

var shadowFilter:DropShadowFilter;
var blurFilter:BlurFilter;


private function setShadow():void{

trace("function Called");

var distance:Number = 4;
var angleInDegrees:Number = 45;
var color:Number = 0x000000;
var alpha:Number = 1;
var blurX:Number = 10;
var blurY:Number = 10;
var strength:Number = 2;
var quality:Number = 1;
var inner:Boolean = false;
var knockout:Boolean = false;
var hideObject:Boolean = false;

shadowFilter = new DropShadowFilter(distance, angleInDegrees, color, alpha, blurX, blurY, strength, quality, inner, knockout, hideObject);

var fblurX:Number = 2;
var fblurY:Number = 0;
var fquality:Number = 3;

blurFilter = new BlurFilter(fblurX, fblurY, fquality);

mcNav.mcLogo.filters = [shadowFilter, blurFilter];

}

}

}Now, in my Fla - frame 1 I've written

import LogoFilter;
var newFilter:LogoFilter = new LogoFilter();

and get the following error:

1120: Access of undefined property mcNav.

So thanks, I'm closer but not sure how I'm suppose to treat mcNav or where?

sasxa
June 9th, 2007, 05:09 PM
your LogoFilter class doesn't know what is mcNav - there's no definition in it, and you don't pass it as argument... easy thing to do here would be to add argument to your class


public function LogoFilter(objectThatImApllyingMyEffectTo:*) {
// you have to pass that argument to setShadow method as well
setShadow(objectThatImApllyingMyEffectTo);
//......
// and add that to setShadow declaration
private function setShadow(objectThatImApllyingMyEffectTo:*) {
//...
objectThatImApllyingMyEffectTo.filters = [shadowFilter, blurFilter];

and when you create it, you just pass object that you want to have those effects:


var newFilter:LogoFilter = new LogoFilter[mcNav.mcLogo];

the rest of the code would be pretty much the same..

p.s. you could actually remove few lines of code - from '//props' line to trace line.. and move those variable definitions outside of constructor...

lonerhino
June 11th, 2007, 06:44 PM
sasxa,

Thanks for your suggestion - your solution made perfect sense to me as the problem was with mcNav as a target. I passed mcNav as a argument but I'm not getting any further.

AS File


package{

//imports
import flash.filters.*;
import flash.display.*;

class LogoFilter{

public function LogoFilter(targetObject:MovieClip){

setShadow(targetObject);

}


//props
var shadowFilter:DropShadowFilter;
var blurFilter:BlurFilter;


//methods
private function setShadow(targetObject:MovieClip):void{

trace("function Called");

var distance:Number = 4;
var angleInDegrees:Number = 45;
var color:Number = 0x000000;
var alpha:Number = 1;
var blurX:Number = 10;
var blurY:Number = 10;
var strength:Number = 2;
var quality:Number = 1;
var inner:Boolean = false;
var knockout:Boolean = false;
var hideObject:Boolean = false;

shadowFilter = new DropShadowFilter(distance, angleInDegrees, color, alpha, blurX, blurY, strength, quality, inner, knockout, hideObject);

var fblurX:Number = 2;
var fblurY:Number = 0;
var fquality:Number = 3;

blurFilter = new BlurFilter(fblurX, fblurY, fquality);

targetObject.filters = [shadowFilter, blurFilter];

}

}

}


fla Code


import LogoFilter;
import flash.filters.*

//var myTargetInstance:MovieClip = mcNav.mcLogo;

var newFilter:LogoFilter = new LogoFilter(mcNav.mcLogo);And now here the Errors:

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


I tried the wildcard dataType for the arguments and I was a bit confused by the use of square brackets in a constructor but tried that as well - although that's not in the posted code - yet both approaches had same errors listed above.

Chaos_Blader
June 11th, 2007, 07:38 PM
If it is not part of a package, you dont have to import it.



import flash.filters.*

//var myTargetInstance:MovieClip = mcNav.mcLogo;

var newFilter:LogoFilter = new LogoFilter(mcNav.mcLogo);

jjcorreia
June 11th, 2007, 07:57 PM
1046: Type was not found or was not a compile-time constant: LogoFilter.
1180: Call to a possibly undefined method LogoFilter.


The problem is you didnt declare your class as public.

public class LogoFilter

lonerhino
June 12th, 2007, 10:43 AM
Doh,

How stupid a mistake was that - Thanks - not a public class makes a big difference. I appreciate all the helpful replies - working finally!