AS3 debugger stops responding while trying to load image into sprite using Loader - actionscript-3

I'm trying to create a simple Menu in AS3. There is a sprite called startButton, which when pressed will call the function startGame, and that's it! But, not so easy. I'm using flashdevelop IDE so I'm trying to call a loader to get a .png image file for the spite startButton. But, it doesn't work. There are no error messages, the debugger just does not respond. Any help? Here is the code for both files
Main code:
package {
//Other Files
import Menu;
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.ui.Mouse;
public class Main extends Sprite {
//Game values
public static var gameWidth:int = 750;
public static var gameHeight:int = 750;
public function Main() {
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
addChild(Menu.startButton);
Menu.startButton.addEventListener(MouseEvent.CLICK, startGame);
stage.addEventListener(Event.ENTER_FRAME, update);
}
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
}
//Function starts game
public function startGame(evt:MouseEvent):void {
removeChild(Menu.startButton);
}
//Updates every 60 seconds
public function update():void {
trace("Updated");
}
}
}
And Menu Image code:
package {
//Other files
import Main;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;
public class Menu extends Sprite {
public static function imageLoaded():void {
startButton.addChild(loader);
//initizlize values for startButton Bitmap
startButton.x = (Main.gameWidth / 2) - (startButton.width / 2);
startButton.y = (Main.gameHeight / 2) - (startButton.height / 2);
}
//create startButton Bitmap
public static var startButton:Sprite = new Sprite();
public static var loader:Loader = new Loader();
loader.load(new URLRequest("lib/menustartbutton.png"));
loader.addEventListener(Event.COMPLETE, imageLoaded);
}
}
By the way, I wait for the loader to successfully load the image before working with it, just in case the image takes more time and it draws errors.

The problem is that you misuse static. all static methods/properties are initialized before the classes themselves. As a result static can receive values but they cannot run any code. Running code has to happen after all classes are ready to go which is not the case when static is initialized. In your case startButton and loader are created correctly but the next line never runs 'loader.load'.
Don't misuse static, you are obviously trying to use static to make you code writing and life easier but at the end because you are misusing it you will always end up with more problems.

Related

Converting ActionScript 3 FLA’s to HTML5 canvas FLA’s in Flash cs6

Hi there,
i have doubt about converting .fla files to html5 canvas. i am developing the project by using Action script version 3 but now i want that project on html5 canvas for the browser compatibility and other issues to be solved..
Anyone help me to solve the issue
I am trying Swiffy Extension and CreateJS tools. But the code was not linking properly because am using action script and also i don't know how to linking files from one to another.. please anyone help me to solve the issues.
Example .FLA code:
package com.privacy_pirates.game {
//import all the Flash libraries we will need
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.Loader;
import flash.events.ProgressEvent;
import flash.net.URLRequest;
import flash.utils.Timer;
import flash.utils.*;
public class Main extends MovieClip{
//all the onjects that are on the stage that will need to be called
public var content_mc:MovieClip;
public var introLoader:Loader;
public var tutotrialLoader:Loader;
public var preloader_mc:Preloader;
public var btn_play:MovieClip;
public function Main() {
// constructor code
//when the class is added to the stage
addEventListener(Event.ADDED_TO_STAGE, addedToStage);
}
public function addedToStage(e:Event):void{
//trace("added to stage is executing");
removeEventListener(Event.ADDED_TO_STAGE, addedToStage);
//the swf that will be loaded
introLoader = new Loader();
//when it's ready, perform the function loadComplete
introLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
//make the preloader
introLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgress);
//the file that will be loaded
introLoader.load(new URLRequest("Animation.swf"));
}
private function loadComplete(e:Event):void{
//trace("load is complete");
//add the file to the stage
addChild(introLoader);
}
//the progress bar at the bottom of the screen
private function loadProgress(e:ProgressEvent):void{
var percent:Number = e.bytesLoaded / e.bytesTotal;
preloader_mc.setPercent(percent);
}
}
}

AS3: Display data(integer) from an swf to another swf through class

