Is it possible to add the (.as) actionscript file dynamically in flex? - actionscript-3

Am developing application in flex which is loaded the action script file .
<DrawingArea id="drawingArea" xmlns="*" width="100%" height="100%" add="drawingArea_addHandler(event)"/>
i need to add it dynamically,how to do this ?guide me
update
This is my Drawing area how to create var da:DrawingArea=new DrawingArea
how to access the listener function?
public function DrawingArea()
{
super();
addEventListener(FlexEvent.CREATION_COMPLETE, function(event:FlexEvent):void {
erase();
});
addEventListener(MouseEvent.MOUSE_DOWN, function(event:MouseEvent):void {
x1 = mouseX;
y1 = mouseY;
isDrawing = true;
});
addEventListener(MouseEvent.MOUSE_MOVE, function(event:MouseEvent):void {
if (!event.buttonDown)
{
isDrawing = false;
}
x2 = mouseX;
y2 = mouseY;
if (isDrawing)
{
graphics.lineStyle(2, drawColor);
graphics.moveTo(x1, y1);
graphics.lineTo(x2, y2);
x1 = x2;
y1 = y2;
}
});
addEventListener(MouseEvent.MOUSE_UP, function(event:MouseEvent):void {
isDrawing = false;
});
}

I have made changes to your code.
package
{
import flash.events.MouseEvent;
import mx.core.UIComponent;
public class DrawingArea
{
private var _target:UIComponent;
private var _x1:Number;
private var _y1:Number;
private var _x2:Number;
private var _y2:Number;
[Bindable]
private var _isDrawing:Boolean = false;
private var _drawColor:uint = 0xFFFF00
public function DrawingArea(target:UIComponent)
{
//TODO: implement function
_target = target
_target.addEventListener(MouseEvent.MOUSE_DOWN, downEvent, false, 0, true);
_target.addEventListener(MouseEvent.MOUSE_UP, upEvent, false, 0, true);
_target.addEventListener(MouseEvent.MOUSE_OUT, upEvent, false, 0, true);
}
private function downEvent(event:MouseEvent):void
{
_x1 = event.localX;
_y1 = event.localY;
_isDrawing = true;
_target.addEventListener(MouseEvent.MOUSE_MOVE, moveEvent, false, 0, true);
}
private function moveEvent(event:MouseEvent):void
{
if (!event.buttonDown)
{
_isDrawing = false;
}
_x2 = event.localX;
_y2 = event.localY;
if (_isDrawing)
{
_target.graphics.lineStyle(2, _drawColor);
_target.graphics.moveTo(_x1, _y1);
_target.graphics.lineTo(_x2, _y2);
_x1 = _x2;
_y1 = _y2;
}
}
private function upEvent(event:MouseEvent):void
{
_isDrawing = false;
_target.removeEventListener(MouseEvent.MOUSE_MOVE, moveEvent);
}
public function set drawColor(value:uint): void{
_drawColor = value;
}
}
}
Created a class DrawingAreaView.mxml in which, created object for DrawingArea and passed the class itself as container.
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns="components.*" xmlns:mx="http://www.adobe.com/2006/mxml"
creationComplete="onInit();">
<mx:Script>
<![CDATA[
import components.DrawingArea;
private var drawingArea:DrawingArea;
private function onInit():void {
drawingArea = new DrawingArea(this);
}
]]>
</mx:Script>
</mx:Canvas>
Use above class in Application.
<DrawingAreaView id="drawingArea" width="100%" height="100%"/>
or
private var _drawingAreaView:DrawingAreaView = new DrawingAreaView();
parentObject.addChild(_drawingAreaView);

You will not be able to load a file in memory and then execute it, if that is what you want, in Acionscript. However, what you can do is create a DrawingArea (or any other element) at run-time based on some event (such as user click or a specific timeout period).

In this case your functions are defined inline. Till you dont give them names it will be impossible anyway.
i would suggest
addEventListener(MouseEvent.MOUSE_DOWN, mouseDownFunction);
public function mouseDownFunction(event:MouseEvent = null ):void {
x1 = mouseX;
y1 = mouseY;
isDrawing = true;
}
Then they should become accesable via the object. DrawingArea.mouseDownFunction()

Related

Flex Mobile - Custom Spark List ItemRenderer

