Change text from other class with AS3? - actionscript-3

I have some action script in the first layer and it imports a CustomSocket class from an external .as file. When I receive a command on the socket I want to edit the GUI objects. For example, when I get the command TIME 23 from the server I want to change the dynamic text box timerBox to the value 23. I have a method called processCommand(cmd:String) in the CustomSocket class, but I don't know how to change the text. Usually you can just do timerBox.text = "23"; but this doesn't work in an external class?

If 'CustomSocket' is not your document class, then you need to specify some reference for your timeline in your class.
For general example,
While importing a class,
import CustomSocket;
csObj:CustomSocket = new CustomSocket(this);
then, inside class, in constructor
public function LibraryS(_ref:Object):void
{
timelineRef = _ref;
}
then in your class you can say,
timelineRef.timeBox.text = "23";

Related

How to check Custom Element is registered?

Some method creates new instance of my custom element (created with polymer) and attaches it on page. But I want to check is Element registered before add it and print error to console in bad case. I mean what if I forgot import component html declaration:
<!--I forgot write it in my HTML file -->
<!--<link rel="import" href="packages/visualytik/vis_starter.html">-->
So, in case when I forgot import I want to print error in console.
I know one tricky method:
import 'my_custom_component.dart';
Element component = new Element.tag('my-custom-component');
bool registered = component is MyCustomComponent;
But it's hard method because we should create component first and have to import MyCustomComponent.dart in dart file. Can I check it in other way? Something like:
document.isRegistered('my-custom-component');
Update3
You can also use the new #HtmlImport annotation. If you import the class, then you can be sure you also have imported the HTML of the element. See also https://stackoverflow.com/a/29710355/217408
Update2
See Hunting down unregistered elements
Update
Use a custom constructor in your elements class and do the registration there but only if it wasn't done already.
class MyCustomComponent extends ... {
bool _isRegistered;
bool get isRegistered => _isRegistered;
factory MyCustomComponent() {
if(!isRegistered) {
registerElement();
_isRegistered = true;
}
return new Element.tag('my-custom-element');
}
}
and then create new instances like
new MyCustomElement();
and you can always be sure the element is registered only once (but you always need to use this constructor of course).
Original
If you register your elements by calling document.RegisterElement() yourself instead of relying on Polymer for example, you need to hold a reference to the constructor reference document.RegisterElement() returns, otherwise you won't be able to create an instance of the element.
Therefore you just need to check if you already have a reference to the constructor. See also https://developer.mozilla.org/en-US/docs/Web/API/Document/registerElement

In AS3, how do I run code when a when the movie starts?

I'm making a level editor for my game, and would like to be able to access a list of all the classes included in my game. I have a static function in my Main class:
public static function register(c:Class, category:String):void {
if (classRegister[category] == null) {
classRegister[category] = new Array();
}
classRegister[category].push(c);
}
Then, in each class I want registered, I put a static initializer:
{
Main.register(prototype.constructor, "motion");
}
However, the static initializers only get called when the class first gets used. Is there a way for a class to force itself to be used right when the game starts? I'm aware that I could explicitly list all the registered classes in the Main file, but that's suboptimal in that the Main file would have to be edited whenever a new class is added that wants registration.
Thanks,
Varga
List all the class definition in the ApplicationDomain, and filter them based on a naming convention or a type (an interface?).
To achieve this, you can use ApplicationDomain.getQualifiedDefinitionNames() (docs), but only if you target FlashPlayer 11.3+.
As a side note, you MUST reference this class somewhere, as a class field so the compiler knows it must include this class in the SWF. You can also put the classes you want to reference inside a SWC library and use -compiler.include-libraries as compiler setting (in that case I wonder if your static initializers gets called?).

Is it possible to access a class outside package from other package?

I have a class like this
package test{
class Test{}
}
class TestInnerClass{}
I can access the TestInnerClass from Test class but I need to access the TestInnerClass(as class, not its instance) from other class as well. And I don't really want to make TestInnerClass an independent class as all it contains are a few variables.
Is there any way to access the class from outside Test class?
[edit]
More specifically, I need the access for the following code to work.
registerClassAlias("TestInnerClass",TestInnerClass);
If you have a class that would not otherwise be accessed except internally by a public class you may define it as internal.
Internal classes are visible to references inside the current package.
In your example, TestInnerClass is only visible to Test.
Otherwise, to access the class or register class alias it must be defined public in its own .as file.
Packages help to order and classify code in hierarchy. Often, I'll keep Value Object or Data Transfer Objects within their own package, such as:
com.jasonsturges.model.vo
This helps to group smaller model classes together.
If you wanted to make a class visible outside of it's containing package your class would be defined as so:
// SampleCode.as file
package samples{
public class SampleCode {}
}
// CodeFormatter.as file
package samples{
class CodeFormatter {}
}
The SampleCode class is visible whilst the CodeFormatter class is not.
Hope I've answered your question

