Function in Sprite does not run to display - actionscript-3

Function X1 simply does not run- no trace result and programme so basic.
Board.as is called- I checked.
Simple sprite display not working.
Main.as
package
{
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.*;
import flash.text.TextField;
import flash.ui.Keyboard;
import Start;
import Board;
/**
* ...
* #author Michael
*/
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init():void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
var Board1:Sprite = new Board();
stage.addChild(Board1);
Board1.visible = true;
var Start1:Sprite = new Start();
Start1.x = 32;
Start1.y = 32;
addChild (Start1);
stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);
function myKeyDown(e:KeyboardEvent):void{
if (e.keyCode ==Keyboard.SPACE)
{
removeChild(Start1);
Start1 = null;
}
}
}
Board.as
package
{
import flash.display.Bitmap;
import flash.display.Graphics;
import flash.display.Sprite;
/**
* ...
* #author Michael
*/
public class Board extends Sprite
{
[Embed(source="../lib/Board.jpg")]
private var BoardClass :Class
public function X1():void
{
var boardclass:Bitmap = new BoardClass () as Bitmap;
trace("Project is running fine!");
this.addChild(boardclass);
}
}
}

You are not calling the function X1(). Change the code part in your Main.as class where you create the Board to this:
var Board1:Sprite = new Board();
Board1.X1();
stage.addChild(Board1);
A few more tips for your code:
1) You don't need Board1.visible = true;, it's visible by default
2) Change the name of Board1 to board1 or just board. It's a standard to call classes with first capital letter.
EDIT:
If you want X1() to be run as you create the object, call this function in the constructor of Board.as. Constructor is a function that is run when you create an object. For Board.as it would be like this:
public function Board():void
{
X1(); // this function will be called when you create a new Board object
}

Related

embedding swf files in flashdevelop and accessing the symbols/buttons

in trying to access a button symbol in an swf file which i embedded into the mianmenu class called PlyBtn but i always get errors and im not sure if im enbedding it right or using the right code to find it within the swf file,
//heres my mainmenu class
package
{
import flash.display.Bitmap;
import flash.display.MovieClip;
import flash.display.Sprite;
/**
* ...
* #author Andrew Dean
*/
public class MainMenu extends MovieClip
{
[Embed(source = "GameMenu.swf")]
public var MainM:Class;
public function MainMenu()
{
var MMenu:MovieClip = new MainM() as MovieClip;
addChild(MMenu);
}
}
}
main class
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
/**
* ...
* #author Andrew Dean
*/
public class Main extends Sprite
{
public var MMenu:MainMenu
public function Main()
{
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
MMenu = new MainMenu;
stage.addChild(MMenu);
addEventListener(MouseEvent.CLICK, startgame);
}
public function startgame(e:Event):void
{
if (e.target == MMenu.PlyBtn)//problem
{
}
}
}
}
Export you swf as a swc and then add it to your flash develop project as a library.
Not only will you have a better workflow, you will also get intellisense code completion for your included art classes.
In flash, just make sure that the symbols you want to use have a class name and set for export in frame 1.

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 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 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.

#2015: Invalid BitmapData

In my class, if I create bitmapData like this:
private var tImage:BitmapData;
public function object():void {
tImage = new BitmapData(30,30,false,0x000000);
}
I get the following error:
ArgumentError: Error #2015: Invalid BitmapData.
But if I declare the variable inside the method:
public function object():void {
var tImage:BitmapData;
tImage = new BitmapData(30,30,false,0x000000);
}
It works fine. WHY!?!?! It's driving me crazy.
Thanks guys!
I think it might be some other code in your class.
The following works, but I didn't name the function "object" (since I'm guessing that's a reserved word??)
package
{
/**
* ...
* #author your name here
*/
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Bitmap;
public class TestBitmap extends MovieClip
{
private var tImage:BitmapData;
public function TestBitmap():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
tImage = new BitmapData(30,30,false,0x000000);
}
}
}
This simplified version below also works too:
package
{
/**
* ...
* #author your name here
*/
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Bitmap;
public class TestBitmap extends MovieClip
{
private var tImage:BitmapData;
public function TestBitmap():void
{
tImage = new BitmapData(30,30,false,0x000000);
}
}
}
You declared tImage as private...
private var tImage:BitmapData;
public function object():void {
tImage = new BitmapData(30,30,false,0x000000);
}
Its should be
var tImage:BitmapData;
public function object():void {
tImage = new BitmapData(30,30,false,0x000000);
}
Derp