I have a "score1.as" class in which it will access a data from an swf and display it into
my "finalscore.fla"...I was able to pass and trace the data successfully into my "finalscore.fla"..But my problem is this: Though I was able to access the data by tracing it, I can't display it to my dynamic text...I thought by simply typing "txtScore.text = ("Score: " + lol1.go() );" would solve the problem but it didn't...Please help..Here's my code..bY the way, I'm using actionscript 3.0..
score1.as:
package
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;
public class score1 extends Sprite
{
private var loader:Loader;
public function Parent()
{
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);
loader.load(new URLRequest("savescore.swf"));//This is the swf where in the data came from
}
public function onLoaded(e:Event):void
{
trace(loader.content['currentScore']);
}
public function go():int{
return loader.content['currentScore'];//This is the data being accessed
}
}
}
finalscore.fla:
var lol1:score1 = new score1();
txtScore.text = ("Score: " + lol1.go() ); // This is where I can't display the data
lol1.Parent();//I successfully traced the data
Check your embedded fonts. You are likely only embedding the characters from the font that were present at compile time. For example, if you have a textfield with "Hello World!", and you wanted to do textfield.text = "Jiggly Jello Lord!" would display as "l ello ord", because only those characters were present in the textfield during compile.
Edit:
I took a closer look at your code. You need to call Parent() before you can get your content. In fact, rename it to be the constructor, and you should be good.
package {
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;
public class score1 extends Sprite {
public var loader:Loader;
public function score1() {
// You need to run this code as your constructor.
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);
loader.load(new URLRequest("savescore.swf"));
}
public function onLoaded(e:Event):void {
trace(loader.content['currentScore']);
}
public function go():int {
return loader.content['currentScore'];
}
}
}
You'd also want to wait for your content to be loaded before accessing the data. For this, you'd need to wait for your onLoaded to fire before trying txtScore.text = ("Score: " + lol1.go() );
Edit 2
onLoaded is how you know if it runs. It gets called when Event.COMPLETE fires. By the same extension, just register for that event from outside your class, or fire an event, or any other solution that completes after the data is loaded.
finalscore.fla:
var lol1:score1 = new score1();
lol1.loader.contentLoaderInfo.addEventListener(Event.COMPLETE, scoreReady);
function scoreReady(e:Event):void {
txtScore.text = "Score:" + lol1.go();
}

AS3 Architecture : How to tween a single instance of an object on multiple calls?

I am new to actionscript and am having a trouble displaying a single instance of an object, using FlashDevelop.
I have a main.as in which I am displaying an image as background. Then I display a rectangle containing some text that tweens as the mouse hovers a target (appearing/disappearing on the stage). The rectangle is in a class TextBox.as .
I know my code is quite messy because it creates a new instance of the rectangle everytime I reach the target (calling the tween). But if I try to switch it around it gives me errors. Also I cannot seem to remove my rectangle (with removeChild()) once it is created, it cannot find the child.
Could anyone indicate me what is the architecture I should use so that only one instance of the rectangle is created?
Here's a bit of my code:
//IMPORT LIBRARIES
import Classes.TextBox;
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import com.greensock.TweenLite;
// Setup SWF Render Settings
[SWF(width = "620", height = "650")]
public class Main extends Sprite
{
//DEFINING VARIABLES
[Embed(source="../lib/myimage.jpg")]
private var picture:Class;
private var myTween:TweenLite;
//CONSTRUCTOR
public function Main():void
{
addChild(new TextBox);
addChild(new picture);
addEventListener(MouseEvent.MOUSE_OVER, appear);
}
//ROLLDOWN FUNCTION
public function appear(e:MouseEvent):void
{
trace("Appear");
var text:TextBox = new TextBox();
addChild(text);
addChild(new picture);
if (picture) {
removeEventListener(MouseEvent.MOUSE_OVER, appear);
//addEventListener(Event.COMPLETE, appearComplete);
myTween = new TweenLite(text, 1, { y:340 , onComplete:appearComplete, onReverseComplete:disappearComplete} );
}
}
Thanks in advance.
i dont know what tweening you want to achieve but you should reuse your textbox instance, e.g.:
import Classes.TextBox;
import com.greensock.TweenLite;
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
[SWF(width = "620", height = "650")]
public class Main extends Sprite {
[Embed(source="../lib/myimage.jpg")]
private var pictureClass:Class;
private var picture:Bitmap;
private var textbox:TextBox;
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);
picture = new pictureClass();
textbox = new TextBox();
addChild(picture);
addChild(textbox);
addEventListener(MouseEvent.MOUSE_OVER, tween);
}
public function tween(e:MouseEvent):void {
removeEventListener(MouseEvent.MOUSE_OVER, tween);
TweenLite.to(textbox, 1, { y:340, onComplete:reverse } );
}
private function reverse():void {
TweenLite.to(textbox, 1, { y:0, onComplete:tweenComplete } );
}
private function tweenComplete():void {
addEventListener(MouseEvent.MOUSE_OVER, tween);
}
}

As3 project with Preloader