Procedure to use the result from a textbox inside a popup - ActionScript

I am relatively new to ActionScript (have started with it 2 months ago), and have a little doubt of 'procedure' or 'technique' related to passing information between objects.
I have made a class that Pops-up a window that contains a panel with a textbox and two buttons, one for accepting, other for cancelling. It should work as a prompt in which you enter some text, and then if you like the changes, you accept, else, you cancel and the text you entered is discarded.
The thing I'm not sure how to handle is how to receive the text, once the user presses 'Accept', from the class I want to receive it from.
So, the approach I took is a bit cumbersome: firstly, when launching the popup, I associate with it a function (called onResult() in the code) from the 'class that launches', which will be called after the user presses the 'Accept' or 'Cancel' buttons; secondly, to get the text that the user inserted in the box, I keep a reference to it public from my class.
Please have a look at the code here:
http://pastebin.com/Kmud8rBe
I've also programmed in Android before, and the approach there would be much cleanier, just putting the text result from the popup inside a bundle inside an intent, and receiving it from the launched class. Here, I have to pass functions and such, which I don't like at all (although it works!).
So, my question is, for you ActionScript gurus out there, how would you approach this?
Thanks and regards!
pepillo
good that you created a class for your popup-functionality but why did you make all functions static? normal class with normal methods would be better ... and let the class extend from Sprite so that you can add the instance right to the stage.
package
{
import flash.display.Sprite;
public class Popup extends Sprite
{
public function Popup (label:String)
{
// add text and buttons ...
}
}
}
then you can just say:
var pop:Popup = new Popup("message");
addChild(pop);
and to get the data back after the popup is closed you would do sth like this:
private function onPressedAccept(event:MouseEvent):void
{
var text:String = _label.text;
// dispatch a custom event which saves the text as its data
dispatchEvent(new MyEvent(MyEvent.ACCEPT, text));
// close popup ...
parent.removeChild(this);
// or you would remove the popup in the ACCEPT eventlistener ...
}
listening for accept/cancel:
var pop:Popup = new Popup("message");
addChild(pop);
// add eventlistener
popup.addEventListener(MyEvent.ACCEPT, onAccept);
popup.addEventListener(MyEvent.CANCEL, onCancel);
private function onAccept(event:MyEvent):void
{
trace(event.data);
}
link about creating custom events:
http://www.8bitrocket.com/2007/7/31/Creating-Custom-Events-In-Flash-AS3-ActionScript-3/

Document class silently fails

I have this weird issue while compiling my .fla file : it won't use the Document class. Here is the document class. (note that the parent class EditorPlugin extends Sprite).
package com.myproject.plugins.editor {
import flash.display.MovieClip;
import com.myproject.editor.EditorPlugin;
import com.myproject.editor.tools.EpisodeEditorTool;
import com.myproject.editor.tools.NewTabTool;
import com.myproject.editor.tools.ToolManager;
public class EpisodeEditorPlugin extends EditorPlugin{
public function EpisodeEditorPlugin(){
trace("creating", this);
AddAuth(ToolManager.EDIT_EPISODE_AUTH, ToolManager.EDIT_EPISODE_AUTH, EpisodeEditorTool, ToolManager.EDIT_EPISODE_LABEL);
}
}
}
The weird part is that in the line below, if I use NewTabTool instead of EpisodeEditorTool, the code works just fine, but with EpisodeEditorTool, the Class is not instanciated. There are no warnings or compiling errors, but i don't get the trace. I loaded the resulting .swf, it is not of type EpisodeEditorPlugin, but rather a simple MovieClip (via getQualifiedClassName() and is EpisodeEditorPlugin).
The EpisodeEditorTool and NewTabTool are quite similar even though of different use, but very huge, here are their declarations :
public class EpisodeEditorTool extends JPanel implements ITool{
and
public class NewTabTool extends JPanel implements ITool{
I should add that both class contain no error (at least according to Flash) and have been compiled in other .flas before. the only problem I can see is that EpisodeEditorTool is even more huge(r?) than other ITools.
Does anyone have any idea of how a document class could fail to be applied? And fail silently at that?
Thanks!
The only thing I can really think of is that you're not specifying the name of the Document class correctly inside the Flash IDE. Inside the Properties panel there's a box where you enter the name of your Document class, which you must presumably have used if the Document class works when you rename the class to NewTabTool. I guess you put NewTabTool into that box, then changed the actual class' name, and forgot to change the reference in the Properties panel. I've forgotten that little bit when changing the name of my Document class before, I hope your solution is as simple as that!
debu