Can't have 2 dynamic text fields AS3 - actionscript-3

This is not a specific situation, this is something that happens a lot to me.
Whenever I have dynamic text (that changes through code, of course) everything works fine.
However, when I add a second Dynamic text, both of them do not show.
An example scenario would be:
textfield1.text="hello";
and on the next frame
textfield2.text="goodbye";
no text would show.
Something is wrong with flash, maybe. The problem starts when I add another textbox to the stage.
I do not want to upgrade to a newer flash, but I could if I have to.
If anybody knows how to fix my problem, please tell me.

I think that embed fonts is optional if you create and add an instance of TextField trough AS3 (excepts if you specify a font that is not present on another computer).
In the example here bellow, the text for textfield1 and textfield2 is always displayed, so I probably misunderstand your question.
Best regards.
Nicolas
PS : the "var timer:Timer" and the callback function are only used to make this example switch an loop from frame 1 to frame 2
Example 1
frame 1:
import flash.utils.Timer;
import flash.events.TimerEvent;
if (! textfield1 && ! textfield2)
{
import flash.text.TextField;
import flash.geom.Point;
var textfield1:TextField = new TextField();
var textfield2:TextField = new TextField();
var tfPosition:Point = new Point(100,50);
}
try{
removeChild(textfield2);
}catch(e:Error){
(trace (" ERROR : textfield2 is not already added"));
}
addChild(textfield1);
textfield1.x = tfPosition.x;
textfield1.y = tfPosition.y;
textfield1.text = "hello";
stop();
function playStop(te:TimerEvent):void{
play();
}
if(!timer){
var timer:Timer=new Timer(1000);
timer.addEventListener(TimerEvent.TIMER,playStop);
timer.start();
}
frame 2 :
removeChild(textfield1);
addChild(textfield2);
textfield2.x = tfPosition.x;
textfield2.y = tfPosition.y;
textfield2.text="goodbye";
stop();
Example 2
If you have two instances of a TextField manually placed on the Timeline, that you select "use device fonts" and the names for the instances are really called "textfield1" on the first frame and "textfield2" on the second frame.
(I've done it by copy and paste in place and didn't get any issue neither).
This works on CS6 too... The text is well displayed
So give us more details please.
Instance of TextField on frame 1 named "textfield1"
code on frame 1 :
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.text.TextField;
textfield1.text = "hello";
stop();
function playStop(te:TimerEvent):void {
play();
}
if (! timer)
{
var timer:Timer = new Timer(1000);
timer.addEventListener(TimerEvent.TIMER,playStop);
timer.start();
}
Instance of TextField on frame 2 named "textfield2"
code on frame 2 :
textfield2.text="goodbye";
stop();

Related

Retrieving percentage of colors of a MovieClip

today my question is as follows:
I have a game, and one of the phases is that you have to pass a "protective cream" on the doll's body. I already have this effect (image below), I'm using an alpha filter combined with a mask that is drawn.
I wonder how can I do to be checking after 20 seconds of the game, if the user has filled 100% of the masks ...
My code is this (forgive me, I am beginner and Brazilian ... any questions ask me):
stop();
import flash.display.Shape;
import flash.events.Event;
import flash.display.BlendMode;
import flash.display.BitmapData;
import flash.utils.Timer;
var tempoFase2:Timer = new Timer(10000, 1);
var corpo_creme:MovieClip = new corpo_mask();
addChild(corpo_creme);
corpo_creme.x = corpo_branco.x;
corpo_creme.y = corpo_branco.y;
setChildIndex(corpo_branco, 1);
setChildIndex(cabeca, 3);
setChildIndex(corpo_creme, 2);
var drawing:Shape = new Shape();
addChild(drawing);
corpo_creme.mask = drawing;
corpo_branco.blendMode = BlendMode.LAYER;
stage.addEventListener(MouseEvent.MOUSE_MOVE,draw);
function draw(e:Event):void {
drawing.graphics.beginFill(0xFFFFFF);
drawing.graphics.drawCircle(mouseX,mouseY,30);
drawing.graphics.endFill();
}
Thank U.
Try this solution:
https://stackoverflow.com/a/15354416/1627055
Basically, you run BitmapData.threshold() against a mask of type BitmapData. You can also draw the relevant portion of your drawing over a temporary BitmapData object and do the same trick, make sure though that your algorithm will count the areas that are not covered by your drawing as filled - you can achieve this by creating the bitmap data pre-filled with white.

AS3 Setup Buttons for All Frames in Document Class