I am looking for some guidance to create a custom Spark List ItemRenderer for an Flex Mobile application I am developing.
Overview: Section List where each item has a checkbox control, Label, button control 1 - opens an accordion list below the item, button control 2 - opens Camera UI.
What I am struggling with is creating an itemrenderer that allows the accoridion list to be visible and be populated.
Update: Here's my existing code
<fx:Metadata>
[Event(name="checkBoxIconItemRendererChanged", type="flash.events.Event")]
[Event(name="cameraIconItemRendererChanged", type="flash.events.Event")]
</fx:Metadata>
<fx:Script>
<![CDATA[
import spark.components.Label;
import spark.components.CheckBox;
import spark.components.HGroup;
import spark.components.Image;
import spark.layouts.HorizontalAlign;
import flashx.textLayout.formats.VerticalAlign;
//camera stuff
public var cameraIcon:Image;
public var friendsIcon:Image;
//checkbox stuff start
public var checkBox:CheckBox;
private var checkBoxChanged:Boolean;
private var _checkBoxField:String;
private var _checkBoxFunction:Function;
//list item group
public var listGroup:HGroup;
private var dataSource:IDataInput;
public function get checkBoxFunction():Function{
return _checkBoxFunction;
}
public function get checkBoxField():String{
return _checkBoxField;
}
public function set checkBoxFunction(value:Function):void{
if(_checkBoxFunction==value){
return;
}
_checkBoxFunction=value;
checkBoxChanged=true;
invalidateProperties();
}
public function set checkBoxField(value:String):void{
if(_checkBoxField==value){
return;
}
checkBoxChanged=true;
_checkBoxField=value;
invalidateProperties();
}
/*override public function set data(value:Object):void
{
checkBoxChanged=true;
super.data = value; //->invalidateProperties();
}*/
override protected function createChildren():void
{
super.createChildren();
listGroup = new HGroup();
var testLabel:Label = new Label();
testLabel.text = "Test Item";
listGroup.addElement(testLabel);
//listGroup.addChild(testLabel);
listGroup.visible = false;
listGroup.includeInLayout = false;
addChild(listGroup);
checkBox = new CheckBox();
//checkBox.skin = skins.ChallengeCheckBox; //throws error in the Skin
checkBox.width=64;//32
checkBox.height=64;//32
checkBox.scaleY=1;//.5
checkBox.scaleX=1;//.5
addChild(checkBox);
//listGroup.addChild(checkBox);
checkBox.addEventListener(MouseEvent.CLICK, function(event:MouseEvent):void{
dispatchEvent(new Event("checkBoxIconItemRendererChanged"));
});
friendsIcon = new Image();
friendsIcon.source = "assets/controls/eye_lightgray.png";
friendsIcon.verticalAlign = VerticalAlign.MIDDLE;
//cameraIcon.horizontalAlign = HorizontalAlign.RIGHT;
friendsIcon.width = 85;
friendsIcon.height = 85;
//cameraIcon.x = 275;
friendsIcon.x = Capabilities.screenResolutionX - 205;
friendsIcon.buttonMode = true;
friendsIcon.addEventListener(MouseEvent.CLICK,showFriends);
addChild(friendsIcon);
//listGroup.addChild(friendsIcon);
cameraIcon = new Image();
cameraIcon.source = "assets/controls/uncheckedbox.png";
friendsIcon.verticalAlign = VerticalAlign.MIDDLE;
//cameraIcon.horizontalAlign = HorizontalAlign.RIGHT;
cameraIcon.width = 85;
cameraIcon.height = 85;
//cameraIcon.x = 275;
cameraIcon.x = Capabilities.screenResolutionX - 105;
cameraIcon.buttonMode = true;
cameraIcon.addEventListener(MouseEvent.CLICK,launchCameraUI);
addChild(cameraIcon);
}
override protected function measure():void
{
super.measure();
measuredWidth+=getStyle("horizontalGap")+checkBox.width*checkBox.scaleY;
measuredHeight=Math.max(measuredHeight, checkBox.height*checkBox.scaleY);
}
override protected function layoutContents(unscaledWidth:Number, unscaledHeight:Number):void
{
var paddingLeft:Number = getStyle("paddingLeft");
var paddingRight:Number = getStyle("paddingRight");
var paddingTop:Number = getStyle("paddingTop");
var paddingBottom:Number = getStyle("paddingBottom");
var horizontalGap:Number = getStyle("horizontalGap");
var verticalAlign:String = getStyle("verticalAlign");
setStyle("paddingLeft",paddingLeft+checkBox.width*checkBox.scaleX+horizontalGap);
super.layoutContents(unscaledWidth, unscaledHeight);
setStyle("paddingLeft",paddingLeft);
var vAlign:Number;
if (verticalAlign == "top")
vAlign = 0;
else if (verticalAlign == "bottom")
vAlign = 1;
else // if (verticalAlign == "middle")
vAlign = 0.5;
var viewHeight:Number = unscaledHeight - paddingTop - paddingBottom;
var checkBoxDisplayY:Number = Math.round(vAlign * (viewHeight - checkBox.height*checkBox.scaleY)) + paddingTop;
checkBox.x=paddingLeft;
checkBox.y=checkBoxDisplayY;
}
override protected function commitProperties():void
{
super.commitProperties();
if(checkBoxChanged){
checkBoxChanged=false;
if (checkBoxFunction != null)
{
checkBox.selected=checkBoxFunction(data);
}
else if (checkBoxField)
{
try
{
if (checkBoxField in data && data[checkBoxField] != null)
checkBox.selected=data[checkBoxField];
}
catch(e:Error)
{
trace(e.message);
}
}
}
}
//end
private var _backgroundSection:Number = 0xDDDDDD; //this is a grey
public function set backgroundSection(value:Number):void {
_backgroundSection = value;
}
private var _normalLabelField:String = "kick";
public function get normalLabelField():String {
return _normalLabelField;
}
public function set normalLabelField(value:String):void {
_normalLabelField = value;
}
private var _sectionField:String = "points";
public function get sectionField():String {
return _sectionField;
}
public function set sectionField(value:String):void {
if (value == _sectionField)
return;
_sectionField = value;
invalidateProperties();
}
/**
* Change the style based on the data: section item or regular item
*/
override public function set data(value:Object):void {
//checkbox
checkBoxChanged=true;
if (value[_sectionField]) {
labelField = _sectionField;
labelDisplay.setStyle("textAlign", "center");
labelDisplay.setStyle("fontWeight", "bold");
} else {
labelField = _normalLabelField;
labelDisplay.setStyle("textAlign", "left");
labelDisplay.setStyle("fontWeight", "normal");
//labelDisplay.width = 300;
//labelDisplay.wordWrap = true;
//labelDisplay.multiline = true;
}
super.data = value;
}
override protected function drawBackground(unscaledWidth:Number, unscaledHeight:Number):void {
super.drawBackground(unscaledWidth, unscaledHeight);
//change the background if we render for a section title item
if (data[_sectionField]) {
graphics.beginFill(_backgroundSection, 1);
graphics.lineStyle();
graphics.drawRect(0, 0, unscaledWidth, unscaledHeight);
graphics.endFill();
//adding .parent to each, adding listGroup as parent reference
/*if (checkBox.parent.parent != null)
listGroup.removeChild(checkBox);
if (friendsIcon.parent.parent != null)
listGroup.removeChild(friendsIcon);*/
if (checkBox.parent != null)
removeChild(checkBox);
if (friendsIcon.parent != null)
removeChild(friendsIcon);
if (cameraIcon.parent != null)
removeChild(cameraIcon);
}
}
protected function launchCameraUI(event:MouseEvent):void
{
var cUI:CameraRoll = new CameraRoll();
if( CameraRoll.supportsBrowseForImage )
{
cUI.addEventListener( MediaEvent.SELECT, imageSelected );
cUI.addEventListener( Event.CANCEL, browseCanceled );
cUI.addEventListener( ErrorEvent.ERROR, mediaError );
cUI.browseForImage();
}
else
{
trace( "Image browsing is not supported on this device.");
}
}
protected function imageSelected(event:MediaEvent):void
{
trace( "Media selected..." );
var imagePromise:MediaPromise = event.data;
dataSource = imagePromise.open();
if( imagePromise.isAsync )
{
trace( "Asynchronous media promise." );
var eventSource:IEventDispatcher = dataSource as IEventDispatcher;
eventSource.addEventListener( Event.COMPLETE, onMediaLoaded );
}
else
{
trace( "Synchronous media promise." );
readMediaData();
}
}
protected function browseCanceled(event:Event):void
{
// TODO Auto-generated method stub
}
protected function mediaError(event:ErrorEvent):void
{
// TODO Auto-generated method stub
}
private function onMediaLoaded( event:Event ):void
{
trace("Media load complete");
readMediaData();
}
private function readMediaData():void
{
//do something with the data
}
protected function showFriends(event:MouseEvent):void
{
// TODO Auto-generated method stub
if (listGroup.visible == true)
{
listGroup.visible = false;
listGroup.includeInLayout = false;
trace("Hide Friends Drop Down");
}
else
{
listGroup.visible = true;
listGroup.includeInLayout = true;
trace("Show Friends Drop Down");
}
}
]]>
</fx:Script>
Im using the following as the base code: http://corlan.org/2011/07/04/creating-flex-mobile-section-lists/
UPDATE: Ultimately I would like to create the following layout where the checkbox is independent of the list item & when you touch the eye icon it opens an accordian style list:

