Sending Image in AS3 - actionscript-3

I'm trying to send an image in as3, since some images are to big, I tried to split the image apart and then send them, after that reassemble them. This is the code:
The Sender Code:
package
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.DatagramSocketDataEvent;
import flash.events.Event;
import flash.net.DatagramSocket;
import flash.net.FileReference;
import flash.utils.ByteArray;
public class test extends Sprite
{
private var socket:DatagramSocket;
private var r:FileReference;
private var loader:Loader;
public function test()
{
socket = new DatagramSocket();
socket.bind(12395, "127.0.0.1");
r = new FileReference();
r.addEventListener(Event.SELECT, loadFile);
r.browse();
}
protected function loadFile(event:Event):void
{
r.addEventListener(Event.COMPLETE, sendFile);
r.load();
}
protected function sendFile(event:Event):void
{
var b:ByteArray;
trace(r.data.bytesAvailable);
for(var i:uint = 0; i < r.data.bytesAvailable;)
{
b = new ByteArray();
if(r.data.bytesAvailable >= 1024)
{
r.data.readBytes(b, 0, 1024);
socket.send(b, 0, 0, "127.0.0.1", 12345);
trace(b.bytesAvailable);
trace(r.data.bytesAvailable);
}
else
{
r.data.readBytes(b, i, r.data.bytesAvailable);
socket.send(b, 0, 0, "127.0.0.1", 12345);
b = new ByteArray();
socket.send(b, 0, 0, "127.0.0.1", 12345);
trace(b.bytesAvailable);
trace(r.data.bytesAvailable);
break;
}
}
trace("----------------------------");
}
}
}
The Receiver Code:
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.DatagramSocketDataEvent;
import flash.events.Event;
import flash.net.DatagramSocket;
import flash.net.FileReference;
import flash.utils.ByteArray;
var socket:DatagramSocket;
var loader:Loader;
var b:ByteArray;
var i:uint = 0;
b = new ByteArray();
socket = new DatagramSocket();
socket.bind(12345, "127.0.0.1");
socket.addEventListener(DatagramSocketDataEvent.DATA, handleData);
socket.receive();
function handleData(event:DatagramSocketDataEvent):void
{
if(event.data.bytesAvailable != 0)
{
trace("writing bytes...");
event.data.readBytes(b, i);
trace(b.bytesAvailable);
//b.length = b.length + 1;
i = i + 1024;
}
else
{
loadImg();
}
}
function loadImg():void
{
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, addToStage);
loader.loadBytes(b);
}
function addToStage(event:Event):void
{
addChild(loader);
}
But the image does't load, even though the sent and the received bytes are exactly equal.

I just simply modify on sender code as below:
else
{
r.data.readBytes(b, i, r.data.bytesAvailable);
socket.send(b, 0, 0, "127.0.0.1", 12345);
b = new ByteArray();
b.writeByte(0) // ADD THIS NEW LINE
socket.send(b, 0, 0, "127.0.0.1", 12345);
trace(b.bytesAvailable);
trace(r.data.bytesAvailable);
break;
}
And it will fire the loadImg function.

Related

AS3. How can a global var take a value which appears in a listener Event.COMPLETE

