PDA

View Full Version : Total newbie question (Part II) for old Flash programmer


ejansson
09-27-2008, 04:42 PM
I'm trying to put an existing Flash gameinto Flex. The Flash program is entirely AS except for the initial frame where I load the movie clip. Just trying to move it into Flex.

Here's an illustration of my problem, which involves Sprites and Controls. Why does the button not show up in this sample app:

Flex MXML file:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" >

<mx:Script>
<![CDATA[
import mx.core.UIComponent;
import mx.containers.Canvas;
import Game;

protected function init():void
{
var game:Game = new Game();
var u:UIComponent = new UIComponent();
u.addChild(game); // can't add sprite directly or we get an error
holder.addChild(u);
}

]]>
</mx:Script>

<mx:Canvas id="holder" width="100%" height="100%" creationComplete="init()"/>

</mx:Application>

AS Class file:

package
{
import flash.display.Sprite;
import mx.controls.Button;

public class Game extends Sprite
{
public function Game()
{
var b = new Button();
b.label = "My Button";
b.width = 100;
b.height = 35;
addChild(b); // does not work: no button shows up
}
}
}

Could not be simpler, but doesn't work.

I've been banging my head against this problem for 2 days. Please help me before my brains liquefy.

EJ

NickZA
09-27-2008, 06:59 PM
EJ,

Uhhm, I think you need to take out that canvas.

My structure is as follows and it works fine:


<mx:Application>
<mx:states>
<mx:State>
<!--Flex UI stuff...-->
</mx:State>
<mx:State>
<mx:AddChild>
<mx:UIComponent>
<!--Add your standard DisplayObjects to the display list here.-->
</mx:UIComponent>
</mx:AddChild>
</mx:State>

</mx:states>
</mx:Application>


and so I'm guessing that...


<mx:Application>
<mx:UIComponent>
<!--Add your standard DisplayObjects to the display list here.-->
</mx:UIComponent>
</mx:Application>


...Should work for you since it's the same thing without using Flex States.

Otherwise I suggest (as I often do) that you whittle your problem down to the simplest case and see where you're going wrong. Meaning don't include your whole game, rather try and get individual adding DisplayObjects to mx:Application working first. And take it from there.

-Nick