CS6 Error 1046: Type was not found or was not a compile-time constant: Vector3D

I'm trying to use vectors to make an enemy track a target (player's ship). However, I keep getting Error 1046. Any advice? The error occurs on the line containing the drawForceVector function in the Game.as file.
Here is my Game.as file code:
package{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Vector3D;
import flash.text.TextField;
public class Game extends MovieClip
{
public static var mouse :Vector3D = new Vector3D(100, 100);
public var boids :Vector.<Boid> = new Vector.<Boid>;
public var forces :Sprite;
public function Game() {
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e :Event) :void {
var i :int, boid :Boid;
for (i = 0; i < 2; i++) {
boid = new Boid(stage.stageWidth * Math.random(), stage.stageHeight * Math.random(), 20 + Math.random() * 20);
addChild(boid);
boids.push(boid);
}
forces = new Sprite();
addChild(forces);
}
public function update():void {
var i :int;
forces.graphics.clear();
for (i = 0; i < boids.length; i++) {
boids[i].update();
drawForces(boids[i]);
}
}
private function drawForces(boid :Boid) :void {
var desired :Vector3D = boid.desired.clone();
var velocity :Vector3D = boid.velocity.clone();
var steering :Vector3D = boid.steering.clone();
velocity.normalize();
desired.normalize();
steering.normalize();
// Force vectors
drawForceVector(boid, velocity, 0x00FF00);
drawForceVector(boid, desired, 0x454545);
drawForceVector(boid, steering, 0x0000FF);
// Target
forces.graphics.lineStyle(1, 0x323232);
forces.graphics.beginFill(0x323232);
forces.graphics.drawCircle(mouse.x, mouse.y, 10);
forces.graphics.endFill();
}
private function drawForceVector(boid :Boid, force :Vector3D, color :uint, scale :Number = 100) :void { //ERROR OCCURS ON THIS LINE
forces.graphics.moveTo(boid.x + boid.width / 2, boid.y + boid.height / 2);
forces.graphics.lineStyle(2, color);
forces.graphics.lineTo(boid.x + force.x * scale, boid.y + force.y * scale);
}
}
}
Boid.as Code:
package {
import flash.display.MovieClip;
import flash.geom.Vector3D;
import flash.object.Vector3D;
public class Boid extends MovieClip
{
public static const MAX_FORCE :Number = 0.4;
public static const MAX_VELOCITY :Number = 3;
public var position :Vector3D;
public var velocity :Vector3D;
public var target :Vector3D;
public var desired :Vector3D;
public var steering :Vector3D;
public var mass :Number;
public function Boid(posX :Number, posY :Number, totalMass :Number = 20) {
position = new Vector3D(posX, posY);
velocity = new Vector3D(-1, -2);
target = new Vector3D(310, 240);
desired = new Vector3D(0, 0);
steering = new Vector3D(0, 0);
mass = totalMass;
truncate(velocity, MAX_VELOCITY);
x = position.x;
y = position.y;
graphics.beginFill(0xFF0000);
graphics.drawRect(0, 0, 20, 20);
graphics.endFill();
}
private function seek(target :Vector3D) :Vector3D {
var force :Vector3D;
desired = target.subtract(position);
desired.normalize();
desired.scaleBy(MAX_VELOCITY);
force = desired.subtract(velocity);
return force;
}
public function truncate(vector :Vector3D, max :Number) :void {
var i :Number;
i = max / vector.length;
i = i < 1.0 ? 1.0 : i;
vector.scaleBy(i);
}
public function update():void {
target = Game.mouse;
steering = seek(target);
truncate(steering, MAX_FORCE);
steering.scaleBy(1 / mass);
velocity = velocity.add(steering);
truncate(velocity, MAX_VELOCITY);
position = position.add(velocity);
x = position.x;
y = position.y;
}
}
}
Main.as Code:
package {
import flash.display.Sprite;
import flash.events.*;
[SWF(width = "600", height = "480")]
public class Main extends Sprite
{
private var game :Game = new Game();
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
stage.addEventListener(Event.ENTER_FRAME, enterFrame);
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
stage.frameRate = 30;
addChild(game);
}
private function mouseMove(e :MouseEvent) :void {
Game.mouse.x = e.stageX;
Game.mouse.y = e.stageY;
}
private function enterFrame(e :Event) :void {
game.update();
}
}
}
Thank you very much.
The target version of the player must be higher than the one you've got - Flash Player 9. As seen by the reference, it's said that Vector3D is included in: Runtime Versions: Flash Player 10, AIR 1.5. So basically you are building for so old Flash Player, that it still doesn't have this class inside it.
Simply change the target version to the latest possible one, and be sure it's higher than Flash Player 10. That will do the job! :)