This is my code:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.display.LoaderInfo;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.display.DisplayObject;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.display.GradientType;
import flash.sampler.getSize;
public class Miniaturka extends MovieClip {
private var id:String;
public static var miniWidth:Number = 0;
private var tween:Tween;
private var tryb:Boolean;
private var button:Sprite;
private var index:Number;
private var aktywna:Boolean = false;
public var bLoad:Boolean = false;
public function Miniaturka(id:String,index:Number):void {
this.id = id;
this.index = index;
tryb = false;
var loader:Loader = new Loader();
loader.load(new URLRequest("images/"+id+"m.jpg"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,nLoadComplete);
this.alpha = 1;
button = new Sprite();
button.graphics.beginFill(0x000000,0);
button.graphics.drawRect(0,0,889,500);
button.graphics.endFill();
button.buttonMode = true;
addChild(button);
button.addEventListener(MouseEvent.MOUSE_OVER,onOver);
button.addEventListener(MouseEvent.MOUSE_OUT,onOut);
button.addEventListener(MouseEvent.CLICK,onClick);
}
private function nLoadComplete(event:Event):void {
var loader:Loader = new Loader();
loader = LoaderInfo(event.target).loader;
pusty.addChild(loader);
ladowanie.visible = false;
tween = new Tween(pusty,"alpha",Regular.easeOut,0,0.6,2,true);
bLoad = true;
setStan(false);
miniWidth = loader.width;
pusty.alpha = 0;
}
private function onOver(event:MouseEvent):void {
if (!aktywna) {
setStan(true);
}
}
private function onOut(event:MouseEvent):void {
if (!aktywna) {
setStan(false);
}
}
private function onClick(event:MouseEvent):void {
aktywuj();
}
public function deaktywuj():void {
setStan(false);
aktywna = false;
}
public function aktywuj():void {
MovieClip(parent).deaktywuj();
aktywna = true;
setStan(true);
MovieClip(parent.parent).loadBig(id,index);
}
private function setStan(tryb:Boolean):void {
this.tryb = tryb;
if (tryb) {
pusty.alpha = 1;
} else {
pusty.alpha = 0.6;
}
}
}
}
I want to create a gallery, and this is a code of a class which loads jpg. files with different widths, but the same height.
My problem is that I want to make the public static var miniWidth which takes the value: loader.width in function: nLoadComplete, take that value as a global var, and put it in the line: button.graphics.drawRect(0,0,889,500); so it would look like button.graphics.drawRect(0,0,miniWidth ,500);
This will create a button(rectangle) the same height and width as the loaded jpg. but i can't figure it out... How can I do that?
Wait for the image to load before drawing your shape. In the example below, I've only reprinted the affected functions.
private function Miniaturka(id:String, index:Number):void {
this.id = id;
this.index = index;
tryb = false;
var loader:Loader = new Loader();
loader.load(new URLRequest("images/" + id + "m.jpg"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, nLoadComplete);
this.alpha = 1;
}
private function nLoadComplete(event:Event):void {
var loader:Loader = new Loader();
loader = LoaderInfo(event.target).loader;
pusty.addChild(loader);
ladowanie.visible = false;
tween = new Tween(pusty, "alpha", Regular.easeOut, 0, 0.6, 2, true);
bLoad = true;
setStan(false);
miniWidth = loader.width;
pusty.alpha = 0;
createBtn();
}
private function createBtn():void {
button = new Sprite();
button.graphics.beginFill(0x000000, 0);
button.graphics.drawRect(0, 0, miniWidth, 500);
button.graphics.endFill();
button.buttonMode = true;
addChild(button);
button.addEventListener(MouseEvent.MOUSE_OVER, onOver);
button.addEventListener(MouseEvent.MOUSE_OUT, onOut);
button.addEventListener(MouseEvent.CLICK, onClick);
}

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

I am trying to add a movieclip that is generated through a loop from SearchVectorTest class file, and put that into sresultnologin class file. Then i will make that movieclip clickable, and when clicked, goes to next class,display a new stage, pull the information from that specific stored in that movieclip, and pull the rest of the information from the database and display them.
I am having an error at the moment, Type not found. I can't seem to add the Movieclip(box) as a button. on this line var newListing:Listing2 = Bolder.getChildAt(0);
sresultnologin.as
package com.clark
{
import com.clark.SearchVectorTest;
import flash.display.*;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.Stage;
import fl.controls.Button;
import flash.display.MovieClip;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class sresultnologin extends MovieClip {
public var s1:Searchreult = new Searchreult ();
public function sresultnologin(){
// init
addEventListener(Event.ADDED_TO_STAGE, onadded);
function onadded (event:Event):void{
s1.x=-10;
s1.y=10;
addChild(s1);
}
var s3:SearchVectorTest= new SearchVectorTest(new Vector.<searchVO1>);
addChild (s3);
s1.SRhome.addEventListener(MouseEvent.CLICK, fl_ClickToGoTol);
s1.ARsearch.addEventListener(MouseEvent.CLICK, fl_ClickToGosearch);
if( s3.listings.length > 0 )
{
// get the first listing in the listing array
var newListing:Listing8 = s3.listings[0];
newListing.addEventListener(MouseEvent.CLICK, gotoscener);
}
else
{
}
}
// private methods
private function gotoscener(event:MouseEvent):void
{
var s9:Listingdetail = new Listingdetail ();
removeChild(s1);
addChild(s9);
}
private function fl_ClickToGoTol(event:MouseEvent):void
{
var s9:Account = new Account ();
removeChild(s1);
addChild(s9);
}
private function fl_ClickToGosearch(event:MouseEvent):void
{
var s9:searchVO1 = new searchVO1 ();
removeChild(s1);
addChild(s9);
}
}
}
SearchVectorTest.as
package com.clark
{
import flash.display.MovieClip;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class SearchVectorTest extends MovieClip
{
public var listings:Vector.<Listing8>;
public function SearchVectorTest(test:Vector.<searchVO1>)
{
for (var j:int = 0; j < test.length; j++)
{
trace(test[j].nobed);
trace(test[j].zip);
trace(test[j].Location);
trace(test[j].price);
}
var len:int = test ? test.length : 0;
// create your vector for your listings with the len size
listings = new Vector.<Listing8>(len, true);
var currentY:int = 200;
for (var k:int = 0; k < test.length; k++)
{
var Bolder:Listing8 = new Listing8();
Bolder.x=20;
var bf:TextField = new TextField();
var bf1:TextField = new TextField();
var bf2:TextField = new TextField();
var bf3:TextField = new TextField();
bf3.width = 100;
bf.defaultTextFormat = new TextFormat("Arial", 12, 0, null, null, null, null, null, TextFormatAlign.CENTER);
bf.width = 100;
bf.autoSize = TextFieldAutoSize.CENTER;
bf1.width = 100;
bf1.autoSize = TextFieldAutoSize.CENTER;
bf2.autoSize = TextFieldAutoSize.CENTER;
bf3.autoSize = TextFieldAutoSize.CENTER;
bf3.width = 100;
bf1.y= bf.height+5;
bf.text = test[k].nobed;
bf1.text = test[k].zip;
bf2.text = test[k].Location;
bf3.text = test[k].price;
bf.x = (Bolder.height-bf.height)*.2
Bolder.addChild(bf);
Bolder.addChild(bf1);
Bolder.addChild(bf2);
Bolder.addChild(bf3);
Bolder.properties = test[k].nobed;
Bolder.properties = test[k].zip;
// position the object based on the accumulating variable.
Bolder.y = currentY;
addChild(Bolder);
Bolder.mouseChildren = false; // ignore children mouseEvents
Bolder.mouseEnabled = true; // enable mouse on the object - normally set to true by default
Bolder.useHandCursor = true; // add hand cursor on mouse over
Bolder.buttonMode = true;
listings[k] = Bolder;
currentY += Bolder.height + 10;
}
}
}
}
SearchVO1
package com.clark
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.Stage;
import fl.controls.Button;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLLoaderDataFormat;
import flash.net.URLVariables;
import flash.utils.*;
import flash.sampler.NewObjectSample;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
public class searchVO1 extends MovieClip {
private var Arvariables:URLVariables;
private var SrSend:URLRequest;
private var SaLoader:URLLoader;
public var nobed:String;
public var zip:String;
public var Location:String;
public var price:String;
public var callMethod:Function;
public var s1:searchpage = new searchpage ();
public function searchVO1():void{
addEventListener(Event.ADDED_TO_STAGE, onadded); // init
function onadded (event:Event):void{
s1.x=-10;
s1.y=10;
addChild(s1);
s1.account.addEventListener(MouseEvent.CLICK, fl_ClickToGoToaccount);
s1.searchs.addEventListener(MouseEvent.CLICK, ValidateAndsearch);
s1.Au.addEventListener(MouseEvent.CLICK, fl_ClickToGoToautomatch);
s1.setting.addEventListener(MouseEvent.CLICK, fl_ClickToGoToss);
s1.Shome.addEventListener(MouseEvent.CLICK, fl_ClickToGoTohom);
// Build the varSend variable
SrSend = new URLRequest("http://localhost/search.php");
SrSend.method = URLRequestMethod.POST;
Arvariables = new URLVariables;
SrSend.data = Arvariables;
SaLoader = new URLLoader();
SaLoader.dataFormat = URLLoaderDataFormat.TEXT;
SaLoader.addEventListener(Event.COMPLETE,Asandler);
// private methods
function Asandler(event:Event):void{
// retrieve data from php call
var resultString :String = event.target.data;
// parse result string as json object and cast it to array
var resultArray :Array = JSON.parse( resultString ) as Array;
// get the length of the result set
var len:int = resultArray.length;
// create vector of SearchVO
var searchVOs:Vector.<searchVO1> = new Vector.<searchVO1>();
// loop the result array
var i:int;
for(i=0; i<len; i++ )
{
searchVOs[i] = new searchVO1();
searchVOs[i].nobed = resultArray[i].nobed;
searchVOs[i].zip = resultArray[i].zip;
searchVOs[i].Location = resultArray[i].Location;
searchVOs[i].price = resultArray[i].price;
}
// call a function to create your boxes
// or maybe create your SearchVector class and pass it your search vector
var mySearchVector:SearchVectorTest = new SearchVectorTest(searchVOs);
addChild(mySearchVector);
}
}
}
public function searchVOs( nobed:String, zip:String, location:String, price:String )
{
this.nobed = nobed;
this.zip = zip;
this.Location = Location;
this.price = price;
}
public function ValidateAndsearch (event:MouseEvent):void {
// validate fields
Arvariables.nobed = s1.nobed.text;
Arvariables.zip = s1.zip.text;
Arvariables.Location = s1.Location.text;
Arvariables.price = s1.price.text;
SaLoader.load(SrSend);
var s7:sresultnologin = new sresultnologin()
removeChild(s1);
addChild(s7);
}
// close else condition for error handling
// close validate and send function
private function fl_ClickToGoToautomatch(event:MouseEvent):void
{
var s7:Auto = new Auto ();
removeChild(s1);
addChild(s7);
}
private function fl_ClickToGoToss(event:MouseEvent):void
{
//
}
private function fl_ClickToGoToaccount(event:MouseEvent):void
{
var s7:Account = new Account ();
removeChild(s1);
addChild(s7);
}
private function fl_ClickToGoTohom(event:MouseEvent):void
{
var s7:homepage = new homepage ();
removeChild(s1);
addChild(s7);
}
}
}
i think you have to remove these lines:
public var Bolder:MovieClip;
public var Listing2:MovieClip;
and try to get your Listing2 object like this:
var newListing:Listing2 = s3.getChildAt(0);
but if you want to access your listings in the SearchVectorTest class it's better to keep a public reference of them like this (maybe in an array or a vector):
SearchVectortest class:
public class SearchVectorTest extends MovieClip
{
/** the array of listings **/
public var listings:Vector.<Listing2>;
public function SearchVectorTest(test:Vector.<searchVO1>)
{
// put the array length in a variable -> if test = null make len = 0
var len:int = test ? test.length : 0;
// create your vector for your listings with the len size
listings = new Vector.<Listing2>(len, true);
// ...
for( var k:int = 0; k < len; k++ )
{
var bolder:Listing2 = new Listing2();
bolder.mouseChildren = false; // ignore children mouseEvents
bolder.mouseEnabled = true; // enable mouse on the object - normally set to true by default
bolder.useHandCursor = true; // add hand cursor on mouse over
bolder.buttonMode = true; // this must be set to true to have hand cursor
// add your linsting to the vector
listings[k] = bolder;
//...
}
// ...
}
}
after that you could get your listing like this:
sresultnologin.as
public class sresultnologin extends MovieClip
{
public var s1 :Searchreult = new Searchreult ();
//public var Bolder :MovieClip; // never used -> useless
//public var Listing2 :MovieClip; // never used -> useless
public function sresultnologin()
{
// init
addEventListener(Event.ADDED_TO_STAGE, onadded);
function onadded (event:Event):void
{
s1.x=-10;
s1.y=10;
addChild(s1);
}
var s3:SearchVectorTest= new SearchVectorTest(new Vector.<searchVO1>);
addChild (s3);
s1.SRhome.addEventListener(MouseEvent.CLICK, fl_ClickToGoTol);
s1.ARsearch.addEventListener(MouseEvent.CLICK, fl_ClickToGosearch);
// verify if there are enties in the array
if( s3.listings.length > 0 )
{
// get the first listing in the listing array
var newListing:Listing2 = s3.listings[0];
newListing.addEventListener(MouseEvent.CLICK, goto);
}
else
{
// do something if there are no results
}
}
//...
}
hope that helps ;)

