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
Related
Is there any workaround to create submenu in a flex context menu other than stopping right click from javascript.
Regards,
Hi Frank,
Yes, I want to create submenus in a context menu. Can you help me here.
Regards,
Hi Frank,
I need the context menu for the application not for datagrid.
In my initial question the phrase "other than stopping right click from javascript" means
"catch the right click in html, call a javascript function and over js call a as function."
The project that you have specified does the above procedure. I don't want to use this
procedure. Is there any other way for achieving submenus in a flex context menu. Could you
please tell me if so..
Regards,
Arvind
Yes, there is.
I don't know, what you exactly mean with this:
other than stopping right click from
javascript.
But, if you want to create a entry in submenu, do this:
//Instance of my own class
private var myContext:myContextMenu = new myContextMenu();
application.contextMenu = myContext.myContextMenu;
//Here is the Class:
package com.my.components
{
/* ////////////////////////////////////////////
///// My Context MenĂ¼ /////////////////////
///////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
//to use: //
// private var myContext:MyContextMenu = new MyContextMenu(); //
// init() in creationComplete //
// application.contextMenu = myContext.myContextMenu; //
////////////////////////////////////////////////////////////////////////////// */
import flash.display.Sprite;
import flash.events.ContextMenuEvent;
import flash.net.URLRequest;
import flash.net.navigateToURL;
import flash.text.TextField;
import flash.ui.ContextMenu;
import flash.ui.ContextMenuBuiltInItems;
import flash.ui.ContextMenuItem;
public class MyContextMenu extends Sprite
{
public var myContextMenu:ContextMenu;
private var menuLabel:String = String.fromCharCode(169)+" My Company GmbH";
public function MyContextMenu()
{
myContextMenu = new ContextMenu;
removeDefaultItems();
addCustomItems();
myContextMenu.addEventListener(ContextMenuEvent.MENU_SELECT, menuSelectHandler);
super();
}
private function removeDefaultItems():void
{
myContextMenu.hideBuiltInItems();
var defaultItems:ContextMenuBuiltInItems = myContextMenu.builtInItems;
defaultItems.print = true;
}
private function addCustomItems():void
{
var item:ContextMenuItem = new ContextMenuItem(menuLabel);
myContextMenu.customItems.push(item);
item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,menuItemSelectHandler);
}
private function menuSelectHandler(event:ContextMenuEvent):void
{
}
private function menuItemSelectHandler(event:ContextMenuEvent):void
{
navigateToURL(new URLRequest('http://www.my-company.de'));
}
private function createLabel():TextField
{
var txtField:TextField = new TextField();
//txtField.text = textLabel;
txtField.text = "RightClickHere";
return txtField;
}
}
}
Have fun
EDIT:
There is an interesting project here. They catch the right click in html, call a javascript function and over js call a as function.
Unfortunately, the limitation of FP or NativeMenu APi allowed just on level contextmenu. Read here
Frank
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/
I was wondering if anyone had any luck with the following senario in flex.
I'd like to be able to have a custom item renderer which delegates to another renderer inside.
The reason for this would be in a datagrid for instance displaying a checkbox if the dataprovider for the row had a boolean value. Using the default item renderer when the value was a non boolean.
Basically I was hoping to use a proxy object (though not necessarily the proxy class) so that I could a renderer which delegated all of its responsibilties to a sub renderer.
Hard to explain.
Edit 1
I think the following gives a clearer idea of what I had in mind. This is only knocked up quickly for the purpose of showing the idea.
SwitchingRenderer.as
package com.example
{
import mx.controls.CheckBox;
import mx.controls.dataGridClasses.DataGridItemRenderer;
import mx.controls.listClasses.BaseListData;
import mx.controls.listClasses.IDropInListItemRenderer;
import mx.core.IDataRenderer;
import mx.core.UIComponent;
public class SwitchingRenderer extends UIComponent implements IDataRenderer, IDropInListItemRenderer
{
private var checkboxRenderer:CheckBox;
private var defaultRenderer:DataGridItemRenderer;
private var currentRenderer:IDataRenderer;
public function SwitchingRenderer()
{
this.checkboxRenderer = new CheckBox();
this.defaultRenderer = new DataGridItemRenderer();
this.currentRenderer = defaultRenderer();
super();
}
public function get data():Object
{
//If the data for this cell is a boolean
// currentRender = checkBoxRenderer
// otherwise
// currentRenderer = defaultRenderer
}
public function set data(value:Object):void
{
currentRenderer.data = value;
}
public function get listData():BaseListData
{
return currentRenderer.listData;
}
public function set listData(value:BaseListData):void
{
currentRenderer.listData = value;
}
}
}
If you're using Flex 4 spark components look into the itemRendererFunction,
Here is a good sample from the interwebs.
Unfortunately, Flex 3 components, such as the DataGrid do not support that.
You're a bit vague on what you'd be displaying if the data sent into the itemRenderer was not a Boolean value. But, you can easily modify the visual appearance of a component based on the data change event, including swapping visible properties of a component's children, changing states or change the selectedIndex of a ViewStack. All these things can be done within an itemRenderer w/o issues.
Edit:
Based on the user's additional posting, I'd add that what he is after can be done like this:
public function get data():Object
{
if(this.data is Boolean){
checkBoxRenderer.visible = true;
defaultRenderer.visible = false;
} else {
checkBoxRenderer.visible = false;
defaultRenderer.visible = true;
}
}
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?
I have an arrayCollection of objects that extend Sprite, and have bitmaps within them.
I want to display these in a list (or some other component that would allow a user to scroll through them, and see their associated data.)
When I do: myList.dataProvider = myArrayCollection
the list just shows a bunch of lines of [Object, Item] instead of the visual sprites.
Here is a simplified version of my Object:
public class myUIC extends UIComponent
{
public var mySprite:Sprite = new Sprite;
[Embed(source="assets/BGimage.png")]
public var BGimage:Class;
public var myBitmap:Bitmap;
public var wordText:TextField = new TextField;
public function myUIC(myWord:String)
{
this.wordText.text = myWord;
this.myBitmap = new BGimage;
this.mySprite.addChild(this.myBitmap);
this.mySprite.addChild(this.wordText);
this.addChild(this.mySprite);
}
}
Tried many different ways to get it to show up in a List, but can't do it.
See this tutorial: Flex Examples - displaying icons in a flex list control
Sounds like you may want to try writing a simple item renderer (perhaps based off UIComponent) that adds the associated sprite the display list of the render using addChild().
try rawChildren.addChild for adding the Sprite
Here, try using an itemRenderer something like this. It ought to work with any generic DisplayObject. It's grabbing the width and height from the assigned data property, so you might need to set variableRowHeight to true in your actual list for it to work as expected.
package
{
import flash.display.DisplayObject;
import mx.controls.listClasses.IListItemRenderer;
import mx.core.UIComponent;
import mx.events.FlexEvent;
/*
Extending UIComponent means we can add Sprites (or any DisplayObject)
with addChild() directly, instead of going through the rawChildren property.
Plus, in this case, we don't need the extra overhead of Canvas's layout code.
IListItemRenderer lets us use it as a List's itemRenderer. UIComponent already
implements all of IListItemRenderer except for the data property
*/
public class SpriteRenderer extends UIComponent implements IListItemRenderer
{
// Implementing the data property for IListItemRenderer is really easy,
// you can find example code in the LiveDocs for IDataRenderer
private var _data:Object;
[Bindable("dataChange")]
public function get data():Object
{
return _data;
}
public function set data(value:Object):void
{
if (value !== _data) {
// We need to make sure to remove any previous data object from the child list
// since itemRenderers are recycled
if (_data is DisplayObject && contains(_data as DisplayObject)) {
removeChild(_data as DisplayObject);
}
_data = value;
// Now we just make sure that the new data object is something we can add
// and add it
if (_data is DisplayObject) {
this.width = (_data as DisplayObject).width;
this.height = (_data as DisplayObject).height;
addChild(_data as DisplayObject);
}
dispatchEvent(new FlexEvent(FlexEvent.DATA_CHANGE));
}
}
public function SpriteRenderer()
{
super();
}
}
}