Missing Class EventResize - actionscript-3

I'm using AS3 Flex 4.6.0 / Air 3.1 and i cannot find the ResizeEvent.
I added the following code:
stage.addEventListener(Event.RESIZE, onStageResize);
and want to have the function
private function onStageResize(ev:Event) : void //Here i want to use ev:ResizeEvent instead of ev:Event
{
}
I need the ev.OldWidth and ev.OldHeight property and i don't know why i cannot use
import flash.event.ResizeEvent;
Anyone has an idea?
Thanks!

The import for Event.RESIZE is:
import flash.events.Event
Remember, that event will only be fired if the StageScaleMode.NO_SCALE is set.
package {
import flash.display.Sprite;
import flash.display.StageScaleMode;
import flash.events.Event
public class Main extends Sprite {
var swfWidthPrev:int;
var swfHeightPrev:int;
function resizeDisplay(event:Event):void
{
trace("~~~ resize event ~~~");
trace("swfWidthPrev: " + swfWidthPrev);
trace("swfHeightPrev: " + swfHeightPrev);
trace("swfWidthCur: " + stage.stageWidth);
trace("swfHeightCur: " + stage.stageHeight);
swfWidthPrev = stage.stageWidth;
swfHeightPrev = stage.stageHeight;
}
public function Main() {
if(stage){
stageReady();
}else{
this.addEventListener(Event.ADDED_TO_STAGE, stageReady);
}
}
private function stageReady(e:Event = null):void {
swfWidthPrev = stage.stageHeight;
swfHeightPrev = stage.stageWidth;
stage.scaleMode = StageScaleMode.NO_SCALE
stage.addEventListener(Event.RESIZE, resizeDisplay);
}
}
}

Related

AS3 Classes Public Atributte

I am getting an error whenever I try running the game. I get errors for every class saying that the public attribute can only be used inside a package and on this line with "private function moveMe" "the private attribute may only be used on class functions". I verified if I have the as file linked properly. I am not sure what the issue is.
package {
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.events.Event;
public class banana_fall extends MovieClip {
public function banana_fall (){
var velX:Number=0;
var velY:Number=0;
var falling:Boolean=false;
var gravity:Number=2;
public function banana() {
var timing:Timer = new Timer(20,0);
timing.addEventListener(TimerEvent.TIMER,moveMe);
timing.start();
}
private function moveMe(event:TimerEvent){
this.x=this.x+velX;
this.y=this.y+velY;
if (falling) {
velY=velY+gravity;
}
}
public function setSpot(atX,atY){
this.x=atX;
this.y=atY;
}
//
public function setSpeed(dx,dy){
velX=dx;
velY=dy;
}
}
}
}
The problem is that your public functions were inside your constructor function, which doesnt work in as3.
try this code:
package {
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.events.Event;
public class banana_fall extends MovieClip {
var velX: Number = 0;
var velY: Number = 0;
var falling: Boolean = false;
var gravity: Number = 2;
public function banana_fall() {
var timing: Timer = new Timer(20, 0);
timing.addEventListener(TimerEvent.TIMER, moveMe);
timing.start();
}
private function moveMe(event: TimerEvent) {
this.x = this.x + velX;
this.y = this.y + velY;
if (falling) {
velY = velY + gravity;
}
}
public function setSpot(atX, atY) {
this.x = atX;
this.y = atY;
}
//
public function setSpeed(dx, dy) {
velX = dx;
velY = dy;
}
}
}

as3 preloader stop loaded swf from caching?

