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

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);
}
}

Related

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

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.

How do I create an object using code in AS3 and give it a picture as display?

package
{
import flash.events.*;
public class declareImage extends Sprite
{
var ship:Sprite = new Sprite();
public function declareImage()
{
}
}
}
I declared an object.
Now I want to give it a background as a picture from my Compute
Should I use Sprite as data type or something else?
Here is an example. Of course, it's a very simple case (using fixed file name, etc), just to illustrate how to achieve what do you want and provide you the foundation to move on.
A tip, begin a class name with an uppercase letter.
package
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.net.URLRequest;
public class DeclareImage extends Sprite
{
private const IMAGE_URL:String = 'myImage.jpg';
private var ship:Sprite;
private var loader:Loader;
public function DeclareImage()
{
ship = new Sprite();
addChild(ship);
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgressHandler, false, 0, true);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadCompleteHandler, false, 0, true);
loader.load(new URLRequest(IMAGE_URL));
}
private function loadProgressHandler(event : ProgressEvent) : void
{
trace('Loading: ' + Math.round((event.bytesLoaded/event.bytesTotal) * 100) + '%');
}
private function loadCompleteHandler(event:Event):void
{
loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, loadProgressHandler);
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadCompleteHandler);
ship.addChild(loader);
trace('complete');
}
}
}

Correct method of incorporating Starling with Preloader

