Why is the compiler complaining about a possibly undefined property - apache-flex

I have a very simple practice program for Flex 4 ( Gumbo ).
package
{
import mx.controls.ColorPicker;
import mx.controls.Label;
import mx.events.ColorPickerEvent;
import flash.display.Sprite;
public class testClass extends Sprite
{
private var cPicker:ColorPicker = new ColorPicker();
private var lbl:Label;
public function testClass()
{
cPicker.addEventListener(ColorPickerEvent.CHANGE,
colorPicker_change);
cPicker.move(10, 10);
addChild(cPicker);
lbl = new Label();
lbl.text = cPicker.hexValue;
lbl.move(10, 40);
addChild(lbl);
}
private function colorPicker_change(evt:ColorPickerEvent):void
{
lbl.text = cPicker.hexValue; // ff0000
}
}
}
But after building with 'mxmlc.exe testClass.as' on the command line, I get...
C:\src>mxmlc testClass.as Loading configuration file
C:\flex_sdk_4\frameworks\flex-config.xml
C:\src\testClass.as(21): col:
32 Error: Access of possibly undefined
pro perty hexValue through a reference
with static type
mx.controls:ColorPicker.
lbl.text = cPicker.hexValue;
^
C:\src\testClass.as(28): col:
32 Error: Access of possibly undefined
pro perty hexValue through a reference
with static type
mx.controls:ColorPicker.
lbl.text = cPicker.hexValue; // ff0000
^
Why does it think that cPicker is static? Or that cPicker.hexValue is undefined?
Also it seems that even after importing the ColorPicker library in the code using the import keyword, I somehow have to import it on the commandline for building as well. Is that correct?

According to my reading of the documentation hexValue isn't a property available on the ColorPicker. Do you mean selectedColor?

Related

AS3: AddChild issue -- "TypeError: Error #2007: Parameter child must be non-null."

My following code gave me
TypeError: Error #2007: Parameter child must be non-null runtime error.
not sure why...I would appreciate any help...
mySb = new ScrollBar();
mySb.x = cont.x; //+ cont.width;
mySb.y = cont.y;
mySb.height = contMask.height;
mySb.enabled = true;
addChild(mySb);
Updated
package com.search.view
{
import com.search.events.YouTubeSearchEvent;
import fl.controls.ScrollBar;
import fl.controls.Slider;
import fl.controls.UIScrollBar;
import fl.events.ScrollEvent;
import fl.events.SliderEvent;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.net.URLLoader;
public class SearchResultContainer extends Sprite
{
private var cont:videoCont;
private var contMask:Sprite;
private var mySb:ScrollBar;
public function SearchResultContainer()
{
super();
}
public function get selectedVideoID():String{
return newVideoID;
}
public function createContainer(_x:Number,_y:Number, videoResult:Array):void{
cont=new videoCont();
cont.x=_x;
cont.y=_y;
addChild(cont);
contMask = new Sprite();
contMask.x = cont.x;
contMask.y = cont.y;
createMask(contMask,0x000000,452,88);
addChild(contMask);
cont.mask = contMask;
mySb = new ScrollBar();
mySb.x = cont.x; //+ cont.width;
mySb.y = cont.y;
mySb.height = contMask.height;
mySb.enabled = true;
addChild(mySb); //problem code here...
}
private function createMask(inSrc:*,inColor:Number=0x999999,inW:Number=80,inH:Number=50):void{
var rect:Shape=new Shape();
rect.graphics.clear();
rect.graphics.beginFill(inColor);
rect.graphics.drawRect(0,0,inW,inH);
rect.graphics.endFill();
inSrc.addChild(rect);
}
}
}
I am in Flex environment....
Try to add a breakpoint before the problem occurs and check the mySb value , it looks like it's probably null , if it's not you'll have to look for null values either in the DisplayObjects you're using or the properties you're assigning them... if it is null , maybe you need to set more properties to your ScrollBar instance before adding it to the display list...
In my case I solved it adding the component to the movie library, but working in Flash CS5.5 environment.

Flex: type was not found or was not a compile-time constant: Button

