Need help with Classes - actionscript-3

i am new in ActionScript 3/ Flash CS4, i am learning classes in ActionScript(OOP). I have in my folder two files - Pro.fla (my flash project) and .as (Pro.as). I created class in Pro.as:
package{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
public class Pro extends MovieClip
{
private var button:my_button=new my_button(); // is the button from the library (MovieClip), which has been linkage Base class: Pro and class: my_button
public function Pro()
{
button.x+=2050;
}
}
}
when i ctrl+enter, i have a mistake: Error: Error #1023: Stack overflow,
What is the problem, can anyone help me with that? Thank you in advance!

It looks like you have a recursion issue. In your comment, you mentioned that "my_button" has a base class of "Pro". I'm guessing "Pro.as" is your Document class and not the base class of your button. Right now, because Pro is the base class of the button, and you're creating the button within Pro, it's creating an endless loop.
If you haven't created a base class for the button, you probably want to set it to "flash.display.Sprite" or "flash.display.MovieClip" to have it extend either Sprite or MovieClip.

The base class of the button should be flash.display.SimpleButton (or MovieClip)

Related

AS3 button labels won't change

I'm working with flash CS6 and AIR, I drew a movieclip with some buttons and exported for actionsript, then I wrote down some code that will make some buttons change label. No errors, no warnings, everything works excepts for labels. How can I fix it?
package{
import flash.display.Sprite;
public class Downloader extends Sprite{
public function Downloader() {
BTNplay.label = 'play'; //this does not work
BTNplay.y=5; //this works
}
}
}
The fact is that I wrote something similar on the main class and it works! How is that possible?
Did you maybe mean this?
BTNPlay.label.text = 'play';
// ^^^^^

How to implement custom google search for ActionScript 3.0

I was trying to implement custom google search in my course-ware that developed in flash. I define a class named 'Main' (Main.as) and put my search code there. But the problem is, that Main class has a conflict with the other codes containing in my course-ware (i've combo box & other basic navigation in course-ware). i have no idea how to resolve it. is there any way to put that code in timeline layer? help please.. thanks. here is my Main class:
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.net.navigateToURL;
import flash.net.URLRequest;
public class Main extends Sprite
{
public function Main():void
{
searchButton.addEventListener(MouseEvent.MOUSE_UP, google);
addEventListener(KeyboardEvent.KEY_DOWN, google);
searchTerms.addEventListener(MouseEvent.MOUSE_DOWN, selectText);
}
private function google(e:*):void
{
if(e.type == "mouseUp")
{
navigateToURL(new URLRequest("http://www.google.com/search?q=" + searchTerms.text));
}
else if(e.keyCode == Keyboard.ENTER)
{
navigateToURL(new URLRequest("http://www.google.com/search?q=" + searchTerms.text));
}
}
private function selectText(e:MouseEvent):void
{
searchTerms.setSelection(0, searchTerms.length);
}
}
}
From what you shared & the messages here, I assume you are trying to add code through the flash IDE while at the same time having a document class called Main, for your application.
There are lots of ways you may get around this.
Assuming you want keep your timeline code unaltered while adding an instance of the Main class :
Add an empty movie clip to the library, say SearchClass.
Go to properties of the movie clip & click export for actionscript.
Set the class to Main. Do make sure where Main.as lies outside with respect to the fla.
Add this movieclip onto the stage, on any frame or layer.
Do remember to clear the document class field.
As a side note, You should also rename the class Main to something meaningful, like SearchClass.
If you wonder about setting the class vs base class,
We use base class only when you wish to extend the features of the class (by adding UI elements for eg).
You could also simply call the class directly from the timeline code as :
var main:Main = new Main();
addChild(main);
Just make sure the Main.as file lies next to the fla... ie make sure the path is available to compiler.

Access the main timeline in a swc

I have some animation that was created in the main timeline in an FLA
I want to publish this as a SWC for importing via Flash Builder.
However- I can't find a way to do this. Any ideas?
The only workaround I know of so far is to copy all the frames into a movieclip symbol and then set that to "export for actionscript" This isn't ideal since then I need to reposition the entire animation so it's at 0,0- and since there are objects moving outside the bounds of the stage, etc. I'll have to do it by eye. Not ideal :\ so a better solution is much appreciated!
You could embed a .swf
package {
import flash.display.MovieClip;
import flash.display.Sprite;
public class Demo extends Sprite{
[Embed (source="test.swf")]
private var Test : Class
public function Demo() {
var test : MovieClip = new Test() as MovieClip;
addChild(test);
}
}
}

