Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > Flex > Flex 2, 3 & 4

Reply
 
Thread Tools Rate Thread Display Modes
Old 11-16-2006, 06:50 PM   #1
meddlingwithfir
Senior Member
 
Join Date: Aug 2005
Location: Minnesota, USA
Posts: 216
Default Using Sprite objects to draw to screen

How are these things supposed to be used? I'm apparently missing the mark. I figured I could use them to display images & shapes in my application. But it doesn't look like I can add them in my MXML or Actionscript:

Code:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application 
	xmlns:mx="http://www.adobe.com/2006/mxml" 
	layout="absolute" 
	width="800"
	height="600"
	creationComplete="onLoad()"
>
	<mx:Script>
		<![CDATA[

			private function onLoad():void
			{
				var square:Sprite = new Sprite();
				square.graphics.beginFill(0xFF88CC);
				square.graphics.drawRect(0, 0, 80, 80);
				panel1.addChild(square);
			}
		]]>
	</mx:Script>

	<mx:Panel id="panel1" 
		x="0" y="1000"
		width="50%" 
		height="80%" 
		title="panel1">
	</mx:Panel>
	<mx:Panel id="panel2" 
		x="400" y="1000" 
		width="50%"
		height="80%" 
		title="panel2">
	</mx:Panel>

	
</mx:Application>
Throws an error during runtime:

TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::Sprite@4dee191 to mx.core.IUIComponent.
at mx.core::Container/http://www.adobe.com/2006/flex/mx/internal::addingChild()
at mx.core::Container/addChildAt()
at mx.core::Container/addChild()
at SpaceFlex/:nLoad()
at SpaceFlex/___Application1_creationComplete()
at flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEventFunctio n()
at flash.events::EventDispatcher/dispatchEvent()
at mx.core::UIComponent/set initialized()
at mx.managers::LayoutManager/::doPhasedInstantiation()
at Function/http://adobe.com/AS3/2006/builtin::apply()
at mx.core::UIComponent/::callLaterDispatcher2()
at mx.core::UIComponent/::callLaterDispatcher()

So I can see that a Sprite isn't of IUIComponent -- so it won't let me add it directly to the panel or to the application. How am I supposed to use these things in my Flex application? I checked out the livedocs: http://livedocs.macromedia.com/flex/...ay/Sprite.html and I don't see why their subclass of Sprite would work, while a regular plain-jane Sprite wouldn't. It doesn't look like they've made any conversion of the Sprite to a IUIComponent...

Pretty much all I'm after is trying to programatically draw a square (or whatever other shape my miniscule brain can conjure ) and display that to the screen.
meddlingwithfir is offline   Reply With Quote
Old 11-16-2006, 07:14 PM   #2
CDHBookingEdge
Registered User
 
Join Date: Oct 2006
Posts: 383
Send a message via MSN to CDHBookingEdge
Default

Trying to work this out in my head too so forgive me, and based on the fact that I'm kinda working "crippled" here at the moment, kinda bear with me.

Sprite is a subclass of ...never mind..LOL here might be the answer.

Try using FlexSprite In the documentation for it, it says:
Quote:
FlexSprite is a subclass of the Player's Sprite class and the superclass of UIComponent. It overrides the toString() method to return a string indicating the location of the object within the hierarchy of DisplayObjects in the application.
Also since FlexSprite is in mx.core and therefore in the Flex 2 SDK you can look at it's implementation to see how they did it and if it doesn't quite work for you, maybe use some similar concepts, to make something that will work.

See if that works for you and let us know. ;-)

Christopher
CDHBookingEdge is offline   Reply With Quote
Old 11-16-2006, 07:25 PM   #3
Tink
Addict
 
Tink's Avatar
 
Join Date: Nov 2001
Location: London
Posts: 2,128
Default

Anything added to a UIComponent needs to be a UIComponent. A Sprite is not, you would use it for Flash and ActionScript projects.

If you really want to use Sprites you can add them as a rawChild or components that have rawChildren

Code:
var square:Sprite = new Sprite();
square.graphics.beginFill(0xFF88CC);
square.graphics.drawRect(0, 0, 80, 80);
panel1.rawChildren.addChild(square);
Also if you were just drawing, you be bext of using a Shape instead of a Sprite.
Tink is offline   Reply With Quote
Old 11-16-2006, 08:07 PM   #4
CDHBookingEdge
Registered User
 