I am trying to build a pure as3 project in flex and I got the following error:
type was not found or was not a compile-time constant: Button
type was not found or was not a compile-time constant: TextField
My code is:
import fl.controls.TextInput; // import my textinput
import flash.display.Shape;
import flash.display.SimpleButton;
import flash.display.Sprite;
import flash.text.TextField;
import fl.controls.Button; //import my button
public class SearchYoutube extends Sprite
{
private var textBx:TextField=new TextField();
private var controls:Controls;
private var bground:Sprite=new Sprite();
private var searchButton:Button;
/************************Constructor*********************/
public function SearchYoutube()
{
/*********************Create Search Form****************************/
createSearchForm("Please Enter Your Keyword");
}
/*********************Search Form****************************/
private function createSearchForm(title:String):void{
var searchInput:TextInput = new TextInput(); //error here
searchInput.width = 200;
searchInput.x=150;
searchInput.y=450;
searchKeyword=searchInput.text;
addChild(searchInput);
searchButton = new Button(); //error here
searchButton.x = 380;
searchButton.y = 450;
searchButton.label = "Search";
addChild(searchButton);
}
}
}
I appreciate any helps!
Agree with the above answer, and add that Button is in package mx.controls.
Use imports
import mx.controls.Button;
import mx.controls.TextInput;
Also to address other comments, doesn't "actionscript only" just mean no mxml and .mxml files? It's still a .as files only.
Haven't done any Flash coding for a while, but I seem to remember that all of the fl. Your Flex Builder can't see those two classes. Scrolling through the language reference there is no fl package, so I would say you need to change those two classes to something that exists in Flex like flash.display.Sprite (with buttonMode on) and flash.text.TextField
http://livedocs.adobe.com/flex/3/langref/

Can I dynamically embed fonts in Flex?

I'm wondering if I can dynamically embed fonts in Flex. I want to embed different fonts for different users so I don't want to embed all possible fonts in the same Flex file. If it's possible could you please post sample code.
You can do this in Actionscript. I've used this trick primarily to use opentype fonts that weren't supported with the compiler in the Flash IDE and to create font libraries that could be loaded lazily (only when needed), but you can also use this to selectively load fonts. If you had the mxmlc compiler on your server you could even generate the fontlib.as file and compile it on command.
// fontlib.as
// font library file
package {
import flash.display.Sprite;
public class fontlib extends Sprite {
[Embed(source = 'font/path/FontFile.otf', fontName = 'FontFile', unicodeRange = 'U+0020-U+007E,U+00AB,etc...')]
public static var FontFile:Class;
public static const FontFile_name:String = "FontFile"; // matches 'fontName' in embed
public function fontlib() {
}
}
}
This can be compiled like so:
mxmlc fontlib.as
You can use in your application like this:
// Main.as
// document class
package {
import flash.text.Font;
import flash.display.Loader;
import flash.events.Event;
import flash.system.ApplicationDomain;
import flash.text.StyleSheet;
public var fontsLoader:Loader;
public var fontFile:String = "";
public var ss:StyleSheet;
public function Main() {
fontsLoader = new Loader();
fontsLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, _onFontsLoadComplete);
}
private function _onFontsLoadComplete(e:Event):void {
var fontlib:Class = e.target.applicationDomain.getDefinition('fontlib');
Font.registerFont(fontlib.FontFile); // registers font
fontFile = fontlib.FontFile_name; // name the font was loaded as
// actually using the font looks like this:
ss = new StyleSheet();
ss.parseCSS("div { fontFamily: " + fontFile + "; }");
}
}

Flex Modules and custom text field

