import classes? is it nesecarry? - actionscript-3

why import classes if they work even if you don't?
import flash.display.Sprite;
import flash.events.MouseEvent;
var circle1:Sprite = new Sprite();
circle1.graphics.beginFill(0xFFCC00);
circle1.graphics.drawCircle(40, 40, 40);
circle1.buttonMode = true;
circle1.addEventListener(MouseEvent.CLICK, clicked);
why are we ment to import classes if the code is working anyways? if the two import lines are escaped the code will still be working

If the code is compiled as strict, it needs those imports. Unless the instances are decalred as fully qualified class names.
e.g.
var circle1:flash.display.Sprite = new flash.display.Sprite();
Which I'm sure you don't want to do ;)
I'd always recommend compiling in strict mode anyway, and making sure that your code is properly typed and has the required references (imports).

Related

Using Actionscript 3.0 to play audio from dropbox

Just looking for some advice and hoping someone can point me in the right direction.
I'm trying to create a Flash MP3 player that can play music stored on DropBox. Never used ActionScript before so kind of learning as I go...
From what I've read so far, I need to use URLRequest to get the link to the mp3 file, then use the Sound object to load the file and play it. However, when I test my SWF file I don't get audio. I've also been reading about URLSream and NETstream, but not sure how to use that with the Sound object. I'm not 100% sure if I'm using Sound correctly, and given that this is pretty old technology now, I'm struggling to find decent tutorials.
Here's the code I've been trying:
package {
//Dependencies
import flash.display.MovieClip;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
import flash.net.URLStream;
import flash.display.*;
import flash.events.*;
import flash.net.*;
import flash.media.*;
//Class code
public class soundObject extends MovieClip {
//Variables
var mySong:Sound = new Sound();
//Init Code here
public function soundObject() {
var myRequest:URLRequest = new URLRequest("https://www.dropbox.com/s/[dropboxurl]/T-U-R-T-L-E%20POWER.mp3?dl=0")
mySong.load(myRequest);
mySong.addEventListener(Event.COMPLETE, onSoundLoad);
}
private function onSoundLoad(e:Event): void
{
mySong.play();
}
}
}
So I figured it out guys... and surprise, surprise, it was something rather simple.
In publish settings, I had it set to 'local only'. When I changed this 'network only' the link started to magically work.
Thanks for the help. Got there in the end :D
EDIT: And just in case, I did also have change ?dl=0 to ?dl=1

GeoTools: How to build a point? (imports issue)

I'm following the GeoTools documentation and found this:
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);
Coordinate coord = new Coordinate(45, 15);
Point point = geometryFactory.createPoint(coord);
When I put it in intellij IDE, for each class there are several suggested imports to use. What import I need to select?
Alternative way (with same issue) is:
GeometryBuilder builder = new GeometryBuilder(DefaultGeographicCRS.WGS84);
Point point = builder.createPoint(45, 15);
When in doubt you can always read the documentation, for example JTSFactoryFinder returns a com.vividsolutions.jts.geom.GeometryFactory, once you know that the other pieces fall into place as:
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;
Meanwhile your GeometryBuilder is an org.geotools.geometry.GeometryBuilder which leads to the following imports:
import org.geotools.geometry.GeometryBuilder;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.opengis.geometry.primitive.Point;

How do I allow user to select tint amount in Flash Professional using ActionScript 3?

So I was in the middle of making a type of test program where I would take parts of a shirt and the user could customize it with different colors.
While I was doing that I realized it wasn't realistic as there was absolutely no fade with the color picker tool (code shown below).
Is there any way to allow the user to use possibly a slider to change the level of tint on the color(s) being used on the shirt?
import fl.controls.ColorPicker;
import fl.events.ColorPickerEvent;
import flash.geom.ColorTransform;
var mycolor:ColorTransform = new ColorTransform();
cp.addEventListener(ColorPickerEvent.CHANGE,colorChanger);
function colorChanger(event:ColorPickerEvent):void{
mycolor.color = cp.selectedColor;
mc2.transform.colorTransform = mycolor;
}
import fl.events.SliderEvent;
import fl.controls.Slider;
var slider:Slider=new Slider();
slider.maximum=100;
slider.value=100;
addChild(slider);
slider.addEventListener(SliderEvent.THUMB_DRAG,changeAlpha);
function changeAlpha(event:SliderEvent):void{
mycolor.alphaMultiplier=slider.value/100;
mc2.transform.colorTransform = mycolor;
}
Check this: flashandmath.com/howtos/tint
Very useful site, not only for tint! It's a big shame the authors don't really update it anymore. (but they still love flash, which is cool :o) )
You only need to use mc2.alpha = slider.value/100 to change it.