What I might be doing wrong in this code?

I am relatively new to flash. I am trying to create a square grid and add it to the movie. When I open the actionscript panel by pressing F9 and when I type the following code,
var square:SquareClip = new SquareClip();
addChild(square);
Things are working fine (the squareclip is appearing in the movie).
Instead when I do this however, I deleted the above code and just create a new instance of Main,
new Main
and inside Main.as
package{
//----
//IMPORT
//
import flash.display.*;
import flash.events.*;
import flash.text.*;
//Class creation
public class Main extends MovieClip {
//PROPERTIES
public function Main():void {
layout_in_grid();
}
private function layout_in_grid():void{
trace("layout_in_grid");
//create a new Square
var square:SquareClip = new SquareClip();
addChild(square);
trace("Square added");
}
}
}
And when I run the code, my square is not coming. I am doing something wrong very basically. Please help me.
You need to add Main to the displaylist:
var myMain : Main = new Main();
addChild(myMain);
You could also set Main as your document class.
#Mattias is correct. But you should set this as the Document Class as he suggested - When you've selected the stage, in the properties there will be an input box allowing you to enter the name of the class.
If your file is in the same location as the FLA and called 'Main.as' you enter in the box:
Main
If the file is within a folder structure e.g. com/company/projects/Main.as - enter:
com.company.projects.Main
--
Kudos on learning the OOP way!

Access main stage from class definition file (as3)

I'd like to access the stage of the main timeline from w/i a class that extends a movieclip. Basically, I have a button in the main timeline that makes a HUD appear. The HUD is an extended MovieClip class. When people click on a button in the HUD, I'd like to remove the object from the stage of the main MovieClip.
#curro: I think your confusion may come from the fact that I am running this code from a class definition file. Clicking on a button w/i this object should remove it from the DisplayList of the MainTimeline. Here's the code from the class definition file:
package classes {
import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Answers extends MovieClip {
public function Answers(){
listen();
}//constructor
//initiatlize variables
public var answersArray:Array = new Array();
private function listen():void {
submit_btn.addEventListener(MouseEvent.CLICK, function(e:MouseEvent){
answersArray.push(answer_txt.text);
e.currentTarget.parent.parent.stage.removeChild(this);
});//listen
}//listen
}//class Definition
}//package
trace(e.currentTarget.parent.parent) gets me the MainTimeline, and trace(e.currentTarget.parent.parent.stage) appears to return the main stage, but I cannot use removeChild w/o getting an error that I am trying to coerce the stage to be a DisplayObject (which it ought to be).
What's on the stage of the MainTimeline: A single button that, when clicked, adds an instance of the Answers class to the stage.
What's part of the Answers class that's not in the code?
I first created Answers as a MovieClip object in the main library. It has 3 parts:
a TextField named "answer_txt"
a "clear_btn" that clears the answer_txt
a "submit_btn" that submits the text of answer_txt and then removes the entire Answers object from the MainTimeline (at least, that's what I want it to do).
your class definition is really weird. Looks like a mixture of as2 and as3.
Try with this:
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.text.*;
import fl.controls.Button;
public class Answers extends MovieClip
{
public var answersArray:Array = new Array();
public function Answers()
{
submit_btn.addEventListener(MouseEvent.CLICK, remove);
}
private function remove(e:MouseEvent)
{
answersArray.push(answer_txt.text);
this.parent.removeChild(this);
}
}
}
This works on my computer. Your code doesn't. I think it has something to do with the listen method. The class isn't still instatiated and you are making it work.
Hey, I can't make head or tail from the code. Where does submit_btn come from? Is it a property of the class? What about answer_txt?
You don't need to access e.currentTarget... to remove "this" simply:
this.parent.removeChild(this);
If you add that movieclip to the stage then you can access the stage from that class as simple as in the document class
stage
Otherwise you can't access stage from that class. But you can access it by sending the stage as argument when instantiate the class.