I want to implement drag&drop possibilities in my application. Here is my source code:
I added images to the container, and now want to be able to move them from element PieceContainer to element board (defined in the another class). I tried to define mouse move handler as it was shown here: http://livedocs.adobe.com/flex/3/html/help.html?content=dragdrop_7.html, but it doesn't do anything... Actually I don't understand how should I define drap initiator, and drop handler
public class PieceContainer extends Canvas
{
//private var image:Image = new Image();
private var piecesWhite:Dictionary;
private var piecesBlack:Dictionary;
private var images:ArrayCollection = new ArrayCollection();
private var placeX:Number;
public var items:Dictionary;
public function PieceContainer()
{
super();
this.addEventListener(Event.ADDED_TO_STAGE, displayPiece);
}
private function displayPiece(event:Event):void
{
placePiecesDict(new Point(0, 0), items);
}
private function placePiecesDict(start:Point, dict:Dictionary):void
{
placeX = 0;
for (var item:* in dict)
{
var image:Image = new Image();
image.source = dict[item];
image.x = placeX/1.4;
image.y = start.y;
image.addEventListener(MouseEvent.MOUSE_MOVE, doDrag);
images.addItem(image);
this.addChild(image);
placeX += height;
}
}
private function doDrag(evt:Event):void
{
trace(evt);
}
}
Please also see attached image for some more description:
http://img.skitch.com/20091221-e6j8d62y9iin621hmcdbssrp6d.png
I reorganized code this way:
public class PieceContainer extends Canvas
{
private var piecesWhite:Dictionary;
private var piecesBlack:Dictionary;
private var images:ArrayCollection = new ArrayCollection();
private var placeX:Number;
public var items:Dictionary;
public function PieceContainer()
{
super();
this.addEventListener(Event.ADDED_TO_STAGE, displayPiece);
}
private function displayPiece(event:Event):void
{
placePiecesDict(new Point(0, 0), items);
}
private function placePiecesDict(start:Point, dict:Dictionary):void
{
placeX = 0;
for (var item:* in dict)
{
var image:Image = new Image();
image.source = dict[item];
image.x = placeX/1.4;
image.y = start.y;
image.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
images.addItem(image);
this.addChild(image);
placeX += height;
}
}
private function mouseMoveHandler(evt:MouseEvent):void
{
var dragInitiator:Image = Image(evt.currentTarget);
var ds:DragSource = new DragSource();
ds.addData(dragInitiator, "img");
DragManager.doDrag(dragInitiator, ds, evt);
}
And also added drag and drop handlers to the board:
public class BoardCell extends Canvas
{
private var _color:Number;
public function BoardCell()
{
super();
this.addEventListener(DragEvent.DRAG_ENTER, dragEnterHanler);
this.addEventListener(DragEvent.DRAG_DROP, dragDropHandler);
}
public function placeMe(x:int, y:int, width:Number, color:Number):void
{
graphics.lineStyle(1, 0x000000);
graphics.beginFill(color);
graphics.drawRect(x, y, width, width);
graphics.endFill();
}
private function dragEnterHanler(event:DragEvent):void
{
if(event.dragSource.hasFormat("img"))
{
DragManager.acceptDragDrop(Canvas(event.currentTarget))
}
}
private function dragDropHandler(event:DragEvent):void
{
event.dragInitiator.x = 0;
event.dragInitiator.y = 0;
}
But actually have 2 problems:
First: Items are draggable, but when i add them to board they stays on in the PiecesContainer.
Second: I want items to be copied, rather then just moved (but when they are on board they should be movable, not copied)
Thanks, in advance
First problem is easily fixed. dragMoveEnabled=true is how you do it in a list control, but since your doing something very custom you will want to look back at the drag initiator and delete the drug item from it. Details here:
http://livedocs.adobe.com/flex/3/html/help.html?content=dragdrop_8.html
This will also help you set up your drag and drop functions so you can solve problem 2 as well.
Related
I am getting null pointer exception when I try spawning an item(Mushroom) when the player collides with a particular coin(As in MARIO game). I have been following this tutorial but I want the inputs to be buttons and not the keys. I have created 4 buttons and added to the stage . The inputs are working perfectly . But I am getting exception when,
I simply add the buttons to the stage and the input is still using keys
I am not getting exception when,
I remove all the buttons from the code and the input is using keys.
Here is my code for adding buttons to the stage ,
public class PlayScreen implements Screen, InputProcessor
{
.......
private Array<Item> items;
private LinkedBlockingQueue<ItemDef> itemsToSpawn;
private TextureAtlas atlas = new TextureAtlas("move_sprites.pack"); //contains the mushroom as well as the player packed together
public PlayScreen(MyGame game) {
.............
stage = new Stage();
atlas = new TextureAtlas("ui/button_pack.pack");
skin = new Skin(atlas);
white = new BitmapFont(Gdx.files.internal("fonts/white.fnt"), false);
black = new BitmapFont(Gdx.files.internal("fonts/black.fnt"), false);
TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle();
textButtonStyle.up = skin.getDrawable("buttonin");
textButtonStyle.down = skin.getDrawable("buttonout");
textButtonStyle.pressedOffsetX = 1;
textButtonStyle.font = black;
buttonright = new TextButton("Right", textButtonStyle);
buttonleft = new TextButton("Left", textButtonStyle);
//table.setBounds(500, 5, 250, 150);
buttonleft.setPosition(350, 5);
buttonleft.setHeight(100);
buttonleft.setWidth(100);
buttonright.setPosition(100, 5);
buttonright.setHeight(100);
buttonright.setWidth(100);
buttonup = new TextButton("up", textButtonStyle);
buttonup.setPosition(600, 5);
buttonup.setHeight(100);
buttonup.setWidth(100);
stage.addActor(buttonup);
stage.addActor(buttonright);
stage.addActor(buttonleft);
Gdx.input.setInputProcessor(stage);
......
}
//create a new method
public void spawnItem(ItemDef idef)
{
itemsToSpawn.add(idef);
}
public void handleSpawingItems()
{
if (!itemsToSpawn.isEmpty()) {
ItemDef idef = itemsToSpawn.poll(); //poll is pop
if (idef.type == Mushroom.class) {
items.add(new Mushroom(this, idef.position.x, idef.position.y));
}
}
}
public void update(float dt) {
handleInput(dt);
handleSpawingItems();
........
}
//Giving inputs using buttons
public void handleInput(float dt)
{
if (gamehero.currentState != Hero.State.DEAD) {
buttonup.addListener(new InputListener() {
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
gamehero.heroBody.setLinearVelocity(0, 4f);
}
return true;
}
}
);
}
}
public void render(float delta)
{
......
update(delta);
stage.act();
stage.draw();
}
}
}
Mushroom Class..
public class Mushroom extends Item {
public Mushroom(PlayScreen screen, float x, float y) {
super(screen, x, y);
//mush is one of the sprite packed together using texture packer. Exception is thrown in the below line
setRegion(screen.getAtlas().findRegion("mush"), 0, 0, 16, 16);
velocity = new Vector2(0.7f, 0); //velocity of mushroom
}
I am triggering this mushroom when mario head collides with coin like below,
public class Coins extends InteractiveTileObject {
....
public void onHeadHit(Hero hero) {
if (object.getProperties().containsKey("mushroom")) {
Gdx.app.log("coins", "collision");
//Exception occurs in the below line too. When the collision occurs, spawnItem method is called in the main class
screen.spawnItem(new ItemDef(new Vector2(body.getPosition().x, body.getPosition().y + 16 / MyGame.PPM), Mushroom.class));
}
ItemDef Class,
public class ItemDef {
public Vector2 position;
public Class<?> type;
public ItemDef(Vector2 position, Class<?> type)
{
this.position = position;
this.type = type;
}
}
Exception occurs if I just add a button to my code and gave the input using keys. Problem lies in spawning the mushroom if I just add a button to my code. Its weird. Please help. I am a beginner . Thanks in advance
I found the cause for this Exception.
I have given the same name for 2 TextureAtlas while declaring,
private TextureAtlas atlas = new TextureAtlas("move_sprites.pack");
and the other,
atlas = new TextureAtlas("ui/button_pack.pack");
But I am referring the first TextureAtlas in other class. Since I have declared the TextureAtlas only once but used for two different packs, It threw null pointer exception as there was name conflict. Thanks for all your answers.
Everything works fine until I create a new tab. Then when I go to the previous and try to use any of the buttons they affect the latest tab not the one I have selected. But if I go to the latest tab it works like normal. Here is the class that I use to make my tabs. So, why is the previous tabs affecting the lastest? And how do I fix it?
public class JTab {
private javafx.scene.control.Tab tab;
private ImageView imgView;
private Image logo;
private BorderPane root;
private Button reloadButton, backButton, forwardButton;
private TextField field;
private WebView view;
private WebEngine engine;
private static JTab instance;
private JBrowser jBrowser;
private JTab(JBrowser jBrowser) {
this.jBrowser = jBrowser;
}
public static JTab getInstance(JBrowser browser) {
if(instance == null)
instance = new JTab(browser);
return instance;
}
public javafx.scene.control.Tab addTab() {
tab = new Tab();
tab.setText("New Tab");
tab.setOnClosed(event2 -> {
if(jBrowser.getTabPane().getTabs().size() == 1) {
jBrowser.getTabPane().setTabClosingPolicy(TabPane.TabClosingPolicy.UNAVAILABLE);
}
});
logo = new Image("unknown-document.png");
imgView = new ImageView(logo);
tab.setGraphic(imgView);
HBox hBox = new HBox(5);
hBox.setAlignment(Pos.CENTER);
reloadButton = new Button("Reload");
backButton = new Button("<");
forwardButton = new Button(">");
reloadButton.setOnAction(event1 -> engine.reload());
backButton.setOnAction(event1 -> loadData(goBack()));
forwardButton.setOnAction(event1 -> loadData(goForward()));
//The TextField for entering web addresses.
field = new TextField("Enter URL");
field.setPrefColumnCount(50); //make the field at least 50 columns wide.
field.focusedProperty().addListener((ObservableValue<? extends Boolean> ov, Boolean t, Boolean t1) -> { //When click on field entire thing selected
Platform.runLater(() -> {
if (field.isFocused() && !field.getText().isEmpty()) {
field.selectAll();
}
});
});
field.setOnKeyPressed(event -> { //When ENTER is pressed it will load page
if (event.getCode() == KeyCode.ENTER) {
if (!field.getText().isEmpty()) {
loadData(field.getText());
}
}
});
//Add all out navigation nodes to the vbox.
hBox.getChildren().addAll(backButton, forwardButton, reloadButton, field);
view = new WebView();
engine = view.getEngine();
engine.setJavaScriptEnabled(true);
engine.getLoadWorker().stateProperty().addListener(
(ov, oldState, newState) -> {
if (newState == Worker.State.SUCCEEDED) {
tab.setText(getTitle());
//TODO setGraphic
}
});
loadData("google.com");
root = new BorderPane();
root.setPrefSize(1024, 768);
root.setTop(hBox);
root.setCenter(view);
tab.setContent(root);
return tab;
}
public void loadData(String URL) {
if(!URL.startsWith("http://")) {
URL = "http://" + URL;
}
field.setText(URL);
tab.setText(URL);
engine.load(URL);
}
private String getTitle() {
Document doc = engine.getDocument();
NodeList heads = doc.getElementsByTagName("head");
String titleText = engine.getLocation() ; // use location if page does not define a title
if (heads.getLength() > 0) {
Element head = (Element)heads.item(0);
NodeList titles = head.getElementsByTagName("title");
if (titles.getLength() > 0) {
Node title = titles.item(0);
titleText = title.getTextContent();
}
}
return titleText;
}
private String goBack() {
final WebHistory history = engine.getHistory();
ObservableList<WebHistory.Entry> entryList = history.getEntries();
int currentIndex=history.getCurrentIndex();
Platform.runLater(() -> history.go(-1));
return entryList.get(currentIndex>0?currentIndex-1:currentIndex).getUrl();
}
private String goForward() {
final WebHistory history = engine.getHistory();
ObservableList<WebHistory.Entry> entryList=history.getEntries();
int currentIndex=history.getCurrentIndex();
Platform.runLater(() -> history.go(1));
return entryList.get(currentIndex<entryList.size()-1?currentIndex+1:currentIndex).getUrl();
}
}
Remove getInstance(jBrowser) method
Make the constructor public.
Then to add a tab to a tabPane do
tabPane.getTabs().add(new JTab(jBrowser).addTab());
I have been digging for custom Flex preloaders and they all seem to rely on the same template:An SWC is created with Flash CS5 and then used by Flash Builder using the "preloader" application property.
I don't own Flash CS, and it feels that Flash builder should be able to do the trick.
I created a Library Project in Flash Builder with the following bare bones code:
package loader
{
import flash.display.DisplayObject;
import flash.events.Event;
import flash.utils.getTimer;
import mx.events.RSLEvent;
import mx.preloaders.DownloadProgressBar;
import mx.preloaders.SparkDownloadProgressBar;
public class Preloader extends SparkDownloadProgressBar
{
[Embed(source="loaderlogo.png")] public var logoClass:Class;
private var _displayStartCount:uint = 0;
private var _initProgressCount:uint = 0;
private var _downloadComplete:Boolean = false;
private var _showingDisplay:Boolean = false;
private var _startTime:int;
// private var preloaderDisplay:PreloaderDisplay;
private var rslBaseText:String = "loading: ";
public function Preloader()
{
super();
}
/**
* Event listener for the <code>FlexEvent.INIT_COMPLETE</code> event.
* NOTE: This event can be commented out to stop preloader from completing during testing
*/
override protected function initCompleteHandler(event:Event):void
{
dispatchEvent(new Event(Event.COMPLETE));
}
/**
* Creates the subcomponents of the display.
*/
override protected function createChildren():void
{
var img:DisplayObject = new logoClass();
img.x = Math.round( ( stageWidth - img.width) / 2);
img.y = Math.round( ( stageHeight - img.height) / 2);
addChild( img);
var dpb:DownloadProgressBar = new DownloadProgressBar();
dpb.x = img.x + 100;
dpb.y = img.x + 100;
dpb.width = 170;
dpb.height = 20;
addChild( dpb);
}
/**
* Event listener for the <code>RSLEvent.RSL_PROGRESS</code> event.
**/
override protected function rslProgressHandler(evt:RSLEvent):void {
if (evt.rslIndex && evt.rslTotal) {
//create text to track the RSLs being loaded
rslBaseText = "loading RSL " + evt.rslIndex + " of " + evt.rslTotal + ": ";
}
}
/**
* indicate download progress.
*/
override protected function setDownloadProgress(completed:Number, total:Number):void {
}
/**
* Updates the inner portion of the download progress bar to
* indicate initialization progress.
*/
override protected function setInitProgress(completed:Number, total:Number):void {
}
/**
* Event listener for the <code>FlexEvent.INIT_PROGRESS</code> event.
* This implementation updates the progress bar
* each time the event is dispatched.
*/
override protected function initProgressHandler(event:Event):void {
var elapsedTime:int = getTimer() - _startTime;
_initProgressCount++;
if (!_showingDisplay && showDisplayForInit(elapsedTime, _initProgressCount)) {
_displayStartCount = _initProgressCount;
show();
// If we are showing the progress for the first time here, we need to call setDownloadProgress() once to set the progress bar background.
setDownloadProgress(100, 100);
}
if (_showingDisplay) {
// if show() did not actually show because of SWFObject bug then we may need to set the download bar background here
if (!_downloadComplete) {
setDownloadProgress(100, 100);
}
setInitProgress(_initProgressCount, initProgressTotal);
}
}
private function show():void
{
// swfobject reports 0 sometimes at startup
// if we get zero, wait and try on next attempt
if (stageWidth == 0 && stageHeight == 0)
{
try
{
stageWidth = stage.stageWidth;
stageHeight = stage.stageHeight
}
catch (e:Error)
{
stageWidth = loaderInfo.width;
stageHeight = loaderInfo.height;
}
if (stageWidth == 0 && stageHeight == 0)
return;
}
_showingDisplay = true;
createChildren();
}
}
}
For short, it's loading a logo and a progress bar
It displays a preloader, but really late in the loading process. As if it was being loaded after Flex.
Do I need to compile this in CS5 to completely avoid use of MX/Spark?
You shouldn't use any components in preloader. Try to remove you imports (Ctrl+Shift+O):
import mx.controls.Image;
import spark.components.Label;
Use TextField and Loader instead if needed. I'm not sure about DownloadProgressBar component.
Also don't use create children in preloader. Here is one working sample:
package {
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import mx.core.mx_internal;
import mx.preloaders.SparkDownloadProgressBar;
use namespace mx_internal;
public class Preloader extends SparkDownloadProgressBar {
private var preloaderLogo : MovieClip;
private var loadingText : TextField;
private var loadingProgress : TextField;
private var _initProgressCount : uint = 0;
private var textFormat : TextFormat = new TextFormat("Verdana", 16, 0x666666, true);
public function Preloader() {
super();
textFormat.align = "center";
}
override public function set preloader(value : Sprite) : void {
super.preloader = value;
if (!preloaderLogo) {
preloaderLogo = new Assets.PRELOADER_LOGO; // kakaranet logo
var startX : Number = Math.round((stageWidth - preloaderLogo.width) / 2);
var startY : Number = Math.round(stageHeight / 2 - preloaderLogo.height) - 100;
preloaderLogo.x = startX;
preloaderLogo.y = startY;
loadingText = new TextField();
loadingProgress = new TextField();
loadingText.width = stageWidth;//to allow center align
loadingProgress.width = stageWidth;
loadingText.text = "Loading...";
loadingText.y = preloaderLogo.y + preloaderLogo.height + 20;
loadingProgress.text = "0%";
loadingProgress.y = loadingText.y + loadingText.textHeight + 10;
addChild(preloaderLogo);
addChild(loadingText);
addChild(loadingProgress);
loadingText.setTextFormat(textFormat);
loadingProgress.setTextFormat(textFormat);
}
}
override protected function progressHandler(event : ProgressEvent) : void {
super.progressHandler(event);
if (loadingProgress) {
loadingProgress.text = Math.floor(event.bytesLoaded / event.bytesTotal * 100) + "%";
loadingProgress.setTextFormat(textFormat);
}
}
override protected function completeHandler(event : Event) : void {
loadingText.text = "Ready!";
loadingText.setTextFormat(textFormat);
preloaderLogo.stop();
}
override protected function initProgressHandler(event : Event) : void {
super.initProgressHandler(event);
//similar to super
_initProgressCount++;
if (loadingProgress) {
loadingProgress.text = "100% / " + Math.floor(_initProgressCount / initProgressTotal * 100) + "%";
loadingProgress.setTextFormat(textFormat);
}
}
}
}
I have created a tree with a custom treeitemrenderer where I have added a textinput box next to each label. My data provider is an ArrayCollection.
When I enter value in the input box, I can see the value fine as I expand or collapse the tree using the disclousure arrow. There is a strange issue when I try to expand and collapse tree from the mxml page with a button click to expand and collapse tree using the following code.
The problem is as I expand or collapse tree, the values I entered in the input boxes appear to be moving.
For example, I have entered 15 in input box 1. When I expand and collapse tree, 15 moves to another input box, and moves to another, and then it moves back to where it was entered as I keep expanding and collapsing the tree. I am still learning Flex. I am not sure what is happening, it is something to do with arraycollection.
Any help would be greatly appreciated. Thanks.
private function expandTree():void{
for (var i:int = 0; i < thisTree.dataProvider.length; i ++){
thisTree.expandChildrenOf(thisTree.dataProvider[i], true)
}
}
private function collapseTree():void{
for (var i:int = 0; i < thisTree.dataProvider.length; i ++){
thisTree.expandChildrenOf(thisTree.dataProvider[i], false)
}
}
Flex item renderers are recycled, which is what is causing your data to walk like this. In your item renderer, I generally find it is useful to cast your data to a bindable value object and override the setter for data.
[Bindable]
private var myBindableValueObject:MyBindableValueObject;
override public function set data(v:Object):void
{
if(v == data)
return;
myBindableValueObject = v as MyBindableValueObject;
super.data = value;
validateNow();
}
You will need to bind the properties of the text input and labels to values in the value object. This will make sure the proper values are displayed at the appropriate location. Using this approach should eliminate the oddities that you are experiencing.
I tried as you suggested but I don't see any changes. Not sure I was able to follow your suggestion. Here is the code. Thanks.
package
{
import mx.collections.*;
import mx.controls.Label;
import mx.controls.TextInput;
import mx.controls.listClasses.*;
import mx.controls.treeClasses.*;
[Bindable]
public class TestSetupTreeItemRenderer extends TreeItemRenderer {
[Bindable]
public var _numQuestion:TextInput;
private var _numOfQuestionText:Label;
private var _listData:TreeListData;
[Bindable]
public var tItem:TestSetupTreeItem;
public function TestSetupTreeItemRenderer():void {
super();
mouseEnabled = false;
}
override protected function createChildren():void {
super.createChildren();
_numQuestion = new TextInput();
_numQuestion.text = "0"; //default
_numQuestion.width = 45;
_numQuestion.height = 20;
_numQuestion.restrict = "0123456789";
addChild(_numQuestion);
_numOfQuestionText = new Label();
_numOfQuestionText.width = 60;
_numOfQuestionText.height = 22;
addChild(_numOfQuestionText);
}
override public function set data(value:Object):void {
if(value == data)
return;
tItem= value as TestSetupTreeItem;
super.data = value;
validateNow();
}
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
super.updateDisplayList(unscaledWidth,unscaledHeight);
if(super.data){
tItem = super.data as TestSetupTreeItem;
this.label.width = 150;
this.label.truncateToFit(null);
this.label.multiline = true;
this.label.wordWrap = true;
var tld:TreeListData = TreeListData(super.listData);
this._numOfQuestionText.text = "of " + tItem.totalQuestionCount;
this._numOfQuestionText.x = 300;
this._numOfQuestionText.y = super.label.y;
this._numQuestion.x = 200;
this._numQuestion.y = super.label.y;
this._numQuestion.editable = true;
tItem.desiredQuestionCount = this._numQuestion.text;
}
}
}
}
Has anyone successfully implemented a custom preloader in Flex 4? In my experience, when I specify a custom preloader using the preloader="com.foo.MyPreloader" in the Application tag, the preloader does not display until the SWF is completely downloaded, defeating the purpose of the preloader! Perhaps this is a bug in the still-beta Flex 4 framework?
I've been using this example in many Flex3 projects. It's still working with Flex4 sdk:
I can remember where I get it from. And you are right when you say it's important that this script is NOT referencing anything...
<s:Application tag ... preloader="com.YYY.XXX.shell.view.CustomPreloader"
CustomPreloader
package com.YYY.XXX.shell.view
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
import mx.events.FlexEvent;
import mx.preloaders.DownloadProgressBar;
public final class CustomPreloader
extends DownloadProgressBar
{
public var loader : LoadScreen;
private var _timer : Timer;
public function CustomPreloader()
{
super();
}
override public function initialize() : void
{
super.initialize();
this.loader = new LoadScreen();
this.addChild(this.loader);
this._timer = new Timer(1);
this._timer.addEventListener(TimerEvent.TIMER, handleTimerTick);
this._timer.start();
}
override public function set preloader(preloader : Sprite):void
{
preloader.addEventListener(ProgressEvent.PROGRESS, SWFDownLoadScreen);
preloader.addEventListener(Event.COMPLETE, SWFDownloadComplete);
preloader.addEventListener(FlexEvent.INIT_PROGRESS, FlexInitProgress);
preloader.addEventListener(FlexEvent.INIT_COMPLETE, FlexInitComplete);
}
private function SWFDownLoadScreen(event : ProgressEvent) : void
{
var prog : Number = event.bytesLoaded / event.bytesTotal * 100;
if (this.loader)
{
this.loader.progress = prog;
}
}
private function handleTimerTick(event : TimerEvent) : void
{
this.stage.addChild(this);
this.loader.x = (this.stageWidth - this.loader.width) / 2;
this.loader.y = (this.stageHeight - this.loader.height) / 2;
this.loader.refresh();
}
private function SWFDownloadComplete(event : Event) : void {}
private function FlexInitProgress(event : Event) : void {}
private function FlexInitComplete(event : Event) : void
{
this.loader.ready = true;
this._timer.stop();
this.dispatchEvent(new Event(Event.COMPLETE));
}
override protected function showDisplayForInit(elapsedTime:int, count:int):Boolean
{
return true;
}
override protected function showDisplayForDownloading(elapsedTime:int,
event:ProgressEvent):Boolean
{
return true;
}
}
}
LoadScreen
package com.YYY.XXX.shell.view
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Graphics;
import flash.display.Loader;
import flash.display.Sprite;
import flash.utils.ByteArray;
import mx.graphics.codec.PNGEncoder;
public class LoadScreen extends Loader {
//~ Settings ----------------------------------------------------------
private static var _BarWidth : int = 153; // Progress bar width
private static var _BarHeight : int = 12; // Progress bar height
private static var _LogoHeight : int = 153; // Logo picture height
private static var _LogoWidth : int = 68; // Logo picture width
private static var _Padding : int = 5; // Spacing between logo and progress bar
private static var _LeftMargin : int = 0; // Left Margin
private static var _RightMargin : int = 0; // Right Margin
private static var _TopMargin : int = 1; // Top Margin
private static var _BottomMargin : int = 1; // Bottom Margin
private static var _BarBackground : uint = 0xFFFFFF; // background of progress bar
private static var _BarOuterBorder : uint = 0x737373; // color of outer border
private static var _BarColor : uint = 0x6F9FD5; // color of prog bar
private static var _BarInnerColor : uint = 0xFFFFFF; // inner color of prog bar
//~ Instance Attributes -----------------------------------------------
[Embed(source="/asset/embed/img/XXX.gif")]
private var MyLogoClass: Class;
private var _logo : Bitmap;
private var _logoData : BitmapData;
private var isReady : Boolean = false;
public var progress : Number;
//~ Constructor -------------------------------------------------------
public function LoadScreen()
{
super();
this.progress = 0;
this._logo = new MyLogoClass as Bitmap;
}
//~ Methods -----------------------------------------------------------
public function refresh() : void
{
this._logoData = this.draw();
var encoder : PNGEncoder = new PNGEncoder();
var bytes : ByteArray = encoder.encode(this._logoData);
this.loadBytes(bytes);
}
override public function get width() : Number
{
return Math.max(_BarWidth, _LogoWidth) + _LeftMargin + _RightMargin;
}
override public function get height() : Number
{
return _LogoHeight + _BarHeight + _Padding + _TopMargin + _BottomMargin;
}
private function draw() : BitmapData
{
// create bitmap data to create the data
var data : BitmapData = new BitmapData(this.width, this.height, true, 0);
// draw the progress bar
var s : Sprite = new Sprite();
var g : Graphics = s.graphics;
// draw the bar background
g.beginFill(_BarBackground);
g.lineStyle(2, _BarOuterBorder, 1, true);
var px : int = (this.width - _BarWidth) / 2;
var py : int = _TopMargin + _LogoHeight + _Padding;
g.drawRoundRect(px, py, _BarWidth, _BarHeight, 2);
var containerWidth : Number = _BarWidth - 4;
var progWidth : Number = containerWidth * this.progress / 100;
g.beginFill(_BarColor);
g.lineStyle(1, _BarInnerColor, 1, true);
g.drawRect(px + 1, py + 1, progWidth, _BarHeight - 3);
data.draw(s);
// draw the logo
data.draw(this._logo.bitmapData, null, null, null, null, true);
return data;
}
public function set ready(value : Boolean) : void
{
this.isReady = value;
this.visible = !this.isReady;
}
public function get ready() : Boolean { return this.isReady; }
}
}
I feel dumb...I was referencing one of my main application classes from within the preloader, thus causing all of my classes to be compiled into the preloader, meaning it cannot display the preloader until everything is loaded.
For future reference: Double-check every reference in your preloader, make sure you use nothing more than what is absolutely necessary
There is a Flex 4 Custom Preloader code sample here http://www.leavethatthingalone.com/blog/index.cfm/2009/11/11/Flex4CustomPreloader