I'm making a simple interface with Flash. Let's say we've got:
frame 1: 1 button that advances to frame 10 (goto10)
frame 10: 2 buttons, one advances to frame 20 (goto20), one opens a URL (openURL)
frame 20: 3 buttons, one goes back to frame 1 (goto1), one goes to frame 10 (goto10), and one opens a URL (openURL)
package {
import flash.display.MovieClip
import flash.events.Event;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.display.SimpleButton;
public class NKE_DocumentClass extends MovieClip {
public var goto1:SimpleButton = new SimpleButton();
public var goto10:SimpleButton = new SimpleButton();
public var goto20:SimpleButton = new SimpleButton();
public var openURL:SimpleButton = new SimpleButton();
public function NKE_DocumentClass() {
goto1.addEventListener(MouseEvent.CLICK,function(self:MouseEvent):void{clickGo(self,1)});
goto10.addEventListener(MouseEvent.CLICK,function(self:MouseEvent):void{clickGo(self,10)});
goto20.addEventListener(MouseEvent.CLICK,function(self:MouseEvent):void{clickGo(self,20)});
openURL.addEventListener(MouseEvent.CLICK,function(self:MouseEvent):void{urlGo(self,"http://google.com")});
}
public function clickGo(event:MouseEvent, nextCue:int):void {
gotoAndStop(nextCue);
trace("Advanced to: " + nextCue);
}
public function urlGo(event:MouseEvent, goURL:String):void {
var request:URLRequest = new URLRequest(goURL);
new URLLoader(request);
trace("Executed URL: " + goURL);
}
}
}
Problem is, once I leave frame 1, none of the buttons work... they're simply unresponsive. It seems like the code doesn't stay loaded once it leaves frame 1.
Thoughts?
I'm pressuming the problem is because when this code is first executed (as a Document Class) the only button that exists is the button on frame 1? This is under the assumption than you've created buttons in the Flash IDE then added them to the stage from the library on the specific keyframes.
I see you've created the SimpleButtons programmatically but since they haven't been added to the stage in the code you've shown, the presumption is that you've just called them the same names as the buttons you've placed on stage? Correct me if I'm wrong and I'll try to offer some other advice if the below doesn't help.
One solution would be to create them all on the first frame then switch their visibility on and off depending on when you need them.
If you're not sure how:
goto10.visible = false;
etc etc
I can't remember now without testing but if you have placed them all on the stage on different keyframes this may cause a problem.
Back in the days of putting code on the timeline if you put code on frame 1 but it referenced objects that weren't on frame 1 then the code would fail (this is probably what's happening with your document class - it's running when not all objects exist).
I would make sure they're all on one layer without any keyframes, from frame 1, and you just switch their visibility on and off. Alternatively, let your classes add and remove the buttons and other interface elements and don't use the timeline at all.

can't add an event listener to a mask AS3 Flash

New to AS3. Trying to do a simple mask exercise, but for some reason when I add event listener to 'myMask', the event doesn't trigger. I tried turning both 'myMask' and 'theMaskee' as sprites and movie clips, but no luck. It does trigger if I don't assign 'myMask' as a mask to 'theMaskee'. It also works if I add the listener directly to the stage, but eventually I want to put many things on the stage, and I'm afraid there will be conflict if it has to listen to the same event but perform several functions... especially if I need them one at a time. I looked through textbooks and the API and mask-related questions other people had, but I can't find anything relating to my specific situation.
(this code is written directly in the timeline)
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.sampler.NewObjectSample;
import flash.events.MouseEvent;
var temp = new backGround();
var myBG:Bitmap = new Bitmap(temp);
temp = new splashMaskee();
var theMaskee:Bitmap = new Bitmap(temp);
var myMask = new MovieClip();
myMask.graphics.beginFill(0x000000, 0);
myMask.graphics.drawRect(0, 0, 800, 600);
myMask.graphics.endFill();
myMask.cacheAsBitmap = true;
theMaskee.cacheAsBitmap = true;
theMaskee.mask = myMask;
addChild(myBG);
addChild(theMaskee);
addChild(myMask);
myMask.addEventListener(MouseEvent.CLICK, myMaskClick);
//stage.addEventListener(MouseEvent.CLICK, myMaskClick);
function myMaskClick(e:MouseEvent):void
{
trace("click");
myMask.graphics.beginFill(0x000000, 1);
myMask.graphics.drawCircle(mouseX, mouseY, 30);
}
Thank you for taking the time
You need to add the listener to theMaskee instead, not your mask.
The mask in AS3 does not implement IEventDispatcher therefore can not catch and dispatch events.
Do this:
theMaskee.addEventListener(MouseEvent.CLICK, myMaskClick);
And it should work. :)
Masks dont take any mouse/keyboard events as it is just a mask and not actually present in the display list.

Actionscript 3 Multiple sounds playing at the same time when accessing different movieclips