Join Date: Oct 2006
Posts: 383
Send a message via MSN to CDHBookingEdge
Default

Interesting! So what the heck is a FlexSprite and where would you really use it?
CDHBookingEdge is offline   Reply With Quote
Old 11-16-2006, 08:28 PM   #5
CDHBookingEdge
Registered User
 
Join Date: Oct 2006
Posts: 383
Send a message via MSN to CDHBookingEdge
Default

Interesting! So what the heck is a FlexSprite and where would you really use it?
CDHBookingEdge is offline   Reply With Quote
Old 11-16-2006, 08:29 PM   #6
CDHBookingEdge
Registered User
 
Join Date: Oct 2006
Posts: 383
Send a message via MSN to CDHBookingEdge
Default

Sorry for the double post, apparently AS.org wasn't responding or such.
CDHBookingEdge is offline   Reply With Quote
Old 11-16-2006, 08:36 PM   #7
meddlingwithfir
Senior Member
 
Join Date: Aug 2005
Location: Minnesota, USA
Posts: 216
Default

CDH -- It sounds like FlexSprite is what you should use, from what you said. But if it's a superclass to UIComponent, wouldn't that mean that it is not in-itself a UIComponent, and therefore I'd get the same error as before?

Tink -- would that be considered a "dirty" way to get the functionality I'm looking for (just drawing a simple shape to the screen programatically), or is that the expected method?
meddlingwithfir is offline   Reply With Quote
Old 11-17-2006, 01:03 AM   #8
CDHBookingEdge
Registered User
 
Join Date: Oct 2006
Posts: 383
Send a message via MSN to CDHBookingEdge
Default

You might need to cast it to a UIComponent, true. (Ducks at the castigation I might receive for throwing that out there. And hopefully not other words that beging with cast LOL) But if you cast it shouldn't you be able to "cirumvent" the compiler error?

Laughs at myself after having deleted what I was going to write, about 3 or 4 times.

Tink's probably got the best idea. He usually does. ;-)

Christopher
CDHBookingEdge is offline   Reply With Quote
Old 11-17-2006, 02:58 AM   #9
meddlingwithfir
Senior Member
 
Join Date: Aug 2005
Location: Minnesota, USA
Posts: 216
Default

Haha I get the same impulses too! Just try anything and everything that might work

I'll give the casting thing a try tomorrow, along with Tinks suggestion. It just seems like I'm going about this wrong though -- like there should be some other class out there for me to use (this seems like fighting an uphill battle for what I think should be a fairly trivial task...). And usually when that happens, it means I'm doing something wrong
meddlingwithfir is offline   Reply With Quote
Old 11-17-2006, 04:48 PM   #10
dr_zeus
Feathers
 
dr_zeus's Avatar
 
Join Date: Mar 2005
Location: Silicon Valley
Posts: 1,999
Default

You could instantiate a UIComponent and draw to that:

Code:
private function onLoad():void
{
	var square:UIComponent = new UIComponent ();
	square.graphics.beginFill(0xFF88CC);
	square.graphics.drawRect(0, 0, 80, 80);
	panel1.addChild(square);
}
The reason you're receiving this error is because Flex Containers expect to receive UIComponents as children. Containers need access to certain functions and properties of UIComponents to position them properly. Tink suggested using rawChildren, which is used internally by the Panel to attach it's skin pieces that aren't UIComponents.
__________________
Josh Tynjala | Feathers
dr_zeus is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Scrollpane component does not draw to screen on load raznov Components 4 07-13-2006 08:44 PM
Using Drawing API to draw on screen and then having flash redraw in "real time" brianhersey ActionScript 1.0 (and below) 2 02-09-2006 04:31 PM
Win SP2 - the end of full screen? hakiro Other Flash General Questions 0 05-12-2005 01:56 PM
simple for loop >>pls help Samantha Gram ActionScript 1.0 (and below) 5 08-04-2004 09:28 AM
enterframe and load and objects, oh my! dice ActionScript 1.0 (and below) 13 07-18-2003 11:07 AM


All times are GMT. The time now is 10:04 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.