Hi would like to know the best method of adding a preloader. Then when the content is loaded create an instance of Starling framework. Here is what I have:
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import loaders.Preloader;
import starling.core.Starling;
//main class container
public class Game extends Sprite
{
//preloader class
private var _loader:Preloader;
//starling instance
private var _starling:Starling;
//constructor to initialise game
public function Game():void
{
this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage)
}
private function onAddedToStage(e:Event):void
{
this.removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
_loader = new Preloader();
addChild(_loader);
_loader.addEventListener("loaded_game", onGameLoaded);
}
private function onGameLoaded(e:Event):void
{
trace("game loaded");
_starling = new Starling(Game,stage,null,null,"auto","baseline");
_starling.start();
}
}
This is the Preloader class:
package loaders
{
import flash.events.Event;
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
public class Preloader extends Sprite
{
private var textLoaded:TextField;
public function Preloader()
{
this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(event:Event):void
{
// remove added to stage listener
this.removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
textLoaded = new TextField();
textLoaded.x = 300;
textLoaded.y = 300;
textLoaded.border = true;
textLoaded.autoSize = "center";
addChild(textLoaded);
//loop current load state
this.addEventListener(Event.ENTER_FRAME, loadGame);
this.addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
}
private function onRemovedFromStage(event:Event):void {
this.removeEventListener(Event.ENTER_FRAME, loadGame);
this.removeEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
}
private function loadGame(e:Event):void
{
//vars
var total:Number = parent.stage.loaderInfo.bytesTotal;
var loaded:Number = parent.stage.loaderInfo.bytesLoaded;
textLoaded.text = Math.floor((loaded/total)* 100) + "%";
if (total == loaded)
{
this.removeEventListener(Event.ENTER_FRAME, loadGame);
dispatchEvent(new Event("loaded_game", true));
}
}
}//end of class
}
Is this the best way of doing it or is there a more efficient, practical option? As you can see I am making use of Flash classes before I instantiate Starling? The Preloader is simply a textfield listing what is loaded, this should allow the application to load up faster.
Don't you think that it would make more sense to create an external SWF that preloads your game? It is always a better solution than incorporating a preloader into your main game file.

AS3 I don't understand the different treatment of an extended movieclip class vs extended simplebutton class

I recently discovered the custom classes in actionscript 3. I started using them in my latest project but I find it hard to bend my brain around how it all works.
I created two different classes to test.
One is an extended movieclip called "Persoon" and the other is an extended simplebutton called "SpeakerBtn".
package {
import flash.display.Sprite;
public class Persoon extends Sprite {
public function Persoon(xPos:Number, yPos:Number, naam:String) {
var persoon:Sprite = new Sprite;
persoon.graphics.beginFill(0x000000,1);
persoon.graphics.drawCircle(xPos, yPos, 2);
persoon.graphics.endFill();
this.addChild(persoon);
trace ("hij heet " + naam);
}
}
}
package {
import flash.display.SimpleButton;
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
public class SpeakerBtn extends SimpleButton {
public var snd:Sound;
public var cnl:SoundChannel = new SoundChannel();
public function SpeakerBtn(xp:Number,yp:Number,naam:String) {
var speaker:SimpleButton = new SimpleButton();
speaker.name = naam;
speaker.x = xp;
speaker.y = yp;
speaker.addEventListener(MouseEvent.CLICK, playSnd);
//this.addChild(speaker);
}
public function playSnd (event:MouseEvent) : void {
trace ("ping");
}
}
}
Then I have my main:
package {
import flash.display.MovieClip;
import SpeakerBtn;
import flash.display.SimpleButton;
import Persoon;
public class Main extends MovieClip {
var sp:SpeakerBtn;
var ps:Persoon;
public function Main() {
sp = new SpeakerBtn(50,50,"donna");
addChild(sp);
ps = new Persoon(300,300,"wilf");
addChild(ps);
}
}
}
Persoon wilf works like I expected, displays fine and traces correctly.
SpeakerBtn donna does not display and does not trace correctly. I commented out the addChild in the SpeakerBtn package because if I turn it on, I get the error 1061: Call to a possibly undefined method addChild through a reference with static type SpeakerBtn
I noticed that when I define the x and the y and addChild in Main for the speakerBtn it does work. But I don't want to have to define all that in Main, I want my SpeakerBtn to do all that.
I checked this question but it does not provide me with an answer. Can someone explain to me what is happening, or alternatively link me to a comprehensible tutorial (one not too heavy on techspeak, more like an explain-it-to-me-like-I'm-5-years-old)? Thanks!
Update
I forgot to add a button with the class SpeakerBtn to my library, so there was nothing to display. Fixed that now, and with this code the button does appear on the stage, only the x and y values are not registered and it appears on 0,0. Also, the event playSnd does not trigger the trace and I assume is not working.
Solution
With help of Cherniv's information I came to the following solution for my SpeakerBtn.
Main does this:
package {
import flash.display.MovieClip;
import SpeakerBtn;
import flash.display.SimpleButton;
public class Main extends MovieClip {
var sp:SpeakerBtn;
public function Main() {
sp = new SpeakerBtn("donna", 300, 50);
addChild(sp);
}
}
}
And SpeakerBtn does this:
package {
import flash.display.SimpleButton;
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;
public class SpeakerBtn extends SimpleButton {
private var snd:Sound;
private var cnl:SoundChannel = new SoundChannel();
private var _naam:String;
private var _x:Number;
private var _y:Number;
public function SpeakerBtn(naam:String, xp:Number, yp:Number) {
_naam = naam;
_x = xp;
_y = yp;
addEventListener(Event.ADDED_TO_STAGE, addBtn);
}
private function addBtn (event:Event) : void {
this.x = _x;
this.y = _y;
this.name = _naam;
snd = new Sound(new URLRequest("mp3/" + _naam + ".mp3"));
addEventListener(MouseEvent.CLICK, playSnd);
}
private function playSnd (event:MouseEvent) : void {
cnl = snd.play();
}
}
}
So what I did was add an EventListener for when the button was added to the stage and then set all the variables like x-position, y-position and name.
That's because of inheritance. Your SpeakerBtn doesn't inherits the addChild method from his ancestors , because as we can see in SimpleButton's documentation it is inheritor of DisplayObject and not of DisplayObjectContainer , which do have a addChild method and passes it to all his inheritors including MovieClip and Persoon.

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.