Webcam shows mirror image using action script

Using Flash CS5.5 and action script 3 I have made a small application for image capturing through web cam and integrated it in php page working perfect also save image. But problem is it shows MIRROR image on screen means if I move my right hand on screen it shows left hand. My query is how can I correct this setting. Here is the code -
package take_picture_fla
{
import com.adobe.images.*;
import flash.display.*;
import flash.events.*;
import flash.media.*;
import flash.net.*;
import flash.ui.*;
import flash.utils.*;
dynamic public class MainTimeline extends MovieClip
{
public var capture_mc:MovieClip;
public var bitmap:Bitmap;
public var rightClickMenu:ContextMenu;
public var snd:Sound;
public var video:Video;
public var bitmapData:BitmapData;
public var warn:MovieClip;
public var save_mc:MovieClip;
public var bandwidth:int;
public var copyright:ContextMenuItem;
public var cam:Camera;
public var quality:int;
public function MainTimeline()
{
addFrameScript(0, frame1);
return;
}// end function
public function onSaveJPG(event:Event) : void
{
var myEncoder:JPGEncoder;
var byteArray:ByteArray;
var header:URLRequestHeader;
var saveJPG:URLRequest;
var urlLoader:URLLoader;
var sendComplete:Function;
var e:* = event;
sendComplete = function (event:Event) : void
{
warn.visible = true;
addChild(warn);
warn.addEventListener(MouseEvent.MOUSE_DOWN, warnDown);
warn.buttonMode = true;
return;
}// end function
;
myEncoder = new JPGEncoder(100);
byteArray = myEncoder.encode(bitmapData);
header = new URLRequestHeader("Content-type", "application/octet-stream");
saveJPG = new URLRequest("save.php");
saveJPG.requestHeaders.push(header);
saveJPG.method = URLRequestMethod.POST;
saveJPG.data = byteArray;
urlLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, sendComplete);
urlLoader.load(saveJPG);
return;
}// end function
public function warnDown(event:MouseEvent) : void
{
navigateToURL(new URLRequest("images/"), "_blank");
warn.visible = false;
return;
}// end function
function frame1()
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
rightClickMenu = new ContextMenu();
copyright = new ContextMenuItem("Developed By www.webinfopedia.com Go to Application");
copyright.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, myLink);
copyright.separatorBefore = false;
rightClickMenu.hideBuiltInItems();
rightClickMenu.customItems.push(copyright);
this.contextMenu = rightClickMenu;
snd = new camerasound();
bandwidth = 0;
quality = 100;
cam = Camera.getCamera();
cam.setQuality(bandwidth, quality);
cam.setMode(320, 240, 30, false);
video = new Video();
video.attachCamera(cam);
video.x = 20;
video.y = 20;
addChild(video);
bitmapData = new BitmapData(video.width, video.height);
bitmap = new Bitmap(bitmapData);
bitmap.x = 360;
bitmap.y = 20;
addChild(bitmap);
capture_mc.buttonMode = true;
capture_mc.addEventListener(MouseEvent.CLICK, captureImage);
save_mc.alpha = 0.5;
warn.visible = false;
return;
}// end function
public function captureImage(event:MouseEvent) : void
{
snd.play();
bitmapData.draw(video);
save_mc.buttonMode = true;
save_mc.addEventListener(MouseEvent.CLICK, onSaveJPG);
save_mc.alpha = 1;
return;
}// end function
public function myLink(event:Event)
{
navigateToURL(new URLRequest("http://www.webinfopedia.com/export-database-data-to-excel-in-php.html"), "_blank");
return;
}// end function
}
}
You can just use video.scaleX = -1; to mirror the image.
You should check your camera's settings at the system level - it is possible that the device driver is set to mirror the image.