Something wrong with my code

I have a fla file and an external class called theAlarmClock.
The hands on the clock should be moving, but after Ctrl+Enter - they are idle.
package
{
import flash.display.MovieClip;
import flash.events.TimerEvent;
import flash.utils.Timer;
public class theAlarmClock extends MovieClip
{
var myDate:Date;
var mySec:int;
var myMin:int;
var myHou:int;
var myTimer:Timer = new Timer(1000);
public function theAlarmClock()
{
myTimer.addEventListener(TimerEvent.TIMER, updateClock);
myTimer.start();
function updateClock(t:TimerEvent):void
{
myDate = new Date();
mySec = myDate.seconds;
myMin = myDate.minutes;
myHou = myDate.hours;
clockHandSeconds_mc.rotation = mySec * 6;
clockHandMinutes_mc.rotation = myMin * 6;
clockHandHours_mc.rotation = myHou * 30 + myMin * 0.5;
trace("tick");
}
}
}
}
package {
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.TimerEvent;
import flash.sensors.Accelerometer;
import flash.utils.Timer;
public class ToolClass extends Sprite
{
private var myDate:Date;
private var mySec:uint;
private var myMin:uint;
private var myHou:uint;
private var myClock:Sprite;
private var myHour:Shape;
private var myMins:Shape;
private var mySeco:Shape;
private var myTimer:Timer=new Timer(1000);
public function ToolClass()
{
drawClock();
myTimer.addEventListener(TimerEvent.TIMER,updateClock);
myTimer.start();
}
private function drawClock():void
{
myClock=new Sprite();
myClock.graphics.lineStyle(0,0x000000,1);
myClock.graphics.drawCircle(0,0,100);
addChild(myClock);
myClock.x=150;
myClock.y=150;
myHour=new Shape();
myHour.graphics.lineStyle(5,0x00000,1);
myHour.graphics.lineTo(0,-50);
myClock.addChild(myHour);
myMins=new Shape();
myMins.graphics.lineStyle(2,0x000000,1);
myMins.graphics.lineTo(0,-80);
myClock.addChild(myMins);
mySeco=new Shape();
mySeco.graphics.lineStyle(1,0x000000,1);
mySeco.graphics.lineTo(0,-80);
myClock.addChild(mySeco);
}
public function getTime():void
{
myDate=new Date();
myHou=myDate.hours;
myMin=myDate.minutes;
mySec=myDate.seconds;
}
private function updateClock(e:TimerEvent):void
{
getTime();
myHour.rotation=myHou*30+30*myMin/60;
myMins.rotation=myMin*6+6*mySec/60;
mySeco.rotation=mySec*6;
trace("tick");
}
}
}
You should try the get-functions of the Date-class.
Try this code in your updateClock function instead:
myDate = new Date();
mySec = myDate.getSeconds();
myMin = myDate.getMinutes();
myHou = myDate.getHours();
clockHandSeconds_mc.rotation = mySec * 6;
clockHandMinutes_mc.rotation = myMin * 6;
clockHandHours_mc.rotation = myHou * 30 + myMin * 0.5;
trace("tick");