sorry if this has been answered elsewhere but I can't find anything matching.
I have two swfs; a preloader (let's call it A) and some content (B). A loads B and adds it as a child. Everything is working beautifully (you can even see it here).
There's just one little problem I'm having. Normally when loading, say an image, into flash using the URLLoader Class, I add + "?" + new Date().getTime() to the URLRequest to force flash to load the latest version of the target, in other words to stop it using a cached version. Now, when I try to do this to the Loader that adds B to A, it can't find the URL (#2035 URL not found). So my question is: is what I'm trying to do possible, or should I take another approach to stopping B from caching?
Here's the preloader code:
package
{
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.ProgressEvent;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.ErrorEvent;
import flash.net.URLLoader;
import flash.text.TextField;
public class claude_loader extends MovieClip
{
public var main_movie:Loader = new Loader();
public var rss_loader:URLLoader;
private var perc_text:TextField = new TextField();
public function claude_loader()
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
with (perc_text)
{
x = this.stage.stageWidth / 2;
y = this.stage.stageHeight / 2;
}
addChild(perc_text);
main_movie.load(new URLRequest("claudia_summers.swf"+ "?" + new Date().getTime()));
main_movie.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, load_progress);
main_movie.contentLoaderInfo.addEventListener(Event.COMPLETE,on_complete);
}
public function load_progress(e:ProgressEvent):void
{
var perc:Number = Math.round((e.bytesLoaded/e.bytesTotal)*100);
perc_text.text = "Loading " + perc + "%";
if (e.bytesLoaded == e.bytesTotal)
{
perc_text.text = "Loading done";
}
}
public function on_complete(e:Event):void
{
rss_loader = new URLLoader(new URLRequest("http://news.sulsc.org/feed"));
rss_loader.addEventListener(Event.COMPLETE,rss_complete);
perc_text.text = "Loading RSS";
}
public function rss_complete(e:Event):void
{
MovieClip(main_movie.content).rss_xml = XML(e.target.data);
addChild(main_movie);
}
}
}
Not sure how/why, but adding String() around new Date().getTime() and giving main_movie an IOErrorEvent listener resolves the issue. I think I have a bug somewhere, as this surely should have worked without these additions, no? Anyway thanks for all your help!
Working code:
package
{
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.ProgressEvent;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.ErrorEvent;
import flash.net.URLLoader;
import flash.text.TextField;
import flash.events.IOErrorEvent;
public class claude_loader extends MovieClip
{
public var main_movie:Loader = new Loader();
public var rss_loader:URLLoader;
private var perc_text:TextField = new TextField();
public function claude_loader()
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
with (perc_text)
{
x = this.stage.stageWidth / 2;
y = this.stage.stageHeight / 2;
textColor = 0xFFFFFF;
}
addChild(perc_text);
main_movie.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,catch_error);
main_movie.load(new URLRequest("claudia_summers.swf" + "?" + String(new Date().getTime())));
function catch_error(e:IOErrorEvent):void
{
main_movie.load(new URLRequest("claudia_summers.swf"));
perc_text.text = "Failed non-cache load"
}
main_movie.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, load_progress);
main_movie.contentLoaderInfo.addEventListener(Event.COMPLETE,on_complete);
}
public function load_progress(e:ProgressEvent):void
{
var perc:Number = Math.round((e.bytesLoaded/e.bytesTotal)*100);
perc_text.text = "Loading " + perc + "%";
if (e.bytesLoaded == e.bytesTotal)
{
perc_text.text = "Loading done";
}
}
public function on_complete(e:Event):void
{
rss_loader = new URLLoader(new URLRequest("http://news.sulsc.org/feed"));
rss_loader.addEventListener(Event.COMPLETE,rss_complete);
perc_text.text = "Loading RSS";
}
public function rss_complete(e:Event):void
{
MovieClip(main_movie.content).rss_xml = XML(e.target.data);
addChild(main_movie);
}
}
}

AS3 preloader class error 1120: access of undefined property e. [Please Help]

I have some issues with my preloader class. everytime I try to run the MovieClip it gives me 4 errors of 1120: access of undefined property e. I dont know what the problem is.
package {
import flash.display.MovieClip;
import flash.events.*;
import flash.text.*;
public class loadingScene extends MovieClip {
public var percentLoad:Number = Math.round(e.bytesLoaded / e.bytesTotal * 100);
public function loadingScene() {
stop();
this.loaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
}
public function onProgress(e:ProgressEvent):void {
loader_txt.text = Math.round(e.bytesLoaded / e.bytesTotal *100)+ "%";
if (percentLoad == 100){
onLoaded();
}
}
function onLoaded() {
this.loaderInfo.removeEventListener(ProgressEvent.PROGRESS, onProgress);
trace("YES");
}
}
}
Delete where you initialize percentLoad. e does not exist at that point in time, thus its undefined. Also you didn't define e anywhere, but MovieClip thinks you did.
import flash.display.MovieClip;
import flash.events.*;
import flash.text.*;
public class loadingScene extends MovieClip {
public var percentLoad:Number = 0;
public function loadingScene() {
stop();
this.loaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
}
public function onProgress(e:ProgressEvent):void {
percentLoad = Math.round(e.bytesLoaded / e.bytesTotal * 100);
loader_txt.text = percentLoad+ "%";
if (percentLoad == 100){
onLoaded();
}
}
function onLoaded() {
this.loaderInfo.removeEventListener(ProgressEvent.PROGRESS, onProgress);
trace("YES");
}
}

AS3 browse client files from SWF in a server