How to add scrollbar for UIComponent in flex4.0 after it zoom

I am using a canvas with UIComponent for loading image in flex4. I want to zoom image, it is done image is zoomed but scrollbar is not added to canvas because i want to move image with scrolling and also mouse over, Please suggest me any solution for this or any having application.
Thanks,
Nitin
LoadPDF.mxml
<mx:Metadata>
[Event(name="PDFComplete",type="flash.events.Event")]
</mx:Metadata>
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Alert;
private var swfURL:String = "pdf/outra.swf";
public var loader:Loader = null;
public var bool:Boolean = false;
public var myPath:String = "";
import mx.events.ScrollEvent;
public var libMC:MovieClip = new MovieClip();
private function init():void {
/*Hide the default context menu with print option*/
var myMenu:ContextMenu = new ContextMenu();
myMenu.hideBuiltInItems();
var defaultItems:ContextMenuBuiltInItems = myMenu.builtInItems;
defaultItems.print = false;
this.contextMenu = myMenu;
//if (ExternalInterface.available)
ExternalInterface.addCallback("pdfURL", loadPDF);
if(bool == false) {
loadPDF(swfURL);
bool = true;
}
}
private function loadPDF(name:String):void {
myPath = name;
if(loader!= null) {
swfContainer.removeChild(loader);
loader = null;
}
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, swfComplete);
var fLoader:ForcibleLoader = new ForcibleLoader(loader);
fLoader.load(new URLRequest(myPath));
swfContainer.addChild(loader);
swfContainer.addEventListener(Event.RESIZE, updateStageSize);
}
private function swfComplete(event:Event):void{
libMC = event.currentTarget.content as MovieClip;
libMC.gotoAndStop(1);
dispatchEvent(new Event("PDFComplete"));
controls.target = libMC;
controls.rootSwfContainer = swfContainer;
libMC.x = (swfContainer.width/2)-(libMC.width/2);
libMC.y = (swfContainer.height/2)-(libMC.height/2);
var point:Point=new Point(libMC.x+libMC.width/2, libMC.y+libMC.height/2);
controls.points = point;
}
public function updateStageSize(e:Event):void
{
if(libMC.width < swfContainer.width && libMC.height < swfContainer.height) {
libMC.x = (swfContainer.width-libMC.width)/2;
libMC.y = (swfContainer.height-libMC.height)/2;
}
}
]]>
</mx:Script>
<views:Control id="controls"/>
<mx:HDividedBox top="40" width="100%" height="100%">
<mx:Canvas id="canvasContainer" backgroundColor="#222222" width="100%" height="100%" horizontalScrollPolicy="auto" verticalScrollPolicy="auto">
<mx:UIComponent id="swfContainer" width="100%" height="100%"/>
</mx:Canvas>
</mx:HDividedBox>
Control.mxml
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.events.DropdownEvent;
import mx.events.IndexChangedEvent;
[Bindable]private var _currentPage:Number;
private var _target:*;
private var _swfContainer:*;
private var _point:Point;
private var currentScale:Number =1;
private var rotateIndex:Number = 0;
public function set target(value:*):void {
_target = value;
}
public function get target():*{
return _target;
}
public function set rootSwfContainer(value:*):void {
_swfContainer = value;
}
public function get rootSwfContainer():* {
return _swfContainer;
}
public function set points(point:Point):* {
_point = point;
}
public function get points():* {
return _point;
}
public function zoomIN():void {
if((Number(_target.width - _swfContainer.width) <= 1000 ) || (Number(_target.height - _swfContainer.height ) <= 1000)) {
var xPos:Number = (_swfContainer.width/2)-(_target.width/2);
var yPos:Number = (_swfContainer.height/2)-(_target.height/2);
Tweener.addTween(_target, {x:xPos, y:yPos, scaleX: _target.scaleX * 1.25, scaleY: _target.scaleY * 1.25, transition: 'easeOut'});
repositionObject();
}
}
public function zoomOUT():void {
if((_target.width > 100) && (_target.height > 100)) {
var xPos:Number = (_swfContainer.width/2)-(_target.width/2);
var yPos:Number = (_swfContainer.height/2)-(_target.height/2);
Tweener.addTween(_target, {x:xPos, y:yPos,scaleX: _target.scaleX * .6, scaleY: _target.scaleY * .6, transition: 'easeOut'});
repositionObject();
}
}
public function repositionObject():void {
var tempXPos:Number = Number((_swfContainer.width/2)-(_target.width/2));
var tempYPos:Number = Number((_swfContainer.height/2)-(_target.height/2));
if(tempXPos <= 0) {
tempXPos = 0;
}
if(tempYPos <= 0) {
tempYPos = 0;
}
_target.x = tempXPos;
_target.y = tempYPos;
}
]]>
</mx:Script>
<!-- icon="#Embed(source='assets/buttonicon.png')" height="16"-->
<mx:Button label="Zoom In (+)" click="zoomIN()" />
<mx:Button label="Zoom Out (-)" click="zoomOUT()"/>