How to call one of the function from this class?

I wanna call the function "nextMenu" & "prevMenu" from this class from Main.as
But I get the error 1136: Incorrect number of arguments. Expected 1.
Can help me see what I've left out on the codes?
CategoryScroller.as
package com.theflashfactor.carouselStackGallery.categoryMenu
{
import com.greensock.TweenMax;
import com.greensock.easing.Quint;
import com.theflashfactor.utils.Ref;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
/**
* Scroller to switch view for category item if there are more than visibleItem available
* #author Rimmon Trieu
*/
public class CategoryScroller extends Sprite
{
private var trackLength:int = 400;
private var scrubber:Sprite;
private var track:Shape;
private var categoryMenu:CategoryMenu;
private var trans:Sprite;
private var center:Number;
public function CategoryScroller(categoryMenu:CategoryMenu)
{
this.categoryMenu = categoryMenu;
buttonMode = true;
initialize();
}
/**
* Draw elemnt scrubber and track
*/
private function initialize():void
{
trans = new Sprite();
trans.graphics.beginFill(0, 0);
trans.graphics.drawRect(0, 0, trackLength, 13);
trans.graphics.endFill();
trans.addEventListener(MouseEvent.MOUSE_DOWN, transMouseDown, false, 0, true);
addChild(trans);
// Draw track
var color:uint = uint(Ref.getInstance().getRef("categoryCircleColor"));
track = new Shape();
track.graphics.lineStyle(1, color);
track.graphics.lineTo(trackLength, 0);
track.y = 6;
addChild(track);
// Draw scrubber
scrubber = new Sprite();
scrubber.graphics.beginFill(color);
scrubber.graphics.drawRect(0, 0, 80, 13);
scrubber.graphics.endFill();
center = (trackLength - scrubber.width) >> 1;
scrubber.x = center;
addChild(scrubber);
// Add dragging functionality
scrubber.addEventListener(MouseEvent.MOUSE_DOWN, scrubberMouseDown,false,0,true);
}
private function transMouseDown(event:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_UP, stageMouseUpHandler,false,0,true);
TweenMax.killTweensOf(scrubber);
categoryMenu.preTransition();
if (mouseX > width - scrubber.width) scrubber.x = (width - scrubber.width); else scrubber.x = mouseX;
}
/*private function transMouseUp(event:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_UP, stageMouseUpHandler);
checkPosition();
}*/
private function scrubberMouseDown(event:MouseEvent):void
{
event.stopImmediatePropagation();
TweenMax.killTweensOf(scrubber);
scrubber.startDrag(false, new Rectangle(0, 0, trackLength - scrubber.width, 0));
stage.addEventListener(MouseEvent.MOUSE_UP, stageMouseUpHandler,false,0,true);
categoryMenu.preTransition();
}
private function stageMouseUpHandler(event:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_UP, stageMouseUpHandler);
scrubber.stopDrag();
checkPosition();
}
private function checkPosition():void
{
var targetX:Number = (trackLength - scrubber.width) >> 1;
if (scrubber.x > (center + 20)) categoryMenu.postTransition(1);
else
if (scrubber.x < (center - 20)) categoryMenu.postTransition(-1);
else
categoryMenu.postTransition(0);
TweenMax.to(scrubber, .5, {x:targetX, ease:Quint.easeOut, overwrite:1});
}
public function nextMenu():void
{
categoryMenu.postTransition(1);
}
public function prevMenu():void
{
categoryMenu.postTransition(-1);
}
}
}
I've inserted the code below in my Main.as to call the function which I can't get it success.
import com.theflashfactor.carouselStackGallery.categoryMenu.CategoryScroller;
private var categoryScroller:CategoryScroller = new CategoryScroller();
categoryScroller.nextMenu();
Main.as
package
{
import com.theflashfactor.carouselStackGallery.CarouselStackGallery;
import com.theflashfactor.carouselStackGallery.categoryMenu.CategoryMenu;
import com.theflashfactor.carouselStackGallery.categoryMenu.CategoryScroller;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import org.casalib.events.LoadEvent;
import org.casalib.load.DataLoad;
/**
* Main document class
* #author Rimmon Trieu
*/
[SWF(frameRate="60", backgroundColor="0", pageTitle="3D Carousel Stack Gallery")]
public class Main extends Sprite
{
private var xmlPath:String = "../xml/Main.xml";
private var ts3:Sprite = new Sprite;
private var categoryMenu:CategoryMenu = new CategoryMenu();
private var categoryScroller:CategoryScroller = new CategoryScroller();
public function Main()
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
addEventListener(Event.ADDED_TO_STAGE, addToStage);
}
private function addToStage(event:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, addToStage);
var xmlLoad:DataLoad = new DataLoad(xmlPath);
xmlLoad.addEventListener(LoadEvent.COMPLETE, xmlLoaded);
xmlLoad.start();
ts3.graphics.beginFill(0x555555,1);
ts3.graphics.drawCircle(0, 0, 50);
ts3.graphics.endFill();
}
private function xmlLoaded(event:LoadEvent):void
{
event.target.removeEventListener(LoadEvent.COMPLETE, xmlLoaded);
opaqueBackground = uint(event.target.dataAsXml.settings.#backgroundColor);
var gallery:CarouselStackGallery = new CarouselStackGallery(event.target.dataAsXml);
addChild(gallery);
addChild(ts3);
ts3.addEventListener(MouseEvent.MOUSE_DOWN, ts3MouseDown,false,0,true);
}
private function ts3MouseDown(event:MouseEvent):void
{
categoryScroller.nextMenu();
}
}
}
As defined:
public function CategoryScroller(categoryMenu:CategoryMenu)
{
...
Shouldn't the class constructor be expecting an argument??
private var categoryScroller:CategoryScroller = new CategoryScroller();

How to mask image with another image in ActionScript 3.0

my issue is this, my users import an image with
FileReference and I need to mask it and then send it to server.
My problem is this: I'm be able do keep the filereference event and
transfer the image data into my canvas. I'm be able to send to server
the result of masking.
But I'm NOT be able to mask the image that my users have load in
my canvas.
You just need to add loaded bitmap to maskable container (e.g. Sprite). For exmaple:
Test.as
package {
import flash.display.Graphics;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.FileFilter;
import flash.net.FileReference;
public class Test extends Sprite {
private var _fileReference:FileReference;
private var _fileFilter:FileFilter;
private var _loader:Loader;
private var _imageContainer:Sprite;
private var _mask:Sprite;
private var _canvas:Sprite;
public function Test() {
addEventListener(Event.ADDED_TO_STAGE, addedToStageListener, false, 0, true);
}
private function addedToStageListener(event:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, addedToStageListener);
_fileReference = new FileReference();
_fileReference.addEventListener(Event.SELECT, fileSelectedListener, false, 0, true);
_fileReference.addEventListener(Event.COMPLETE, fileLoadCompleteListener, false, 0, true);
_fileFilter = new FileFilter("Images", "*.jpg;*.gif;*.png");
_loader = new Loader();
_canvas = new Sprite();
_mask = new Sprite();
var maskGraphics:Graphics = _mask.graphics;
maskGraphics.beginFill(0xFFFFFF);
maskGraphics.drawCircle(50, 50, 50);
maskGraphics.endFill();
_imageContainer = new Sprite();
_imageContainer.mask = _mask;
_canvas.addChild(_imageContainer);
_canvas.addChild(_mask);
addChild(_canvas);
stage.addEventListener(MouseEvent.CLICK, mouseClickListener, false ,0, true);
}
private function mouseClickListener(event:Event):void {
_fileReference.browse([_fileFilter]);
}
private function fileSelectedListener(event:Event):void {
_fileReference.load();
}
private function fileLoadCompleteListener(event:Event):void {
_loader.loadBytes(event.target.data);
while(_imageContainer.numChildren) {
_imageContainer.removeChildAt(0);
}
_imageContainer.addChild(_loader);
}
}}
This changes the situation, but not dramatically. Check out the modified solution:
package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.net.FileFilter;
import flash.net.FileReference;
public class Test extends Sprite {
private var _fileReference:FileReference;
private var _fileFilter:FileFilter;
private var _imageLoader:Loader;
private var _maskLoader:Loader;
private var _canvas:Sprite;
private var _imageLoaded:Boolean = false;
public function Test() {
addEventListener(Event.ADDED_TO_STAGE, addedToStageListener, false, 0, true);
}
private function addedToStageListener(event:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, addedToStageListener);
_fileReference = new FileReference();
_fileReference.addEventListener(Event.SELECT, fileSelectedListener, false, 0, true);
_fileReference.addEventListener(Event.COMPLETE, fileLoadCompleteListener, false, 0, true);
_fileFilter = new FileFilter("Images", "*.jpg;*.gif;*.png");
_imageLoader = new Loader();
_imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoadCompleteListener, false, 0, true);
_maskLoader = new Loader();
_maskLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, maskLoadCompleteListener, false, 0, true);
_canvas = new Sprite();
addChild(_canvas);
stage.addEventListener(MouseEvent.CLICK, mouseClickListener, false ,0, true);
}
private function mouseClickListener(event:Event):void {
_fileReference.browse([_fileFilter]);
}
private function fileSelectedListener(event:Event):void {
_fileReference.load();
}
private function fileLoadCompleteListener(event:Event):void {
if (!_imageLoaded) {
_imageLoader.loadBytes(event.target.data);
} else {
_maskLoader.loadBytes(event.target.data);
}
}
private function imageLoadCompleteListener(event:Event):void {
_imageLoaded = true;
}
private function maskLoadCompleteListener(event:Event):void {
var imageBitmap:Bitmap = _imageLoader.content as Bitmap;
var maskBitmap:Bitmap = _maskLoader.content as Bitmap;
if (imageBitmap && maskBitmap) {
var imageBitmapData:BitmapData = imageBitmap.bitmapData;
var maskBitmapData:BitmapData = maskBitmap.bitmapData;
var boundRectange:Rectangle = new Rectangle(0, 0, maskBitmap.width, maskBitmap.height);
var destinationPoint:Point = new Point(0, 0);
var finalBitmapData:BitmapData = new BitmapData(maskBitmap.width, maskBitmap.height);
finalBitmapData.copyPixels(imageBitmapData, boundRectange, destinationPoint, maskBitmapData, destinationPoint);
var finalBitmap:Bitmap = new Bitmap(finalBitmapData);
while(_canvas.numChildren) {
_canvas.removeChildAt(0);
}
_canvas.addChild(finalBitmap);
}
}
}}
First click on the stage allows you to select masked image. Second click allows you to select image, which contains the masking information (e.g. png file with transparency). Hope this helps.