This is a really strange one. I've created my own CustomTextField class which I am using to embed the font and set the defaultTextFormat. This is working absolutely fine, but for some reason when I try to create a new CustomTextField in any module but the parent application, text text is only showing sometimes.
Here is my CustomTextField class:
package uk.package.text
{
import flash.text.AntiAliasType;
import flash.text.Font;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
public class CustomTextField extends TextField
{
[Embed(source='../assets/fonts/Arial.ttf',fontName='CustomFont',fontWeight='regular',
unicodeRange='U+0020-U+0040,U+0041-U+005A,U+005B-U+0060,U+0061-U+007A,U+007B-U+007E,U+0080-U+00FF,U+0100-U+017F,U+0400-U+04FF,U+0370-U+03FF,U+1E00-U+1EFF',
mimeType='application/x-font-truetype'
)]
public static var MY_FONT:Class;
[Embed(source='../assets/fonts/Arial Bold.ttf',fontName='CustomFont',fontWeight='bold',
unicodeRange='U+0020-U+0040,U+0041-U+005A,U+005B-U+0060,U+0061-U+007A,U+007B-U+007E,U+0080-U+00FF,U+0100-U+017F,U+0400-U+04FF,U+0370-U+03FF,U+1E00-U+1EFF',
mimeType='application/x-font-truetype'
)]
public static var MY_FONT_BOLD:Class;
public static const DEFAULT_FONT:String = "Arial";
public static const DEFAULT_TEXT_COLOUR:int = 0x000000;
public static const DEFAULT_TEXT_SIZE:int = 14;
private var _tf:TextFormat = new TextFormat(DEFAULT_FONT, DEFAULT_TEXT_SIZE, DEFAULT_TEXT_COLOUR);
public function CustomTextField():void
{
var CustomFont:Font = new MY_FONT();
_tf.font = CustomFont.fontName;
_tf.size = 16;
embedFonts = true;
antiAliasType = AntiAliasType.ADVANCED;
defaultTextFormat = _tf;
autoSize = TextFieldAutoSize.LEFT;
}
public override function set htmlText(value:String):void
{
super.htmlText = value;
setTextFormat(_tf);
}
public function get textFormat():TextFormat
{
return _tf;
}
}
}
It's weird how sometimes it works and sometimes it doesn't... perhaps there is something odd going on with the modules?
Ok, this one took me ages to figure out. I finally got it working by using the following code:
preinitialize="moduleLoader.moduleFactory=Application.application.systemManager;"
In the module loader item.
Thanks!
yes it's almost definitely a module issue. I've seen something similar before. I'm searching out the answer but my initial thought is to set
moduleLoader.applicationDomain = ApplicationDomain.currentDomain
Another issue is if you're loading the same module twice. If so you need to make the url unique by adding file.swf?<randomNumber> or something similar.

Adobe Flash Builder (flex4): addChild() is not available in this class.

I want to load an swf into a flex 4 application in order to use its classes.
var ldr:Loader=new Loader();
ldr.load(new URLRequest("file://path/to/fileswf"));
ldr.contentLoaderInfo.addEventListener(Event.INIT, loaded);
function loaded(evt:Event):void { addChild(ldr); }
I receive the error:
Error: addChild() is not available in this class. Instead, use addElement() or modify the skin, if you have one.
at spark.components.supportClasses::SkinnableComponent/addChild()[E:\dev\gumbo_beta2\frameworks\projects\spark\src\spark\components\supportClasses\SkinnableComponent.as:966]
at main/private:init/loaded()[C:\Documents and Settings\ufk\Adobe Flash Builder Beta 2\xpogames-toolkit-test\src\main.mxml:22]
If I change addChild() to addElement(), I receive the following compilation error:
1067: Implicit coercion of a value of type flash.display:Loader to an unrelated type mx.core:IVisualElement. main.mxml path/dir line 22 Flex Problem
Any ideas how to resolve this issue?
Create another container to place the displayObject in:
// container ( IVisualElement ) for DisplayObjects
var container:UIComponent = new UIComponent();
addElement( container );
// displayObject goes to container
var displayO:Sprite = new Sprite();
container.addChild( displayO );
well in flash builder 4 full version, there isn't any this.rawChildren.
The best approach to resolve the issue would be to convert each required class to a flex component and to use it on your flex application:
download and install flex component kit
http://www.adobe.com/cfusion/entitlement/index.cfm?e=flex_skins
create a movie clip
convert to flex component
add the relevant functions to this class
a skeleton for a class that is attached to a movieclip that is about to be converted to a flex component:
package {
import mx.flash.UIMovieClip;
import flash.text.TextField;
import flash.events.Event;
import flash.events.MouseEvent;
public dynamic class challenge_screen extends UIMovieClip {
public function challenge_screen() {
super();
}
}
}
this.rawChildren.addChild( ldr )
should work
private var _loader:SWFLoader = new SWFLoader();
private var _uicomponent:UIComponent = new UIComponent();
private function swfLoaded(event:Event):void
{
Alert.show("inside swf Loaded");
var content:DisplayObject =_loader.content;
_uicomponent.addChild(content);
}
public function loadSWF () : void
{
_loader.addEventListener(Event.INIT, swfLoaded);
_loader.load("http://intelliveysoft.com:5080/myelearn/Admin.swf");
addElement(_uicomponent);
}
Try this. It will work

Resources