I am at the moment trying to create an interactive movie, structured so that each keyframe in the timeline contains a movieclip navigated to using buttons inside the movieclips, with the appropriate code inside the main timeline.
The problem right now is that when you access the movieclip inside frame 3, the sound from frame 2 also plays simultaneously. After doing some research i found that this appears to be a bug with flash itself, and most of the time is dealt with using SoundMixer.stopAll();. Sadly, i have no idea how to use it to kill the sound from frame 2 when only frame 3 is accessed.
I know that when accessing frame 2 instead, only frame 2 is played, which should mean that flash basically goes through all frames on the way to the frame you are supposed to go to.
This is the limited code i am using at the moment:
Frame 1:
import flash.events.MouseEvent;
import flash.display.SimpleButton;
import flash.media.SoundMixer;
stop();
var soundVar:int = 0;
var Choice1F:SimpleButton;
var Choice1R:SimpleButton;
this.Val1.Choice1F.addEventListener(MouseEvent.CLICK, function(me:MouseEvent):void{buttonHandler(me, 2)});
this.Val1.Choice1R.addEventListener(MouseEvent.CLICK, function(me:MouseEvent):void{buttonHandler(me, 3)});
function buttonHandler(e:MouseEvent, Value:int): void{
SoundMixer.stopAll();
soundVar = Value;
this.gotoAndPlay(Value);
}
Frame 2:
import flash.media.SoundMixer;
stop();
if(soundVar == 3){
SoundMixer.stopAll();
}
Frame 3 simply contains a stop(); statement. The code in frame 2 was a futile attempt from me to make it kill the sound on its way to frame 3. Hopefully, you guys can think of a better solution, if one even exists.
The correct structure of the project assumes that you control the playback of music and sounds with a special instance of custom class. And timeline you use only gave him command when and what to do.
One SoundChannel and couple of Sound's will do the trick.
You could use this one
package src.utils{
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
public dynamic class BackgroundMusicPlayer extends Object{
public var playlist;
public var sndChannel;
private var ID;
public function BackgroundMusicPlayer(srcList:Array){
playlist=[];
for(var i=0;i<srcList.length;i++){
var src= new URLRequest(srcList[i]);
var newSound = new Sound(src);
playlist.push(newSound);
}
}
public function playMusic(id){
if (sndChannel!=undefined) {
sndChannel.stop();
}
sndChannel = playlist[id].play();
ID=id;
sndChannel.addEventListener("soundComplete",replayListener);
}
public function replayListener(e){
sndChannel = playlist[ID].play();
}
}
}
import class to you timeline, create instance passing him files list
var musicPlayer = new BackgroundMusicPlayer("music1.mp3","music2.mp3");
And then you want start some sound, call
musicPlayer.playMusic(0);
If you want use imported to project sounds, just share them to actionscript, give them class names and slightly modify given class constructor
public function BackgroundMusicPlayer(srcList:Array){
playlist=[];
for(var i=0;i<srcList.length;i++){
playlist.push(srcList[i]);
}
}
So your instance creation now should be
var musicPlayer = new BackgroundMusicPlayer(new MySound1(),new MySound2());

Typewriter effect with AS3

I'm trying to create a typewriter effect with AS3.
I read tutorials the hole day, but can't find, what I'm looking for....
Perhaps you can help me. - please
That's what I want:
- a typewriter text effect
- the speed can be set
- no import from an external .as file
- no import from an external .txt file (the text should be defined with a variable)
- if the textfield is full of text, it should be "scroll" down....it should jump down one line, so that theres a new empty line, where the typewriter could write....
could you actionscript gurus help me?
I always worked with as2 and it's very hard for me to get a solution in as3.. :(
thanks a lot!
Ok that sounds simple what you have is good.
firtst create the textfield that will display the final text. What you did next is adding all charackters at once, but what you want is adding each charackter after a time.
try something like:
import flash.events.TimerEvent;
import flash.text.TextField;
import flash.utils.Timer;
// the textfield guess you will add this on timeline instead of coding it...
var myTextField:TextField = new TextField();
// this is the text that should be displayed tywriterstyle
var typewriterText:String ="Hello World Typewriter";
// Charackter count and timer for timedelay between each upcoming charackter
var counter:int = 0;
var delayTimer: Timer = new Timer(300);
// starts Timer
delayTimer.addEventListener(TimerEvent.TIMER, addCharackter);
delayTimer.start();
private function addCharackter( E:Event = null ):void{
// get a single Charackter out of the String
var charackterToAdd:String = typewriterText.charAt(counter);
// add the charackter to the Textfield
myTextField.text.append(charackterToAdd);
counter++;
// if you reached the end of the String stop Timer
if(counter == typewriterText.length){
delayTimer.stop();
}
}
For text animation you can use flupie.
I think it's a better way to do.
See also this and this.
If you are a watch&learn guy this would be much convenient to you.