<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule"
>

<channel>
	<title>Streamhead&#187; featured Archives  &#8211; Streamhead</title>
	<atom:link href="http://www.streamhead.com/category/featured/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.streamhead.com</link>
	<description>multimedia webapplication thoughts and experiments</description>
	<lastBuildDate>Wed, 08 Sep 2010 20:55:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
<creativeCommons:license>http://creativecommons.org/licenses/by-sa/3.0/</creativeCommons:license>
		<item>
		<title>Sandy 3D Tutorial: Getting Started with Sandy and FlashDevelop</title>
		<link>http://www.streamhead.com/sandy-3d-tutorial/</link>
		<comments>http://www.streamhead.com/sandy-3d-tutorial/#comments</comments>
		<pubDate>Tue, 05 May 2009 08:00:04 +0000</pubDate>
		<dc:creator>Peter Backx</dc:creator>
				<category><![CDATA[FlashMedia]]></category>
		<category><![CDATA[featured]]></category>

		<guid isPermaLink="false">http://www.streamhead.com/?p=1278</guid>
		<description><![CDATA[Over the last year or two, developing 3D applications in Flash has become increasingly feasible, for almost any ActionScript developers. A number of open source 3D engines, like Sandy 3D, have popped up. Combined with the incredible FlashDevelop, any one can get started, right away. However, because FlashDevelop does not have the authoring tools of [...]]]></description>
			<content:encoded><![CDATA[<p>Over the last year or two, developing 3D applications in Flash has become increasingly feasible, for almost any ActionScript developers. A number of open source 3D engines, like <a title="Sandy 3D engine for AS3 &amp; AS2" href="http://www.flashsandy.org/" target="_blank">Sandy 3D</a>, have popped up. Combined with the incredible <a title="FlashDevelop.org" href="http://www.flashdevelop.org/" target="_blank">FlashDevelop</a>, any one can get started, right away. However, because FlashDevelop does not have the authoring tools of Adobe&#8217;s Flash tools, you need to know a few differences that you won&#8217;t find in many tutorials, but I will explain them in this post.</p>
<p>This is a refreshed version of <a title="Tutorial - getting started with Sandy 3D and FlashDevelop" href="http://www.streamhead.com/tutorial-getting-started-with-sandy-3d-and-flashdevelop/" target="_blank">my previous tutorial</a>, where I explained the important differences you need to know to get started with FlashDevelop and Sandy 3D. I won&#8217;t go into setting up FlashDevelop, all details mentioned in the previous post are still valid.</p>
<p>But the Sandy 3D Engine has changed slightly (for the better, mind you). I&#8217;ll point out what&#8217;s new and give some additional examples. But first, lets look at the result:</p>
<p><object type="application/x-shockwave-flash" data="http://www.streamhead.com/wp-content/uploads/2009/04/boxes.swf" width="778" height="300" class="embedflash"><param name="movie" value="http://www.streamhead.com/wp-content/uploads/2009/04/boxes.swf" /><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><small>(Please open the article to see the flash file or player.)</small></object></p>
<p>The code consists of 4 main parts, I&#8217;ve split them up, but if you want, you can go right ahead and download the entire FlashDevelop workspace:</p>
<p><a title="FlashDevelop example project with Sandy3D" href="http://www.streamhead.com/wp-content/uploads/2009/05/boxes.zip" target="_blank"><img class="alignleft size-full wp-image-498" title="download" src="http://www.streamhead.com/wp-content/uploads/2008/11/download.png" alt="download" width="30" height="24" />download the full source and FlashDevelop project</a></p>
<h2>1. Using bitmaps</h2>
<p>I&#8217;ve gone over this a few times, but it bears repeating as this is something you won&#8217;t use in CS3. In FlashDevelop, you add a bitmap in the lib directory. With this done, you put an embed tag in your code (or double click, FD can usually do this for you) and on the next line you put the class name you want to assign to this embedded resource. As a final step, you need to instantiate the object.</p>
<p>It sounds much more complicated then it is, just look at the code:</p>

<div class="wp_codebox"><table><tr id="p12786"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p1278code6"><pre class="actionscript" style="font-family:monospace;"><span style="color: #66cc66;">&#91;</span>Embed<span style="color: #66cc66;">&#40;</span>source = <span style="color: #ff0000;">'../../../../lib/aqua.png'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> aquaClass:<span style="color: #000000; font-weight: bold;">Class</span>;
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> aquaBitmap:Bitmap = <span style="color: #000000; font-weight: bold;">new</span> aquaClass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<h2>2. Creating and texturing the 3D objects</h2>
<p>With the bitmaps imported, we can use them to texture objects. This is done by setting the objects appearance. The appearance has a few options, but for now, we&#8217;ll only set one material on it. The material is the object that actually contains the texture.</p>
<p>For instance, the background image when you move over the cube is a Plane3D object on which we set an appearance that consists of a BitmapMaterial:</p>

<div class="wp_codebox"><table><tr id="p12787"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p1278code7"><pre class="actionscript" style="font-family:monospace;">plane = <span style="color: #000000; font-weight: bold;">new</span> Plane3D<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;thePlane&quot;</span>, <span style="color: #cc66cc;">200</span>, <span style="color: #cc66cc;">200</span><span style="color: #66cc66;">&#41;</span>;
material = <span style="color: #000000; font-weight: bold;">new</span> BitmapMaterial<span style="color: #66cc66;">&#40;</span>beamsBitmap.<span style="color: #006600;">bitmapData</span><span style="color: #66cc66;">&#41;</span>;
material.<span style="color: #006600;">lightingEnable</span> = <span style="color: #000000; font-weight: bold;">true</span>;
plane.<span style="color: #006600;">appearance</span> = <span style="color: #000000; font-weight: bold;">new</span> Appearance<span style="color: #66cc66;">&#40;</span>material<span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>More complicated objects can contain multiple surfaces. If you want to set them all alike, just use the code above, but you can also address them individually. You use the &#8220;aPolygons&#8221; collection for that. Depending on the object and the parameters you choose, you might need to experiment a little to find the correct polygons.</p>
<p>For instance, this code sets the front and back side of the box in the example:</p>

<div class="wp_codebox"><table><tr id="p12788"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p1278code8"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> material:Material = <span style="color: #000000; font-weight: bold;">new</span> BitmapMaterial<span style="color: #66cc66;">&#40;</span>logoBitmap.<span style="color: #006600;">bitmapData</span><span style="color: #66cc66;">&#41;</span>;
material.<span style="color: #006600;">lightingEnable</span> = <span style="color: #000000; font-weight: bold;">true</span>;
<span style="color: #000000; font-weight: bold;">var</span> app:Appearance = <span style="color: #000000; font-weight: bold;">new</span> Appearance<span style="color: #66cc66;">&#40;</span> material <span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span>;
<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>i = <span style="color: #cc66cc;">0</span>; i <span style="color: #66cc66;">&amp;</span>lt; <span style="color: #cc66cc;">4</span>; i++ <span style="color: #66cc66;">&#41;</span>
    myBox.<span style="color: #006600;">aPolygons</span><span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">appearance</span> = app;</pre></td></tr></table></div>

<h2>3. Listening to events</h2>
<p>While the way you handle events hasn&#8217;t changed since previous versions of Sandy, the MOUSE_MOVE event was removed. It is no longer handled, probably because of performance reasons, but I&#8217;m not entirely clear on the reasons.</p>
<p>The official tutorials also now suggest to put the event handling on the container, this is part of your object that is actually displayed. If you can, I would suggest to use this, because it&#8217;s probably a lot quicker.</p>
<p>There are cases where you might like to obtain the Shape3DEvent. Take a look at <a title="Tutorial 18 - Sandy events" href="http://www.flashsandy.org/tutorials/3.0/sandy_cs3_tut18" target="_blank">the tutorial on the Sandy site</a> to decide if you&#8217;d need that. But to reiterate, the Sandy event system no longer supports the MOUSE_MOVE event.</p>
<p>In the example, the event listener for the mouse over event looks like this:</p>

<div class="wp_codebox"><table><tr id="p12789"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p1278code9"><pre class="actionscript" style="font-family:monospace;">    myBox.<span style="color: #006600;">container</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_OVER</span>, overHandler<span style="color: #66cc66;">&#41;</span>;
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> overHandler<span style="color: #66cc66;">&#40;</span>event:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span>zoomedIn<span style="color: #66cc66;">&#41;</span>
        g.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>plane<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<h2>4. Setting up the scene and camera</h2>
<p>New in Sandy3D (although it&#8217;s not that new any longer) is the removal of World3D. World3D was a singleton class, which meant that you could have only one in your program. It has now been replaced by Scene3D, of which you can create and render multiple in your program.</p>
<p>So if you want, you can have different scenes or different cameras in your program. Cool, but don&#8217;t overdo it, keep in mind that every scene you add will also add to the processing power required.</p>
<p>Setting up the camera is exactly the same as before:</p>

<div class="wp_codebox"><table><tr id="p127810"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code" id="p1278code10"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
    removeEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ADDED_TO_STAGE</span>, init<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #808080; font-style: italic;">// entry point</span>
    <span style="color: #0066CC;">camera</span> = <span style="color: #000000; font-weight: bold;">new</span> Camera3D<span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">300</span>, <span style="color: #cc66cc;">300</span> <span style="color: #66cc66;">&#41;</span>;
    <span style="color: #0066CC;">camera</span>.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">0</span>;
    <span style="color: #0066CC;">camera</span>.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">50</span>;
    <span style="color: #0066CC;">camera</span>.<span style="color: #006600;">z</span> = -<span style="color: #cc66cc;">200</span>;
    <span style="color: #0066CC;">camera</span>.<span style="color: #006600;">lookAt</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #000000; font-weight: bold;">var</span> root:Group = createScene<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    scene = <span style="color: #000000; font-weight: bold;">new</span> Scene3D<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;scene&quot;</span>, <span style="color: #0066CC;">this</span>, <span style="color: #0066CC;">camera</span>, root <span style="color: #66cc66;">&#41;</span>;
&nbsp;
    addEventListener<span style="color: #66cc66;">&#40;</span> Event.<span style="color: #006600;">ENTER_FRAME</span>, enterFrameHandler <span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>I haven&#8217;t discussed the tweening used to move the cube in the program. If you&#8217;re interested, please let me know and I&#8217;ll write another blogpost. Otherwise, you now have all parts to get started with 3D development in FlashDevelop.</p>
<p>Good luck and don&#8217;t forget to show those results!</p>
<p><a title="FlashDevelop example project with Sandy3D" href="http://www.streamhead.com/wp-content/uploads/2009/05/boxes.zip" target="_blank"><img class="alignleft size-full wp-image-498" title="download" src="http://www.streamhead.com/wp-content/uploads/2008/11/download.png" alt="download" width="30" height="24" />download the full source and FlashDevelop project</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.streamhead.com/sandy-3d-tutorial/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-sa/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>The Lazy ActionScript Developer&#8217;s Way to Stream Flash Video</title>
		<link>http://www.streamhead.com/lazy-actionscript-developers-stream-flash-video/</link>
		<comments>http://www.streamhead.com/lazy-actionscript-developers-stream-flash-video/#comments</comments>
		<pubDate>Tue, 04 Nov 2008 08:00:40 +0000</pubDate>
		<dc:creator>Peter Backx</dc:creator>
				<category><![CDATA[FlashMedia]]></category>
		<category><![CDATA[featured]]></category>
		<category><![CDATA[recommended]]></category>

		<guid isPermaLink="false">http://www.streamhead.com/?p=450</guid>
		<description><![CDATA[In this final installment of my tutorial on YouTube hacking, I&#8217;m going to show how to build the FLV URL for YouTube videos and start streaming the FLV in your ActionScript application. As you will notice, this is very little work, because it is based on the streaming example in the AS3 language reference. I&#8217;m [...]]]></description>
			<content:encoded><![CDATA[<p>In this final installment of my tutorial on YouTube hacking, I&#8217;m going to show <strong>how to build the FLV URL for YouTube videos</strong> and start <strong>streaming the FLV</strong> in your ActionScript application. As you will notice, this is very little work, because it is based on the streaming example in the AS3 language reference. I&#8217;m being lazy.</p>
<p>A recap of <strong>the ActionScript 3.0 YouTube player</strong> series:</p>
<ul>
<li><a title="Little Known Way to Create a Fully Customizable ActionScript 3.0 YouTube Player - Streamhead" href="../create-fully-customizable-actionscript-30-youtube-player/" target="_blank">Getting <strong>a list of videos</strong> (and some project setup)</a></li>
<li><a title="what you should know about flash.display.StageScaleMode" href="http://www.streamhead.com/flash-developer-flashdisplaystagescalemode/" target="_blank">A bit about how <strong>the Flash stage</strong> works and <strong>fullscreen</strong></a></li>
<li><a title="The YouTube Flash FLV secret you should know" href="http://www.streamhead.com/youtube-flash-video-flv-secret/" target="_blank">Install <strong>a php script</strong> to get the video parameters<br />
</a></li>
<li><a title="Tying the YouTube Services Together" href="http://www.streamhead.com/tying-youtube-services-pro-web-20-developer/" target="_blank">Getting the <strong>parameters for a video</strong></a></li>
<li>playing the video using <strong>Flash’s FLV player</strong> classes [this post]</li>
</ul>
<h2>Constructing the YouTube video URL</h2>
<p>There are several ways to get to this one. You could fire up FireBug and check the network logs, or you could check <a title="YouTube AS3 API" href="http://www.lostinactionscript.com/blog/index.php/2007/10/13/flash-you-tube-api/" target="_blank">Shane&#8217;s code</a> and open his Main.as class. Skip to line 67 and you&#8217;ll see:</p>

<div class="wp_codebox"><table><tr id="p45013"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p450code13"><pre class="actionscript" style="font-family:monospace;"><span style="color: #b1b100;">case</span> YouTube.<span style="color: #006600;">VIDEOID</span> :
	fvc.<span style="color: #0066CC;">play</span><span style="color: #66cc66;">&#40;</span>YouTube.<span style="color: #006600;">FLVUrl</span> + <span style="color: #0066CC;">e</span>.<span style="color: #006600;">value</span>.<span style="color: #006600;">id</span> + <span style="color: #ff0000;">&quot;&amp;amp;t=&quot;</span> + <span style="color: #0066CC;">e</span>.<span style="color: #006600;">value</span>.<span style="color: #006600;">t</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #b1b100;">break</span>;</pre></td></tr></table></div>

<p>What this means: to construct the full URL to a YouTube Flash video file, you take the base URL and add both the video id and the T-parameter as arguments. An example URL might look like this:</p>
<p>http://www.youtube.com/get_video?<strong>video_id</strong>=v6ICEpYEcD8&amp;<strong>t</strong>=OEgsToPDskIltmXs7gZ89kc2a3Z1SRGN</p>
<p>(I have not linked the URL, as it probably won&#8217;t work, because the T-parameter changes over time)</p>
<h2>Streaming the FLV</h2>
<p>For this one I&#8217;m going to use the existing ActionScript 3 classes. In particular <a title="flash.net.NetConnection language reference" href="http://livedocs.adobe.com/flex/3/langref/flash/net/NetConnection.html" target="_blank">flash.net.NetConnection</a> and <a title="flash.net.NetStream language reference" href="http://livedocs.adobe.com/flex/3/langref/flash/net/NetStream.html" target="_blank">flash.net.NetStream</a>. In the following code I&#8217;m adding <a title="flash.net.NetStream example" href="http://livedocs.adobe.com/flex/3/langref/flash/net/NetStream.html#includeExamplesSummary" target="_blank">the language reference example</a> to the already existing code.</p>
<p>What happens might not be clear when you look at the code. That&#8217;s because we use a lot of event handlers. But if you take your time and go over the code it will become clear. What goes on is this:</p>
<ol>
<li>We create a NetConnection.</li>
<li>If this goes well the netStatusHandler is called with the event &#8220;NetConnection.Connect.Success&#8221;. This in turn will launch the YouTube query to locate a video and create the URL to play it.</li>
<li>This part we&#8217;ve already discussed in previous posts:
<ol>
<li>Create the YouTube object and add listeners.</li>
<li>Get a list of videos for a tag.</li>
<li>When that lists returns, select the first one and get the T-parameter from our PHP script</li>
</ol>
</li>
<li>We construct the YouTube FLV URL (see above) and call the play method.</li>
<li>The play method creates a NetStream and Video object and starts the play the video.</li>
</ol>
<p>Everything else you see is basically code to handle possible errors:</p>

<div class="wp_codebox"><table><tr id="p45014"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
</pre></td><td class="code" id="p450code14"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #0066CC;">import</span> com.<span style="color: #006600;">flashdynamix</span>.<span style="color: #006600;">events</span>.<span style="color: #006600;">CustomEvent</span>;
  <span style="color: #0066CC;">import</span> com.<span style="color: #006600;">flashdynamix</span>.<span style="color: #006600;">services</span>.<span style="color: #006600;">YouTube</span>;
  <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
  <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">AsyncErrorEvent</span>;
  <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">ErrorEvent</span>;
  <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
  <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">NetStatusEvent</span>;
  <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">SecurityErrorEvent</span>;
  <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">media</span>.<span style="color: #0066CC;">Video</span>;
  <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #0066CC;">NetConnection</span>;
  <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #0066CC;">NetStream</span>;
&nbsp;
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #0066CC;">extends</span> Sprite <span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> connection : <span style="color: #0066CC;">NetConnection</span>;
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> stream : <span style="color: #0066CC;">NetStream</span>;
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">video</span> : <span style="color: #0066CC;">Video</span>;
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> yt : YouTube;
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Main<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">stage</span><span style="color: #66cc66;">&#41;</span> init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
      <span style="color: #b1b100;">else</span> addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ADDED_TO_STAGE</span>, init<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
      removeEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ADDED_TO_STAGE</span>, init<span style="color: #66cc66;">&#41;</span>;
&nbsp;
      <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;creating NetConnection&quot;</span><span style="color: #66cc66;">&#41;</span>;
      connection = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">NetConnection</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
      connection.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>NetStatusEvent.<span style="color: #006600;">NET_STATUS</span>, netStatusHandler<span style="color: #66cc66;">&#41;</span>;
      connection.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>SecurityErrorEvent.<span style="color: #006600;">SECURITY_ERROR</span>, securityErrorHandler<span style="color: #66cc66;">&#41;</span>;
      connection.<span style="color: #0066CC;">connect</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> netStatusHandler<span style="color: #66cc66;">&#40;</span>event : NetStatusEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #b1b100;">switch</span> <span style="color: #66cc66;">&#40;</span>event.<span style="color: #006600;">info</span>.<span style="color: #006600;">code</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;NetConnection.Connect.Success&quot;</span>:
          connectStream<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
          <span style="color: #b1b100;">break</span>;
        <span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;NetStream.Play.StreamNotFound&quot;</span>:
          <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Unable to locate video&quot;</span><span style="color: #66cc66;">&#41;</span>;
          <span style="color: #b1b100;">break</span>;
        <span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;NetStream.Play.Start&quot;</span>:
          <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;start playing&quot;</span><span style="color: #66cc66;">&#41;</span>;
          <span style="color: #b1b100;">break</span>;
        <span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;NetStream.Play.Stop&quot;</span> :
          <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;stop playing&quot;</span><span style="color: #66cc66;">&#41;</span>;
          <span style="color: #b1b100;">break</span>;
      <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> securityErrorHandler<span style="color: #66cc66;">&#40;</span>event:SecurityErrorEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;securityErrorHandler: &quot;</span> + event<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> connectStream<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;start YouTube search&quot;</span><span style="color: #66cc66;">&#41;</span>;
      yt = <span style="color: #000000; font-weight: bold;">new</span> YouTube<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
      yt.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>, onLoaded<span style="color: #66cc66;">&#41;</span>;
      yt.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>ErrorEvent.<span style="color: #0066CC;">ERROR</span>, onError<span style="color: #66cc66;">&#41;</span>;
      yt.<span style="color: #006600;">videosForTag</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;lego&quot;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> onLoaded<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:CustomEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #b1b100;">switch</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #006600;">id</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">case</span> YouTube.<span style="color: #006600;">SEARCH</span> :
          <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;search finished&quot;</span><span style="color: #66cc66;">&#41;</span>;
          <span style="color: #0066CC;">try</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;first item : &quot;</span> + <span style="color: #0066CC;">e</span>.<span style="color: #006600;">value</span>.<span style="color: #006600;">items</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">title</span> + <span style="color: #ff0000;">&quot; - &quot;</span> + <span style="color: #0066CC;">e</span>.<span style="color: #006600;">value</span>.<span style="color: #006600;">items</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">link</span><span style="color: #66cc66;">&#41;</span>;
          <span style="color: #66cc66;">&#125;</span> <span style="color: #0066CC;">catch</span> <span style="color: #66cc66;">&#40;</span>evt:ArgumentError<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;ERROR : No Videos For Tag&quot;</span><span style="color: #66cc66;">&#41;</span>;
          <span style="color: #66cc66;">&#125;</span>
          <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;requesting the video id and t-parameter&quot;</span><span style="color: #66cc66;">&#41;</span>;
          yt.<span style="color: #006600;">videoId</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #006600;">value</span>.<span style="color: #006600;">items</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">link</span><span style="color: #66cc66;">&#41;</span>;
          <span style="color: #b1b100;">break</span>;
        <span style="color: #b1b100;">case</span> YouTube.<span style="color: #006600;">VIDEOID</span> :
          <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;loaded: video id = &quot;</span> + <span style="color: #0066CC;">e</span>.<span style="color: #006600;">value</span>.<span style="color: #006600;">id</span> + <span style="color: #ff0000;">&quot; and T-parameter = &quot;</span> + <span style="color: #0066CC;">e</span>.<span style="color: #006600;">value</span>.<span style="color: #006600;">t</span><span style="color: #66cc66;">&#41;</span>;
          <span style="color: #0066CC;">play</span><span style="color: #66cc66;">&#40;</span>YouTube.<span style="color: #006600;">FLVUrl</span> + <span style="color: #0066CC;">e</span>.<span style="color: #006600;">value</span>.<span style="color: #006600;">id</span> + <span style="color: #ff0000;">&quot;&amp;amp;t=&quot;</span> + <span style="color: #0066CC;">e</span>.<span style="color: #006600;">value</span>.<span style="color: #006600;">t</span><span style="color: #66cc66;">&#41;</span>;
          <span style="color: #b1b100;">break</span>;
      <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> onError<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:ErrorEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;IOError : &quot;</span> + <span style="color: #0066CC;">e</span> <span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">play</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;playing: &quot;</span> + <span style="color: #0066CC;">url</span><span style="color: #66cc66;">&#41;</span>;
      stream = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#40;</span>connection<span style="color: #66cc66;">&#41;</span>;
      stream.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>NetStatusEvent.<span style="color: #006600;">NET_STATUS</span>, netStatusHandler<span style="color: #66cc66;">&#41;</span>;
      stream.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>AsyncErrorEvent.<span style="color: #006600;">ASYNC_ERROR</span>, asyncErrorHandler<span style="color: #66cc66;">&#41;</span>;
      <span style="color: #0066CC;">video</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Video</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
      <span style="color: #0066CC;">video</span>.<span style="color: #006600;">attachNetStream</span><span style="color: #66cc66;">&#40;</span>stream<span style="color: #66cc66;">&#41;</span>;
      stream.<span style="color: #0066CC;">play</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span><span style="color: #66cc66;">&#41;</span>;
      addChild<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">video</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> asyncErrorHandler<span style="color: #66cc66;">&#40;</span>event:AsyncErrorEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #808080; font-style: italic;">// ignore AsyncErrorEvent events.</span>
    <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>I&#8217;ve put <a title="a zip file with all files used in this 5 part tutorial" href="http://www.streamhead.com/wp-content/uploads/2008/10/ytposts.zip" target="_blank">all files we used in the tutorials in a handy zip file</a>. Just expand and open in FlashDevelop.</p>
<h2>Conclusion</h2>
<p>In this 5th and final post of the series I have shown how to actually <strong>stream the YouTube FLV</strong>. It was particularly easy because Flash already has everything you need. It&#8217;s a matter of finding the right classes and using them.</p>
<p>That wraps up the 5 part tutorial. Feel free to leave a comment if you found this useful or not. I&#8217;m also open for suggestions for improvements or other subjects for future tutorials.</p>
<p><a title="lazy on Flickr" href="http://flickr.com/photos/50826080@N00/2235601624/" target="_blank">Image credit</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.streamhead.com/lazy-actionscript-developers-stream-flash-video/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-sa/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>How to use images in ActionScript 3 with FlashDevelop (and some other AS3 tips)</title>
		<link>http://www.streamhead.com/how-to-use-images-in-actionscript-3-with-flashdevelop-and-some-other-as3-tips/</link>
		<comments>http://www.streamhead.com/how-to-use-images-in-actionscript-3-with-flashdevelop-and-some-other-as3-tips/#comments</comments>
		<pubDate>Thu, 22 May 2008 10:53:30 +0000</pubDate>
		<dc:creator>Peter Backx</dc:creator>
				<category><![CDATA[FlashMedia]]></category>
		<category><![CDATA[featured]]></category>

		<guid isPermaLink="false">http://www.streamhead.com/?p=98</guid>
		<description><![CDATA[update: I&#8217;ve created a new and updated tutorial on embedding images in FlashDevelop. I suggest you check out that tutorial instead of the one below. The new tutorial is cleaner, clearer and more up-to-date. One of the main bumps you will run into when coding Sandy3D, or basically any kind of graphical applet is loading [...]]]></description>
			<content:encoded><![CDATA[<p class="update">
<strong>update</strong>: I&#8217;ve created <a title="Embedding Bitmap Images in ActionScript 3 with FlashDevelop" href="http://www.streamhead.com/embedding-images/">a new and updated tutorial on embedding images in FlashDevelop</a>. I suggest you check out that tutorial instead of the one below. The new tutorial is cleaner, clearer and more up-to-date.
</p>
<p>One of the main bumps you will run into when <a title="previous tutorial on Sandy3D and FlashDevelop" href="http://www.streamhead.com/?p=74" target="_blank">coding Sandy3D</a>, or basically any kind of graphical applet is loading resources, such as images. If you are working inside Adobe&#8217;s Flash environment, this all comes pretty naturally. But open source tools such as <a title="FlashDevelop - open source Flash development" href="http://www.flashdevelop.org/community/" target="_blank">FlashDevelop</a> do not have a graphical component, so you have to create your multimedia assets in other applications (<a title="Paint.NET - free software for digital photo editing" href="http://www.getpaint.net/" target="_blank">Paint.NET is nice</a>).</p>
<p>Before you get started, have a look at <a title="AS3 MovieClip vs Sprite" href="http://kanuwadhwa.wordpress.com/2007/09/10/76/" target="_blank">this MovieClip vs Sprite comparison</a> and <a title="AS3 getting started tutorial" href="http://www.senocular.com/flash/tutorials/as3withmxmlc/" target="_blank">this tutorial on starting with AS3</a> to understand why our base class extends Sprite and how events are handled. And if you want to know where all this is going, the idea is to recreate <a title="Flash game tutorial" href="http://www.emanueleferonato.com/2008/03/27/create-a-flash-game-like-bloons-tutorial/" target="_blank">something similar to this</a> in ActionScript 3 and FlashDevelop and then afterwards add some 3D goodness to it (3D objects, maybe 3D camera).</p>
<p>One important thing to know about event handlers is: don&#8217;t attach them to your main Sprite. This won&#8217;t work:</p>

<div class="wp_codebox"><table><tr id="p9818"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p98code18"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Test <span style="color: #0066CC;">extends</span> Sprite <span style="color: #66cc66;">&#123;</span>
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Test<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    graphics.<span style="color: #0066CC;">lineStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
    graphics.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0xFF8000<span style="color: #66cc66;">&#41;</span>;
    graphics.<span style="color: #006600;">drawCircle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span>, <span style="color: #cc66cc;">50</span>, <span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span>;
    addEventListener<span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_DOWN</span>, <span style="color: #0066CC;">down</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">down</span><span style="color: #66cc66;">&#40;</span>evt:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;down&quot;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>My intuition was that it would attach mouse down events to the circle. But it does not. In most cases you would want to capture the whole sprite anyway. The solution is to add your listeners to the &#8220;<em>stage</em>&#8221; object. See the the examples below for a demonstration of this.</p>
<p>In Flash, images come in 2 types. Vector drawings or bitmaps. Pretty much all graphics you see on the Internet are bitmaps, every pixel in the bitmap is defined. A program such as <a title="Paint.NET - free software for digital photo editing" href="http://www.getpaint.net/" target="_blank">Paint.NET</a> will create those for you. Vector images are a different beast. Those images define the lines and forms that are used. In the example above, a circle is drawn, not a collection of points. The advantage is that the image quality will be a lot higher and it is possible to zoom in on the Flash applet without loosing quality. But (there always is a but) you can only draw those in the Adobe Flash editor. You can use existing .fla files with vector drawings in them, but you can&#8217;t make your own, unless you pay for the program. Either that or you would have to handcode everything. A future post will probably deal with that.</p>
<p>But this post deals with bitmaps. We will start from the following simple mouse-follow applet and replace the circle with a bitmap:</p>

<div class="wp_codebox"><table><tr id="p9819"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="code" id="p98code19"><pre class="actionscript" style="font-family:monospace;">package  <span style="color: #66cc66;">&#123;</span>
  <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
  <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
  <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">ui</span>.<span style="color: #0066CC;">Mouse</span>;
&nbsp;
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Test <span style="color: #0066CC;">extends</span> Sprite <span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> circle:Sprite = <span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Test<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #0066CC;">Mouse</span>.<span style="color: #0066CC;">hide</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
      circle.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
      circle.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0xFF8000<span style="color: #66cc66;">&#41;</span>;
      circle.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawCircle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span>;
      addChild<span style="color: #66cc66;">&#40;</span>circle<span style="color: #66cc66;">&#41;</span>;
&nbsp;
      <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, mouseMove<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> mouseMove<span style="color: #66cc66;">&#40;</span>evt:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
      circle.<span style="color: #006600;">x</span> = mouseX;
      circle.<span style="color: #006600;">y</span> = mouseY;
    <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>This is the result:</p>
<p><object type="application/x-shockwave-flash" data="http://www.streamhead.com/wp-content/uploads/2008/05/testcircle.swf" width="200" height="200" class="embedflash"><param name="movie" value="http://www.streamhead.com/wp-content/uploads/2008/05/testcircle.swf" /><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><small>(Please open the article to see the flash file or player.)</small></object></p>
<p>I like to store all my resources in a separate directory, called &#8220;<em>library</em>&#8220;. It&#8217;s not a the &#8220;real&#8221; library that you have in the Flash IDE, but it&#8217;s close enough. I&#8217;m using this crosshair bitmap:</p>
<p><a href="http://www.streamhead.com/wp-content/uploads/2008/05/crosshair.png"><img class="alignnone size-thumbnail wp-image-113" title="crosshair" src="http://www.streamhead.com/wp-content/uploads/2008/05/crosshair-150x150.png" alt="" width="78" height="78" /></a></p>
<p>Once we have an image, it&#8217;s time to embed it. ActionScript 3 has an &#8220;Embed&#8221; tag that will allow you to put images in an applet. See <a title="Embedding resources with AS3" href="http://www.bit-101.com/blog/?p=853" target="_blank">this link</a> for the complete explanation. Basically, you define a private Class variable, put the cursor just above it and double click on your image. FlashDevelop will automatically insert the correct &#8220;Embed&#8221; tag in your code. A little tweaking to get the correct size and there you are:</p>

<div class="wp_codebox"><table><tr id="p9820"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code" id="p98code20"><pre class="actionscript" style="font-family:monospace;">package  <span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Bitmap</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">ui</span>.<span style="color: #0066CC;">Mouse</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Test <span style="color: #0066CC;">extends</span> Sprite <span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #66cc66;">&#91;</span>Embed<span style="color: #66cc66;">&#40;</span>source=<span style="color: #ff0000;">'library/crosshair.png'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> crosshairClass:<span style="color: #000000; font-weight: bold;">Class</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> crosshair:Bitmap = <span style="color: #000000; font-weight: bold;">new</span> crosshairClass <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Test<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">Mouse</span>.<span style="color: #0066CC;">hide</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			crosshair.<span style="color: #0066CC;">height</span> = <span style="color: #cc66cc;">30</span>;
			crosshair.<span style="color: #0066CC;">width</span> = <span style="color: #cc66cc;">30</span>;
			addChild<span style="color: #66cc66;">&#40;</span>crosshair<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #0066CC;">stage</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, mouseMove<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> mouseMove<span style="color: #66cc66;">&#40;</span>evt:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
			crosshair.<span style="color: #006600;">x</span> = mouseX - <span style="color: #cc66cc;">15</span>;
			crosshair.<span style="color: #006600;">y</span> = mouseY - <span style="color: #cc66cc;">15</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p><object type="application/x-shockwave-flash" data="http://www.streamhead.com/wp-content/uploads/2008/05/testcross.swf" width="200" height="200" class="embedflash"><param name="movie" value="http://www.streamhead.com/wp-content/uploads/2008/05/testcross.swf" /><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><small>(Please open the article to see the flash file or player.)</small></object></p>
<p>As you will notice, the quality of the scaled down image is not that good, but using a larger sprite allows you to zoom in with little quality loss. Next time, I&#8217;ll show you how to fix this.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.streamhead.com/how-to-use-images-in-actionscript-3-with-flashdevelop-and-some-other-as3-tips/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-sa/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>Tutorial: jMonkeyEngine applet &#8211; hardware 3D in the browser</title>
		<link>http://www.streamhead.com/tutorial-jmonkeyengine-applet-hardware-3d-in-the-browser/</link>
		<comments>http://www.streamhead.com/tutorial-jmonkeyengine-applet-hardware-3d-in-the-browser/#comments</comments>
		<pubDate>Sun, 27 Apr 2008 21:57:30 +0000</pubDate>
		<dc:creator>Peter Backx</dc:creator>
				<category><![CDATA[JavaMedia]]></category>
		<category><![CDATA[featured]]></category>
		<category><![CDATA[recommended]]></category>

		<guid isPermaLink="false">http://www.streamhead.com/?p=91</guid>
		<description><![CDATA[Before I continue any Sandy3D or Flash tutorials, I&#8217;d like to go into another method of getting multimedia content in the browser: Java applets. One major advantage of applets is the possibility to use hardware acceleration inside the browser. This will tremendously increase the capabilities, quality and speed of the displayed pictures. None of the [...]]]></description>
			<content:encoded><![CDATA[<img width="339" height="305" src="http://www.streamhead.com/wp-content/uploads/2008/04/jmecube.png" class="attachment-post-thumbnail wp-post-image" alt="it&#039;s a cube" title="jME cube" /><p><a href="http://www.streamhead.com/jme/" target="_blank"><img class="aligncenter size-full wp-image-92" title="jME cube" src="http://www.streamhead.com/wp-content/uploads/2008/04/jmecube.png" alt="it's a cube" width="339" height="305" /></a></p>
<p>Before I continue any Sandy3D or Flash tutorials, I&#8217;d like to go into another method of getting multimedia content in the browser: <strong>Java applets</strong>. One major advantage of applets is the possibility to use hardware acceleration inside the browser. This will tremendously increase the capabilities, quality and speed of the displayed pictures. None of the Flash 3D engines even come close.</p>
<p>As always, there is a drawback. Access to hardware requires more security privileges and is operating system dependant. Java solves a little, but at some point you need to go to the hardware. Enter <a href="http://www.lwjgl.org/" target="_blank">Lightweight Gaming Library (LWJGL)</a>. It allows crossplatform access to the OpenGL library. Combine this with some of the Java security features and you&#8217;ve got a winner.</p>
<p>If you click on the image above, a new window should open that loads the applet. After everything&#8217;s done loading, you should get a security warning. I haven&#8217;t paid for a certificate, so you will have to trust me that the applet won&#8217;t mess with your system. Let me know if you see any errors. If you get a red cross, right click and open the Java console. I&#8217;m very interested in seeing how well everything performs across different browsers and OS&#8217;s.</p>
<p>OpenGL is still pretty lowlevel so to make things a little easier, I&#8217;ve added <a href="http://www.jmonkeyengine.com/" target="_blank">jMonkeyEngine</a> into the mix. At the core jME is a scenegraph based 3D engine, but it adds many more libraries that are useful for 3D and game development, such as the physics library.</p>
<p>jME already has <a href="http://www.jmonkeyengine.com/wiki/doku.php?id=writing_a_jme_applet" target="_blank">a nice tutorial on the WIKI</a>, however there are a few things mising. If you want to get the tutorial running, you can just follow along with the example right until you&#8217;ve build the example and packaged it into a jar file (using a IDE like <a href="http://www.netbeans.org" target="_blank">NetBeans</a> or <a href="http://www.eclipse.org/" target="_blank">Eclipse</a> will make this very easy). Now lets assume you have the jar file with your applet class. Copy it into your &#8216;release&#8217; directory. This is the directory you will upload to your site. To get it running, you need a few libraries (most of these you probably already have downloaded during the development of the applet):</p>
<ul>
<li><a href="https://sourceforge.net/project/showfiles.php?group_id=58488" target="_blank">LWJGL applet libraries</a>: From that link, download from release 1.1.4, the file <strong>lwjgl_applet-1.1.4.zip</strong>. Unzip it and copy <strong>lwjgl.jar</strong>, <strong>lwjg_applet_util.jar</strong> and <strong>natives.jar</strong> into your release directory.</li>
<li><a href="https://jme.dev.java.net/servlets/ProjectDocumentList?folderID=418&amp;expandFolder=418&amp;folderID=0" target="_blank">JME libraries</a>: Download and unzip version 1.0 and copy <strong>jme.jar</strong> and <strong>jme-awt.jar</strong> in the directory.</li>
<li><a href="https://jme.dev.java.net/servlets/ProjectDocumentList?folderID=148&amp;expandFolder=148&amp;folderID=418" target="_blank">JME test libraries</a> (give you easy access to a few images and objects): Again get version 1.0 and copy <strong>jmetest.jar</strong> and <strong>jmetest-data-images.jar</strong> into the directory.</li>
</ul>
<p>Because jME needs to access the texture file, the applet needs to be signed. LWJGL is alrady signed, so no problem, however, the jar file you created yourself also needs to be signed. Open a command prompt window:</p>
<ul>
<li>If you don&#8217;t have a certificate, create one: <strong> keytool -genkey -alias key_name</strong></li>
<li>Change to the path where the jar file is located</li>
<li>Sign the file: <strong>jarsigner my_jar.jar key_name</strong></li>
</ul>
<p>Now you can go ahead and create the <strong>index.html</strong>. You can take the one from the tutorial, however, you need to add a few libraries to the &#8220;archives&#8221; parameter. Make sure all jar files mentioned above are referenced:</p>
<p><strong>archive=&#8221;lwjgl_util_applet.jar,lwjgl.jar,natives.jar,jme.jar,jme-awt.jar,jmetest.jar,jmetest-data-images.jar,my_jar.jar&#8221;</strong></p>
<p>And that should do it. Load the index page in your browser to see the result. Here&#8217;s a screenshot of what you should see:</p>
<p><a href="http://www.streamhead.com/wp-content/uploads/2008/04/jmerunning.png"><img class="alignnone size-thumbnail wp-image-93" title="jME in a browser" src="http://www.streamhead.com/wp-content/uploads/2008/04/jmerunning-150x150.png" alt="" width="150" height="150" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.streamhead.com/tutorial-jmonkeyengine-applet-hardware-3d-in-the-browser/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-sa/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>Tutorial &#8211; getting started with Sandy 3D and FlashDevelop</title>
		<link>http://www.streamhead.com/tutorial-getting-started-with-sandy-3d-and-flashdevelop/</link>
		<comments>http://www.streamhead.com/tutorial-getting-started-with-sandy-3d-and-flashdevelop/#comments</comments>
		<pubDate>Tue, 08 Apr 2008 00:27:46 +0000</pubDate>
		<dc:creator>Peter Backx</dc:creator>
				<category><![CDATA[FlashMedia]]></category>
		<category><![CDATA[featured]]></category>
		<category><![CDATA[recommended]]></category>

		<guid isPermaLink="false">http://www.streamhead.com/?p=74</guid>
		<description><![CDATA[(Please open the article to see the flash file or player.) 3D for the web, it has been coming to you ever since virtual reality was the mot-du-jour and VRML the format to support, but it&#8217;s still not there yet. The latest craze now are Flash 3D engines. There are three worth mentioning: Papervision3D, Away3D [...]]]></description>
			<content:encoded><![CDATA[<p><object type="application/x-shockwave-flash" data="http://www.streamhead.com/wp-content/uploads/2008/04/sandystream.swf" width="450" height="250" class="embedflash"><param name="movie" value="http://www.streamhead.com/wp-content/uploads/2008/04/sandystream.swf" /><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><small>(Please open the article to see the flash file or player.)</small></object></p>
<p>3D for the web, it has been coming to you ever since virtual reality was the mot-du-jour and <a href="http://en.wikipedia.org/wiki/VRML" target="_blank">VRML</a> the format to support, but it&#8217;s still not there yet. The latest craze now are Flash 3D engines. There are three worth mentioning: <a href="http://papervision3d.org/">Papervision3D</a>, <a href="http://www.away3d.com/">Away3D</a> and, the one I&#8217;ll be talking about here, <a href="http://www.flashsandy.org/">Sandy3D</a>.</p>
<p>Flash development (or more specificically Action Script) is all fine and dandy, but it does require an investment in some software products. If you&#8217;d go the route of least resistance, you need <a href="http://www.adobe.com/products/creativesuite/">Adobe Creative Suite 3</a> ($999 for the web standard edition) and some 3D software, <a href="http://usa.autodesk.com/adsk/servlet/index?id=5659302&amp;siteID=123112">Autodesk 3ds Max</a> ($3495 for 2009 edition) is the most widely supported. I don&#8217;t know about you, but as a hobbiest that leaves me only 2 options. Pirate the software, or go look in the free and open source community.</p>
<p>Luckely there are a few options for creating Flash and 3D. This post is about the Flash part. <a href="http://osflash.org/flashdevelop">FlashDevelop</a> is an open source development environment for Flash Actionscript and integrates seamlessly with the Flex SDK (free from Adobe). As you will see in this post, it is missing the designer features that the CS3 tools have (vector graphics, timeline editing), but as far as Actionscript editing goes, it&#8217;s on par with anything out there and better than most.</p>
<p>As an introduction, here is a short tutorial on how to get the <a href="http://www.flashsandy.org/tutorials/3.0/video_getting_started">Sandy3D getting started video</a> example working on FlashDevelop.</p>
<p><strong><em>First set up your environment</em></strong></p>
<ol>
<li>If you want to use the latest release of Sandy3D, as we will do in this tutorial, get and install <a href="http://tortoisesvn.net/">TortoiseSVN</a>.</li>
<li>Get FlashDevelop (<a href="http://www.flashdevelop.org/community/viewtopic.php?t=2574">currently 3.0.0 beta 6</a>) and the <a href="http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+3">Adobe Flex SDK</a>. Install both.</li>
<li>Start up FlashDevelop and open <strong>Tools &gt; Program Settings&#8230;</strong> Click on <strong>AS3Context</strong> on the left and fill in the <strong>Flex SDK location</strong>:</li>
</ol>
<p><img src="http://www.streamhead.com/wp-content/uploads/2008/04/fdsetup.png" alt="flash develop setup" /></p>
<p><em><strong>Lets get started</strong></em></p>
<ol>
<li>Watch the <a href="http://www.flashsandy.org/tutorials/3.0/video_getting_started">getting started video</a> and follow along.</li>
<li>Create a project directory, eg &#8220;New Sandy Project&#8221;.</li>
<li>Inside it, create a &#8220;<strong>sandy</strong>&#8221; directory and use TortoiseSVN to check out the latest Sandy3D release, as shown in the video. Use <a href="http://sandy.googlecode.com/svn/trunk/sandy/as3/branches/3.0.2/src/sandy">the current release URL here</a> at your own risk (best to follow the video and find the latest release yourself)</li>
<li>In your project folder, create a <strong>Demo1.as</strong> file.</li>
<li>No need to create the <span style="text-decoration: line-through;">Demo1.fla</span> file as this is not used by FlashDevelop.</li>
<li>Unlike with CS3, you will need to manage your resource library (images, etc) by hand. So create a &#8220;<strong>library</strong>&#8221; directory inside your project folder.</li>
<li>Search two images on the Net and place them in the library directory. One will be used for the background, one will be used as a texture for the plane object we are going to draw.</li>
<li>Open FlashDevelop and start a new Project: <strong>Project &gt; New Project..</strong>.</li>
<li>Choose an empty ActionScript3 project. Enter any project name you like. In the location field browse to the project directory you created. Leave the package name empty and click OK.</li>
<li>Select an output file name in <strong>Project &gt; Properties&#8230;</strong>. For instance &#8220;New Sandy Project.swf&#8221;.</li>
<li>In the Project view, open the library directory and right click on both images and select &#8220;<strong>Add to library</strong>&#8220;.</li>
<li>Again in the Project view, right click on Demo1.as and select &#8220;<strong>Always compile</strong>&#8220;. Double click on the file to open it. You should have a view similar to this one:</li>
</ol>
<p><img src="http://www.streamhead.com/wp-content/uploads/2008/04/fdreadytogo.png" alt="fdreadytogo.png" /></p>
<p><em><strong>Enter the code</strong></em></p>
<p>This one is the bulk of the work. In the Demo1.as view, type the code for the example:</p>
<pre>package
{
	import flash.display.Sprite;
	import sandy.core.scenegraph.Camera3D;
	import sandy.core.scenegraph.Group;
	import sandy.core.scenegraph.Shape3D;
	import sandy.core.World3D;
	import sandy.materials.Appearance;
	import sandy.materials.WireFrameMaterial;
	import sandy.primitive.Plane3D;

	public class Demo1 extends Sprite
	{
		private var world:World3D;

		public function Demo1()
		{
			world = World3D.getInstance ();
			world.container = this;

			world.camera = new Camera3D (this.width, this.height);

			var scene:Group = new Group ('scene');
			var shape:Shape3D = new Plane3D ('plane', 100, 100, 10, 10);
			shape.appearance = new Appearance (new WireFrameMaterial ());

			scene.addChild (shape);
			scene.addChild (world.camera);
			world.root = scene;
		}
	}
}</pre>
<p>Hit ctrl-enter or select the little play button (test movie). Everything should compile and run fine, but you will see an empty movie.  FlashDevelop automatically imports classes for you, so if you typed in everything by hand, like in the video, you could skip the step where you locate the missing import statements.</p>
<p>There are two reasons the code is not displaying anything:</p>
<p><strong><em>1.</em></strong> The &#8220;this&#8221; object does not have a width and height. In the demo video a vector image is attached as background. I don&#8217;t think this is possible with FlashDevelop only (let me know if it is), so my option is to get a bitmap from the net &#8220;gradient.jpg&#8221; in this case and use that one. We can do this by embedding an image. Just before the &#8220;private var world:World3D;&#8221; line, add this:</p>
<pre>[Embed(source = 'library/gradient.jpg')]
private var gradient:Class;</pre>
<p>And now add this to the main sprite class. Just before &#8220;world =  &#8230;&#8221; add</p>
<pre>this.addChild (new gradient ());</pre>
<p>Run again and you should already see your background.</p>
<p><em><strong>2.</strong></em> The second issue is that the camera is not pointed in the right direction. Add the code as instructed in the video. Your entire program should now be:</p>
<pre>package
{
	import flash.display.Sprite;
	import sandy.core.scenegraph.Camera3D;
	import sandy.core.scenegraph.Group;
	import sandy.core.scenegraph.Shape3D;
	import sandy.core.World3D;
	import sandy.materials.Appearance;
	import sandy.materials.WireFrameMaterial;
	import sandy.primitive.Plane3D;

	public class Demo1 extends Sprite
	{
		[Embed(source = 'library/gradient.jpg')]
		private var gradient:Class;

		private var world:World3D;

		public function Demo1()
		{
			this.addChild (new gradient ());

			world = World3D.getInstance ();
			world.container = this;

			world.camera = new Camera3D (this.width, this.height);

			var scene:Group = new Group ('scene');
			var shape:Shape3D = new Plane3D ('plane', 100, 100, 10, 10);
			shape.appearance = new Appearance (new WireFrameMaterial ());

			scene.addChild (shape);
			scene.addChild (world.camera);
			world.root = scene;

			world.camera.setPosition ( -100, -100, -100);
			world.camera.lookAt (0, 0, 0);
			world.render ();
		}
	}
}</pre>
<p>Run the movie again and the plane should be visible.</p>
<p>There&#8217;s a little more in the video than that, but that&#8217;s for another post. At least you should already be well on your way to running all the other Sandy3D tutorials.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.streamhead.com/tutorial-getting-started-with-sandy-3d-and-flashdevelop/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-sa/3.0/</creativeCommons:license>
	</item>
	</channel>
</rss>