ActionScript 3: Can not Find Uploaded Files

This is my first ActionScript 3 application. It is supposed to upload data to specific location, which should be the www root location - the location where upload.php resides.
After I run the ActionScript application, I can select the file and I can see that the data is being uploaded, but I can never find it.
Would you be so kind, please, and help me to understand what is going on, and how can I find the uploaded data? I checked both: temporary files location, and the target destination.
Here is the ActionScript code:
package {
import flash.display.Sprite;
import flash.events.*;
import flash.net.*;
import flash.text.*;
public class ch30ex2 extends Sprite {
protected var fileRef:FileReference;
protected var uploadButton:TestButton;
protected var tf:TextField;
protected const YOUR_UPLOAD_URL:String = "http://localhost/Saifa/www/upload.php";
public function ch30ex2() {
fileRef = new FileReference();
fileRef.addEventListener(Event.SELECT, selectHandler);
fileRef.addEventListener(Event.CANCEL, cancelHandler);
fileRef.addEventListener(ProgressEvent.PROGRESS, progressHandler);
fileRef.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
fileRef.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler);
fileRef.addEventListener(Event.COMPLETE, completeHandler);
var btn:TestButton;
btn = new TestButton(100, 25, "Browse...");
btn.addEventListener(MouseEvent.CLICK, function(event:Event):void {
fileRef.browse();
});
btn.x = btn.y = 20;
addChild(btn);
uploadButton = btn = new TestButton(100, 25, "Upload");
btn.addEventListener(MouseEvent.CLICK, function(event:Event):void {
fileRef.upload(new URLRequest(YOUR_UPLOAD_URL));
});
btn.x = 20; btn.y = 55;
addChild(btn);
tf = new TextField();
tf.defaultTextFormat = new TextFormat("_sans", 11, 0);
tf.multiline = tf.wordWrap = true;
tf.autoSize = TextFieldAutoSize.LEFT;
tf.width = 300; tf.x = 130; tf.y = 58;
addChild(tf);
cancelHandler(null);
}
protected function selectHandler(event:Event):void {
tf.text = fileRef.name;
uploadButton.mouseEnabled = uploadButton.tabEnabled = true;
uploadButton.alpha = 1;
}
protected function cancelHandler(event:Event):void {
tf.text = "";
uploadButton.mouseEnabled = uploadButton.tabEnabled = false;
uploadButton.alpha = 0.3;
}
protected function progressHandler(event:ProgressEvent):void {
tf.text = "Uploading " +
event.bytesLoaded + " / " + event.bytesTotal + "bytes ...";
}
protected function errorHandler(event:ErrorEvent):void {
tf.text = event.text;
}
protected function completeHandler(event:Event):void {
tf.text = "Upload complete!";
}
}
}
import flash.display.*;
import flash.text.*;
class TestButton extends Sprite {
public var label:TextField;
public function TestButton(w:Number, h:Number, labelText:String) {
graphics.lineStyle(0.5, 0, 0, true);
graphics.beginFill(0xa0a0a0);
graphics.drawRoundRect(0, 0, w, h, 8);
label = new TextField();
addChild(label);
label.defaultTextFormat = new TextFormat("_sans", 11, 0, true, false,
false, null, null, "center");
label.width = w;
label.height = h;
label.text = labelText;
label.y = (h - label.textHeight)/2 - 2;
buttonMode = true;
mouseChildren = false;
}
}
and here is the PHP code, which is supposed to copy the temporary uploaded file:
<?php
move_uploaded_file($_FILES[‘Filedata’][‘tmp_name’], ‘./‘.time().$_FILES[‘Filedata’][‘name’]);
?>
All the code is from ActionScript Bible book
I would kindly like to ask for the following:
What can be the possible sources of my problem?
Looks my code correct? It gets compiled by FlashBuilder without problems
How can I identify source of my issue?
If you would have an example of working ActionScript + PHP application, I would be happy to see it.
As I spend hours after hours trying various things and combinations, I hope somebody might have had similar problem. Thank you.
In your PHP statement, there's a weired quote ‘ instead of a simple quote '.
<?php
move_uploaded_file($_FILES['Filedata']['tmp_name'], './'.time().$_FILES['Filedata']['name']);
?>
I tried with this and it worked fine.
Also here's the MXML i used to test:
<?xml version="1.0" encoding="utf-8"?>
<s:Application
minHeight="600"
minWidth="955"
creationComplete="application1_creationCompleteHandler(event)"
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
import spark.components.Button;
import spark.components.Label;
protected function application1_creationCompleteHandler(event:FlexEvent):void {
ch30ex2();
}
protected var fileRef:FileReference;
protected var uploadButton:Button;
protected var tf:Label;
protected const YOUR_UPLOAD_URL:String = "http://127.0.0.1/test/test.php";
public function ch30ex2():void {
fileRef = new FileReference();
fileRef.addEventListener(Event.SELECT, selectHandler);
fileRef.addEventListener(Event.CANCEL, cancelHandler);
fileRef.addEventListener(ProgressEvent.PROGRESS, progressHandler);
fileRef.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
fileRef.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler);
fileRef.addEventListener(Event.COMPLETE, completeHandler);
var btn:Button;
btn = new Button();
btn.label = "Browse...";
btn.addEventListener(MouseEvent.CLICK, function(event:Event):void {
fileRef.browse();
});
btn.x = btn.y = 20;
addElement(btn);
uploadButton = btn = new Button();
btn.label = "Upload";
btn.addEventListener(MouseEvent.CLICK, function(event:Event):void {
fileRef.upload(new URLRequest(YOUR_UPLOAD_URL));
});
btn.x = 20;
btn.y = 55;
addElement(btn);
tf = new Label();
tf.width = 300;
tf.x = 130;
tf.y = 58;
addElement(tf);
cancelHandler(null);
}
protected function selectHandler(event:Event):void {
tf.text = fileRef.name;
uploadButton.mouseEnabled = uploadButton.tabEnabled = true;
uploadButton.alpha = 1;
}
protected function cancelHandler(event:Event):void {
tf.text = "";
uploadButton.mouseEnabled = uploadButton.tabEnabled = false;
uploadButton.alpha = 0.3;
}
protected function progressHandler(event:ProgressEvent):void {
tf.text = "Uploading " + event.bytesLoaded + " / " + event.bytesTotal + "bytes ...";
}
protected function errorHandler(event:ErrorEvent):void {
tf.text = event.text;
}
protected function completeHandler(event:Event):void {
tf.text = "Upload complete!";
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Placer ici les éléments non visuels (services et objets de valeur, par exemple). -->
</fx:Declarations>
</s:Application>