hey all - here’s the deal, i’ve been using Flash pretty comfortably for a while now, building my own class files etc etc.
i’m trying to make the transition over to using Flex Builder for both my AS projects and for a Flex project i’m doing for a class (school class - not OOP class!).
my problem is that i have some fundamental confusion over what is and what isn’t possible in Flex vs Flash, and more specifically, in AS Projects (created in Flex Builder) vs Flex Projects.
anyway let me get down to specifics -i built some basic mouse follow projects in Flash (based on Friend of Ed “AS3 Animation” code) that i am trying to port over into a Flex Project.
i’ll post the code in a moment, but first let me just say that when i used Flex Builder to build an “Actionscript Project” i was able to get this animation to play in the browser without too much trouble.
but now that i’m trying it in a “Flex Project” i’m getting the above Coercion Error.
first, here’s the main Class file - Mouser:
package classes
{
import flash.display.Sprite;
import flash.events.*;
public class Mouser extends Sprite
{
private var arrow:Arrow;
private var vx:Number = 0;
private var vy:Number = 0;
private var force:Number = .5;
public function Mouser()
{
super();
arrow = new Arrow();
addChild(arrow);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(event:Event):void
{
var dx:Number = mouseX - arrow.x;
var dy:Number = mouseY - arrow.y;
var angle:Number = Math.atan2(dy, dx);
arrow.rotation = angle * 180 / Math.PI;
var ax:Number = Math.cos(angle) * force;
var ay:Number = Math.sin(angle) * force;
vx += ax;
vy += ay;
arrow.x += vx;
arrow.y += vy;
}
}
}
and here’s the Arrow Class file (in the same directory):
package {
import flash.display.Sprite;
public class Arrow extends Sprite {
public function Arrow() {
graphics.lineStyle(1, 0, 1);
graphics.beginFill(0xff0000);
graphics.moveTo(-50, -25);
graphics.lineTo(0, -25);
graphics.lineTo(0, -50);
graphics.lineTo(50, 0);
graphics.lineTo(0, 50);
graphics.lineTo(0, 25);
graphics.lineTo(-50, 25);
graphics.lineTo(-50, -25);
graphics.endFill();
}
/*public function init():void {
}*/
}
}
and finally, the 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 classes.Mouser
public var myFollow:Mouser = new Mouser();
public function init():void {
addChild(myFollow);
}
]]>
</mx:Script>
<mx:Button x="300" y="425" label="Make Mouser" id="makeMouser" click="init();"/>
</mx:Application>
as far as i can tell from using the Debugger, everything ‘works’ until the AddChild(myFollow); statement - but as soon as i click the button this shows up in the Console:
[SWF] Users:bob:Desktop:SWARM:bin-debug:SWARM.swf - 555,188 bytes after decompression
TypeError: Error #1034: Type Coercion failed: cannot convert classes::Mouser@9ae4629 to mx.core.IUIComponent.
at mx.core::Container/http://www.adobe.com/2006/flex/mx/internal::addingChild()[E:\dev\3.0.x\frameworks\projects\framework\src\mx\core\Container.as:3251]
at mx.core::Container/addChildAt()[E:\dev\3.0.x\frameworks\projects\framework\src\mx\core\Container.as:2200]
at mx.core::Container/addChild()[E:\dev\3.0.x\frameworks\projects\framework\src\mx\core\Container.as:2140]
at SWARM/init()[/Users/bob/Desktop/SWARM/src/SWARM.mxml:15]
at SWARM/__makeFollower_click()[/Users/bob/Desktop/SWARM/src/SWARM.mxml:22]
Please tell me there’s a way to use custom “Flash-type” graphical objects in Flex?!? i thought that a Flex project had access to all of the Flash libraries and classes. am i wrong? are there well formed AS classes that run in Flash but that cannot be made to fun in Flex?
i should also add that i need the final project to be a Flex Project and not an Actionscript Project built in the Flex Builder… part of the course requirements you see.
anyway, i could REALLY use some help with this, i’m pulling my hair out. and since i’m already bald, i’m pulling my nose hair out - and that hurts.
please remember to ‘dumb-down’ any responses to allow for my near-total ignorance.
thanks alot and sorry for the epic post!
bennett