Scrollbar not working with TLF text with dynamic content in ActionScript 3

I'm creating a simple Flash project with Flash CS5 and ActionScript 3.
What I want to do is that I want to dynamically update a TLF text container with given source and destination, something like loadData(text_placeX, "markup.xml"); anywhere that I want.
It's working like a charm, but the problem is I can't use any scroll-bar for my text. I have added a UIScrollBar to text container and it's working with the default text that I've putted into text container, but when I update container with my data it's not working. What am I missing?
Another question is that how I can empty my text container before loading new data in it?
My code is:
import fl.text.TLFTextField;
import flash.display.Loader;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.text.TextFieldAutoSize;
import flashx.textLayout.container.ContainerController;
import flashx.textLayout.elements.TextFlow;
import flashx.textLayout.conversion.TextConverter;
import fl.controls.ScrollBar;
var ldr:URLLoader = new URLLoader();
var flow:TextFlow = new TextFlow();
function loadData(text_place, fileURL:String):void {
text_place.border = true;
ldr.dataFormat = URLLoaderDataFormat.TEXT;
ldr.addEventListener(Event.COMPLETE, function(evt:Event){ ldr_complete(text_place) }, false, 0, true);
ldr.load(new URLRequest(fileURL));
ldr.addEventListener(IOErrorEvent.IO_ERROR, loadError);
}
function ldr_complete(text_place:TLFTextField):void {
ldr.removeEventListener(Event.COMPLETE, ldr_complete);
ldr.removeEventListener(IOErrorEvent.IO_ERROR, loadError);
initText(text_place, ldr.data);
}
function loadError(e:IOErrorEvent):void {
trace("Error loading an external file. The server may be busy. Try refreshing the page.");
}
function initText(text_place:TLFTextField, fileContent):void {
flow = TextConverter.importToFlow(fileContent, TextConverter.TEXT_FIELD_HTML_FORMAT);
flow.flowComposer.addController(new ContainerController(text_place, text_place.width, text_place.height));
flow.flowComposer.updateAllControllers();
}
UPDATE: When I skip using of initText function contents and instead I use text_place.tlfMarkup = fileContent; it works; but my option on TextFlow is missing. And also I was missing "update scrollbar" after putting content in text-field.
I think this line may be the problem:
ldr.addEventListener(Event.COMPLETE, function(evt:Event){ ldr_complete(text_place) }, false, 0, true);
You have an anonymous function here (function(evt:Event){...) which passes the object text_place to the function ldr_complete(). However, you do not have access to text_place, since it is a variable declared within a different scope. If you make the function into a named one, you will not assume that you have that access. E.g.,
function loadCompleteHnd(evt:Event):void{
[...]
}
However, you still have to gain access to the object in text_place. So, you can make text_place a class-level (global) variable, and set that variable whenever. But that may run the risk of creating a race condition -- if you have a slow load, you might be trying change that object from two places at once.
Another option is to create an entirely new Event which extends the Event.COMPLETE setup. At that point, you can create another parameter for the Event.COMPLETE listener to use. This is complicated, and a bit of a learning curve, but it makes events more versatile.
In either case, you may want to set up a flag that tells you whether something else is editing the same object. It's not foolproof, but it can save some headaches.

How to import FXG to a Shape?

I can import FXG files as Sprites as follows:
import graphics.mypic; // graphics/mypic.fxg
var mysprite:Sprite = new mypic();
I don't always need the fancy things that come with Sprites. How can I import them to Shapes?
No, you can't cast them as Shape(s) - the internal compile-time FGX is built on top of Sprite. You can find that out by running
var tig:* = new tiger();
if (tig instanceof Sprite)
...
What George Profenza is referring to is runtime loading of FXG's
I don't know if there's a better way to do this know, but last year I've played with a nice FXGParser library that simplified things a lot.
Get the library:
svn export http://www.libspark.org/svn/as3/FxgParser/
Use it:
import fxgparser.FxgDisplay;
import graphics.mypic;
var fxg:XML = new XML(mypic);//this bit depends on how you load/embed the fxg xml
var mysprite: FxgDisplay= new FxgDisplay( fxg );
addChild( mysprite );
Goodluck!