newbie on AS3 here! :)
basically I'm trying to write an application that let the user choose an image file and display it (then I will manipulate pixels, so i don't want the application to store the image in a new file, just managing the ByteArray).
So far I wrote in Flash Develop some working code that show a window to choose the image and then display it. but when I upload to a server the generated files (myapplication.swf, expressinstall.swf, index.html, and the js folder) the window shows no more.
I'm using FileReference.browse() method.
What's wrong?
(edit: as pointed out from The_asMan here we miss some code, here it is - improved with the suggestion of The_asMan)
my package:
package searchfiles
{
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.Sprite;
import flash.net.FileReference;
import flash.net.FileReferenceList;
import flash.net.FileFilter;
import flash.events.*;
import flash.net.FileFilter;
import flash.net.FileReference;
import flash.net.URLRequest;
import flash.utils.ByteArray;
import flash.display.DisplayObject;
/**
* ...
* #author ddd
*/
public class searchForFiles extends EventDispatcher
{
public var newfile:FileReference;
public var loader:Loader
public var bitmapimg:BitmapData;
public function searchForFiles() {
newfile = new FileReference();
newfile.addEventListener(Event.SELECT, onFileSelected);
newfile.addEventListener(Event.CANCEL, onCancel);
newfile.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
newfile.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
trace("abbiamo instanziato un searchForFiles");
var textTypeFilter:FileFilter = new FileFilter("Image files (*.png, *.jpg, *tif)",
"*.png; *.jpg; *tif");
newfile.browse([textTypeFilter]);
}
public function onFileSelected(evt:Event):void
{
newfile.addEventListener(ProgressEvent.PROGRESS, onProgress);
newfile.addEventListener(Event.COMPLETE, onComplete);
newfile.load();
}
public function onProgress(evt:ProgressEvent):void
{
trace("Loaded " + evt.bytesLoaded + " of " + evt.bytesTotal + " bytes.");
}
public function onComplete(evt:Event):void
{
trace("File was successfully loaded.");
loader = new Loader();
loader.loadBytes(newfile.data);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, getBitmapData);
}
private function erroremanip(evt:IOErrorEvent):void {
trace("errore " + evt);
}
private var bitmapData:BitmapData
public function getBitmapData(e:Event):void {
var content:* = loader.content;
bitmapData = new BitmapData(content.width,content.height,true,0x00000000);
trace("loader.width = " +loader.width);
dispatchEvent( new Event(Event.COMPLETE));
//trace("get bitmap data called");
}
public function onCancel(evt:Event):void
{
trace("The browse request was canceled by the user.");
}
public function onIOError(evt:IOErrorEvent):void
{
trace("There was an IO Error.");
}
public function onSecurityError(evt:Event):void
{
trace("There was a security error.");
}
}
}
and here's the main()
package
{
import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.errors.IOError;
import flash.events.*;
import flash.events.MouseEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import searchfiles.searchForFiles;
/**
* ...
* #author ddd
*/
[SWF(width = "550", height = "600")]
public class Main extends MovieClip
{
public var file:searchForFiles;
public var mybtn:Loader = new Loader();
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);
// entry point
mybtn.addEventListener(MouseEvent.CLICK, mouseclicked);
mybtn.addEventListener(IOErrorEvent.IO_ERROR, erroremanip);
var urlqst:URLRequest = new URLRequest("preview_true.png");
mybtn.load(urlqst);
addChild(mybtn);
}
public function mouseclicked(e:MouseEvent):void {
trace("clicked");
file = new searchForFiles();
file.addEventListener(Event.COMPLETE, puttheimage);
}
private function erroremanip(e:IOError):void {
trace("ciao erroreio");
}
private function puttheimage(e:Event) :void {
addChild(file.loader);
}
}
}
FileReference.browse()
When outside local sandbox needs to be triggered via user interaction IE: mouseclick.
Basically, the click event needs to be in the stack somewhere.
You can verify this with.
file = new FileReference();
file.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
private function securityErrorHandler(event:SecurityErrorEvent):void {
trace("securityErrorHandler: " + event);
}
However, you posted no code and it is very hard to determine exactly what you did wrong.
[EDIT]
package searchfiles
{
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.Sprite;
import flash.net.FileReference;
import flash.net.FileReferenceList;
import flash.net.FileFilter;
import flash.events.*;
import flash.net.FileFilter;
import flash.net.FileReference;
import flash.net.URLRequest;
import flash.utils.ByteArray;
import flash.display.DisplayObject;
/**
* ...
* #author ddd
*/
public class searchForFiles extends EventDispatcher
{
public var newfile:FileReference;
public var loader:Loader
public var bitmapimg:BitmapData;
public function searchForFiles() {
newfile = new FileReference();
newfile.addEventListener(Event.SELECT, onFileSelected);
newfile.addEventListener(Event.CANCEL, onCancel);
newfile.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
newfile.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
}
// new function
public function browse(event:Event):void{
var textTypeFilter:FileFilter = new FileFilter("Image files (*.png, *.jpg, *tif)", "*.png; *.jpg; *tif");
newfile.browse([textTypeFilter]);
}
public function onFileSelected(evt:Event):void
{
newfile.addEventListener(ProgressEvent.PROGRESS, onProgress);
newfile.addEventListener(Event.COMPLETE, onComplete);
newfile.load();
}
public function onProgress(evt:ProgressEvent):void
{
trace("Loaded " + evt.bytesLoaded + " of " + evt.bytesTotal + " bytes.");
}
public function onComplete(evt:Event):void
{
trace("File was successfully loaded.");
loader = new Loader();
loader.loadBytes(newfile.data);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, getBitmapData);
}
private function erroremanip(evt:IOErrorEvent):void {
trace("errore " + evt);
}
private var bitmapData:BitmapData
public function getBitmapData(e:Event):void {
var content:* = loader.content;
bitmapData = new BitmapData(content.width,content.height,true,0x00000000);
trace("loader.width = " +loader.width);
dispatchEvent( new Event(Event.COMPLETE));
//trace("get bitmap data called");
}
public function onCancel(evt:Event):void
{
trace("The browse request was canceled by the user.");
}
public function onIOError(evt:IOErrorEvent):void
{
trace("There was an IO Error.");
}
public function onSecurityError(evt:Event):void
{
trace("There was a security error.");
}
}
}
and here's the main()
package
{
import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.errors.IOError;
import flash.events.*;
import flash.events.MouseEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import searchfiles.searchForFiles;
/**
* ...
* #author ddd
*/
[SWF(width = "550", height = "600")]
public class Main extends MovieClip
{
public var file:searchForFiles;
public var mybtn:Loader = new Loader();
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);
// entry point
// moved to init
file = new searchForFiles();
file.addEventListener(Event.COMPLETE, puttheimage);
mybtn.addEventListener(MouseEvent.CLICK, mouseclicked);
mybtn.addEventListener(IOErrorEvent.IO_ERROR, erroremanip);
var urlqst:URLRequest = new URLRequest("preview_true.png");
mybtn.load(urlqst);
addChild(mybtn);
}
public function mouseclicked(e:MouseEvent):void {
trace("clicked");
// events need to be set before any active code is run in the object
// that is why we moved listeners or else you risk the listener
// not getting triggered
file.browse()
}
private function erroremanip(e:IOError):void {
trace("ciao erroreio");
}
private function puttheimage(e:Event) :void {
addChild(file.loader);
}
}
}
FileReference is for accessing files on the user's local machine. It sounds like you want to load a file from the same server that is hosting the SWF files.
You can't 'browse' the server from Actionscript - unlesss you write code on your server to enable that - but you can load files by name using URLLoader.

