ActionScript 3.0 Dynamic Text disappearing with button event - actionscript-3

So, I am having an issue with ActionScript 3.0. I double checked, and ensured that there is nothing missing. However, I am coming up with an interesting result.
I have a dynamic text, which has an instance of varMoney. Also have a button instance called btnTen_percent.
When, I click the button; it should update the text saying something else. Instead it simply disappear.
public function startShop():void {
btnTen_percent.addEventListener(MouseEvent.CLICK, tenPercent);
}
private function tenPercent(e:MouseEvent):void {
trace("10 percent");
varMoney.text = "1000";
}
So I was wondering, where did it went wrong?

Related

Actionscript 3: Changing frame in main timeline from package?

I have a class called "airport" which extends movieclip. I have, successfully, managed to add a mouseclick-eventlistener for every object of the class that i create.
When I click on one of the objects, I am supposed to enter frame 2 on the main timeline.
I've tried to use the following code inside the class:
this.addEventListener(MouseEvent.CLICK, clickHandler);
private function clickHandler(evt:MouseEvent):void
{
MovieClip(parent).nextFrame();
}
When I click on one of the objects, I don't enter frame 2, but the whole screen goes blank. Any ideas? Thanks for advice.
PS: If you request more of my code to provide an answer, I would be happy to provide it.
Whatever 'this' is isn't a direct child of the main timeline. Try this:
private function clickHandler(evt:MouseEvent):void {
MovieClip(root).nextFrame();
}
Oh..I just found the error. In my code I used the line
this.parent.visible = false
to hide the objects when I entered next frame.
What I did not know was that this also would hide every other movieclip, which results in the screen beeing totally white.

Applying action to a button in one frame makes it apply to whole movieclip in Flash CS6

I have a movieclip with several layers and frames. Some of those contain buttons. When I click button 1 I want it to go to the next frame. When I click button 2 I want it to go to frame 23. This is the code I'm using for button 1:
this.addEventListener(MouseEvent.CLICK, convnext);
function convnext(evt:MouseEvent):void
{
MovieClip(parent).gesprekkencivcen.nextFrame();
}
and this is the code for button 2:
this.addEventListener(MouseEvent.CLICK, convend1);
function convend1(evt:MouseEvent):void
{
MovieClip(parent).gesprekkencivcen.gotoAndStop(23);
}
What happens now is that when I click either of the buttons, or in fact anywhere inside the movieclip (even layers I haven't applied actions to) it executes both functions at the same time so I end up going to frame 24. Can somebody provide an answer to this problem?
Obviously this in both cases refers to the same object, and not to the buttons in particular. Record the names of those buttons as you've named their instances on the timeline, say button1 and button2 and write the code employing those names.
button1.addEventListener(MouseEvent.CLICK, convnext);
function convnext(evt:MouseEvent):void
{
parent.parent.gesprekkencivcen.nextFrame();
}
button2.addEventListener(MouseEvent.CLICK, convend1);
function convend1(evt:MouseEvent):void
{
parent.parent.gesprekkencivcen.gotoAndStop(23);
}
With this, however, you will need to update the link to gesprekkencivcen in both listeners, as those buttons will have this as parent, and their target apparently is not a child of this. I have tried to plainly set a call to parent.parent.gesprekkencivcen, which might not work.

flex setFocus on creation complete

Normally, forms and pop Ups don't have the focus set when they are just displayed. The obvious solution was to set the focus to the first input in the creation complete event of the component, so the keyboard short Cuts like tab and space start working.
The problem is that, creation complete is not the panacea, sometimes the element is not focus-able at that point, and i am not sure why that happens.
The render event would ensure the focus, but it dispatches too much for a very simple purpose.
In which point a component is always ready to be focus-able?
Edit: The component giving me trouble to get start up focus, is a TitleWindow, which can be poped in 2 ways, a Mouse click event and a keyboard event.
When the tite window is displayed by a click, the first input gets focus in the creation complete event, but when displayed by a keyboard event, it doesnt...
By now i got it working with the following code:
private function titlewindow_creationCompleteHandler(e:FlexEvent):void{
callLater( setTextInputFocus);
}
private function setTextInputFocus():void{
txtPregunta.setFocus();
}
But doubt the way is shown has anything to do with this... because, some other TitleWindow are displayed this way too and they're fine.
So what could it be?
The render event would ensure the focus, but it dispatches too much for a very simple purpose.
If this is true then why not try this:
private function titlewindow_creationCompleteHandler(e:FlexEvent):void{
var callback : Function = function(re : Event) : void {
titlewindow.removeEventHandler(RenderEvent.orsomething, callback);
setTextInputFocus();
};
titlewindow.addEventHandler(RenderEvent.orsomething, callback);
}
Might be kind of a hack since it should be focusable on creationComplete but it would probably work.

actionscript 3 event.target

I have a movie clip named button1 and in this movie clip
there is a dynamic text named txt
public function mouse_down(event:MouseEvent)
{
if(event.target==button1)
{
...//this only recognizes when i click the button without intersecting the dynamic text area
}
if(event.target==button1||event.target==button1.txt)
{
...//this works
}
i would like to know why it dosen't recognize clicks made in the area that contains the dynamic click if i don't specify it, because txt is part of button1, so normally i would only need to check if the target is button1 but it dosen't work:i also have to check if the target is button1.txt
Thanks for your help!
event.target always points to the object the event originated from, even if it is nested in the object you added the listener to. Use event.currentTarget instead.
Check out this blog post to learn more.

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/