I want to use the Preloader provided by FlashDevelop but it does not react as it should.
My loader tells me 100% when no file has been downloaded.
The code should display a trace() containing the percent intermediary but does not
Could you help me?
Mains.as
package
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;
[Frame(factoryClass="Preloader")]
public class Main extends Sprite
{
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
var imgRequest:URLRequest = new URLRequest("http://next-web.be/actionscript/0.jpg");
var img:Loader = new Loader();
img.load(imgRequest);
addChild(img);
}
}
}
Preloader.as
package
{
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.utils.getDefinitionByName;
import flash.display.Sprite;
public class Preloader extends MovieClip
{
private var bar:Sprite;
public function Preloader()
{
if (stage) {
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
}
addEventListener(Event.ENTER_FRAME, checkFrame);
loaderInfo.addEventListener(ProgressEvent.PROGRESS, progress);
loaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioError);
// TODO show loader
bar = new Sprite();
bar.graphics.lineStyle(1, 0x4444ff, 1, true);
bar.graphics.drawRect(0, 0, 100, 6);
bar.x = stage.stageWidth / 2 - bar.width / 2;
bar.y = stage.stageHeight / 2 - bar.height / 2;
addChild(bar);
}
private function ioError(e:IOErrorEvent):void
{
trace(e.text);
}
private function progress(e:ProgressEvent):void
{
// TODO update loader
bar.graphics.lineStyle(0, 0, 0);
bar.graphics.beginFill(0x8888ff);
bar.graphics.drawRect(1, 1, (e.bytesLoaded / e.bytesTotal) * 98 , 4);
bar.graphics.endFill();
trace( "loading:" + (e.bytesLoaded / e.bytesTotal) * 100 );
}
private function checkFrame(e:Event):void
{
if (currentFrame == totalFrames)
{
stop();
loadingFinished();
}
}
private function loadingFinished():void
{
removeEventListener(Event.ENTER_FRAME, checkFrame);
loaderInfo.removeEventListener(ProgressEvent.PROGRESS, progress);
loaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, ioError);
// TODO hide loader
removeChild(bar);
bar = null;
startup();
//stop();
}
private function startup():void
{
var mainClass:Class = getDefinitionByName("Main") as Class;
addChild(new mainClass() as DisplayObject);
}
}
}
You need to register your listener on the contentLoaderInfo property of the Loader you use to download the data (in your case img.contentLoaderInfo).
In your code, you register progress on loaderInfo, which is a field of your Preloader class (inherited from MovieCLip), and will give you the progress on loading the SWF that contains the Preloader class.
public class Preloader extends MovieClip
{
public function Preloader()
{
// This is wrong.
loaderInfo.addEventListener(ProgressEvent.PROGRESS, progress);
// Function `progress` will show the progress of loading your SWF file,
// *not* the JPEG you're loading in class Main.
}
}
You need to register your listener on the contentLoaderInfo property of the Loader you use to download the data (in your case img.contentLoaderInfo).
img.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progress);
Of course, then it will show only the progress of loading the JPEG file.
You'll also need to either pass the Loader object (or just the contentLoaderLinfo) to the Preloader somehow, or include the event handler in your Main class.
After a continued search, I realized my mistake.
Until now, I always use "loader" but this function does not include my pictures to swf file. So I use a library swc allowing me to generate a complete swf.

About embeding assets(images) in AS3 project

I am using flex sdk 4.5.1 and flash develop to compile my AS3 project. I have small images, about 12KB which is silly to load them using Loader class, so embeding is better solution.
However when this line at the top of my Main class is uncomented i get blank swf
[Embed(source = "../assets/gui/play1.png", mimeType = "image/png")]
private var PlayUpImg:Class;
(when i comment it out, the compilated swf is ok)
What could be the error. Why do I get the blank swf?
I am working on pure AS3 project in FlashDevelop
You could try embing the png image as arbitrary binary data by using the "application/octet-stream" mimetype. Then you would use a Loader object's loadBytes() method to load the png image's binary data. The following is an example of this:
package
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
[SWF(width="256", height="256", backgroundColor="#FF0000")]
public class Main extends Sprite
{
[Embed(source = "assets/ie9.png", mimeType = "application/octet-stream")]
private var IE9:Class;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}// end function
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
var loader:Loader = new Loader();
loader.loadBytes(new IE9());
addChild(loader);
}// end function
}// end class
}// end package
[UPDATE]
I know this answer has already been accepted but here's an extra added bonus.
You need to add the image to the Displaylist:
package
{
import flash.display.Bitmap;
import flash.display.Sprite;
public class Main extends Sprite{
[Embed(source = "../assets/gui/play1.png", mimeType = "image/png")]
private var PlayUpImg : Class;
public function Main()
{
var myImage : Bitmap = new PlayUpImg();
addChild( myImage );
}
}
}