Actionscript: add a sprite from a subclass to the display list of its super class?

Say i have these two classes:
MAIN.as
package
{
import flash.display.*; import mx.core.*;
import flash.events.*; import mx.collections.*;
import flash.geom.*; import mx.controls.*;
import flash.text.*; import mx.events.*;
import mx.styles.*;
import mx.containers.*;
public class MAIN extends Sprite
{
public var APPLICATION:Application = Application(Application.application);
public var keyDownText:TextField = new TextField();
public function MAIN()
{
stage.addEventListener(KeyboardEvent.KEY_DOWN,KEY_DOWN);
addEventListener(Event.ENTER_FRAME,STEP);
new OBJECT_square().CREATE(10,100,1);
}
public function STEP():void {}
public function DRAW():void {}
public function KEY_DOWN(event:KeyboardEvent):void
{
keyDownText.text = "Key code: " + event.keyCode;
this.addChild(keyDownText);
}
}
}
OBJECT_square.as
package
{
import flash.display.*;
import flash.events.*;
public class OBJECT_square extends Sprite
{
public var X:int = 0;
public var Y:int = 0;
public var DEPTH:int = 0;
public var SPRITE:Sprite = new Sprite();
public function CREATE(X:int,Y:int,DEPTH:int):void
{
this.DEPTH = DEPTH;
this.X = X;
this.Y = Y;
DRAW();
}
public function DRAW():void
{
SPRITE.graphics.beginFill(0xFF0000,1);
SPRITE.graphics.drawRect(X - 10,Y - 10,20,20);
SPRITE.graphics.endFill();
addChild(SPRITE);
}
}
}
Now how is it that I can add the variable SPRITE which is a Sprite in the OBJECT_square class to the display list of MAIN class? I've tried addChild(SPRITE) and super.addChild(SPRITE). If everything works I should see a red square somewhere on the screen but right now its all blank, except for the text drawn in the MAIN class.
Basically I want it so i can just make a new OBJECT_square and it will draw itself without any more instructions from the MAIN class.
Try this:
var obj:OBJECT_square = new OBJECT_square();
obj.CREATE(10,100,1);
addChild(obj);
Or, if you really want to do it in one go, you could try this:
In main
addChild((new OBJECT_square()).CREATE(10,100,1));
And change your draw function to return the square object
public function DRAW():OBJECT_square
{
SPRITE.graphics.beginFill(0xFF0000,1);
SPRITE.graphics.drawRect(X - 10,Y - 10,20,20);
SPRITE.graphics.endFill();
addChild(SPRITE);
return this;
}