I have a stage set up with a "movie clip" in the frame named jackpotNote. In that movie clip (or symbol), there is a dynamic text field named "jackpotAmt".
What is supposed to happen is that I'm to press the Space Bar on my keyboard and it will trigger the Movie Clip to play out. What's also supposed to happen is that the text is supposed to change as well, here is the current coding:
if (event.keyCode == Keyboard.SPACE) {
if (jackpotNote.visible == true) {
trace("Jackpot amount already shown. Ignored.");
} else {
jackpotNote.visible = true;
jackpotSymbol.visible = true;
//jackpotSymbol.jackpotAmt.text = currency_str + String(jackpotamt_int);
if (jackpotamt_int < 100000) jackpotSymbol.jackpotAmt.text = currency_str + String(jackpotamt_int);
else if (jackpotamt_int >= 100000 && jackpotamt_int <= 999999) jackpotSymbol.jackpotAmt.text = String(jackpotamt_int);
else jackpotSymbol.jackpotAmt.text = currency_str + "1MIL+";
dingsound.play();
jackpotSymbol.play(); // DO NOT START PLAYING
jackpotSymbol.gotoAndPlay(1);
trace("Jackpot amount shown.");
}
}
However when I trigger this event, the following happens:
The sound effect jackpotSymbol.play() plays as normal.
The jackpotSymbol becomes visible.
However, the text is not changed in the following line: jackpotSymbol.jackpotAmt.text = currency_str + String(jackpotamt_int);
This used to run well on Adobe Flash Professional CS6, however when I upgrade to Adobe Animate CC... it broke. (I had to remove all of the TLF fields and make them dynamic again.)
What is missing to make all of this work?
Related
I'm trying to create a new keyboard somehow, for educational purposes.
I've written this code using actionscript 3.I've created an input text field (named it t1) .when the user presses q button on keyboard(which has an ASCII aquals 81 ) I want the letter b to be printed out on the text field so i've written this code :
stage.addEventListener(KeyboardEvent.KEY_DOWN, pressing);
function pressing(event:KeyboardEvent):void
{
//trace(event.keyCode);
if(event.keyCode==81)
t1.replaceSelectedText("b");
}
the problem was that the method replaceSelectedText prints the tow letters on the screen(q&b) which method can i use instead?
Any help would be appreciated.
When using the replaceSelectedText method, you first need to select the text you want to replace. This can be done with the "setSelection" method. This from the adobe help website:
setSelection(beginIndex:int, endIndex:int):void
"Sets as selected the text designated by the index values of the first and last characters, which are specified with the beginIndex and endIndex parameters."
At the moment, since you don't have any text selected, it appears to just be adding the text "b" as it's replacing nothing. Therefore, you should try first selecting the "q".
Alternatively, you can just use a different method. from the adobe help website:
replaceText(beginIndex:int, endIndex:int, newText:String):void
"Replaces the range of characters that the beginIndex and endIndex parameters specify with the contents of the newText parameter."
This would cut out an extra line of code.
I haven't actually done this myself, so if that doesn't work, here's the link to the adobe help page for Text Fields: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/text/TextField.html
I think that to do what you are looking for ( replace a char when it's typed ), a KeyboardEvent.KEY_DOWN is not enough, because when that event is fired, the text is not yet changed, so any change that you did in its handler to your text field will not cancel the insertion of the current typed char. Also, using KeyboardEvent.KEY_UP ( in addition to KeyboardEvent.KEY_DOWN ) will not resolve the problem, because you can fire n times a KeyboardEvent.KEY_DOWN event with the KeyboardEvent.KEY_UP event fired once !
So, I think that the best event that can do the job is the Event.CHANGE event which is fired every time the text of your text field is changed, so you can do like this :
// is there a char to replace ?
var replace_char:Boolean = false;
// the position of the char that we want to replace
var char_position:int = -1;
var text_input:TextField = new TextField();
text_input.type = 'input';
text_input.border = true;
text_input.addEventListener(Event.CHANGE, onTextChange);
function onTextChange(e:Event):void {
if(replace_char && char_position >= 0){
text_input.replaceText(char_position, char_position + 1, 'b');
replace_char = false;
}
}
addChild(text_input);
stage.addEventListener(KeyboardEvent.KEY_DOWN, _onKeyDown);
function _onKeyDown(e:KeyboardEvent):void {
if(e.keyCode == 81) {
replace_char = true;
char_position = text_input.selectionBeginIndex;
}
}
EDIT :
To use a list of keys and their equivalents, you can use an object to stock your keys like this :
// list of all keys (chars) and their equivalents
var chars:Object = {
81: 'b', // q => b
83: 'v', // s => v
68: 'c' // d => c
// other chars
}
var char_to_replace:int = -1;
// other instructions
function onTextChange(e:Event):void {
if(replace_char && char_position >= 0 && char_to_replace >= 0){
// get the equivalent of the pressed key from chars object using : chars[key_pressed]
text_input.replaceText(char_position, char_position + 1, chars[char_to_replace]);
replace_char = false;
}
}
// other instructions
function _onKeyDown(e:KeyboardEvent):void {
if(chars[e.keyCode]) {
replace_char = true;
// save the last pressed key to get its equivalent, or save this last one directly, to replace it next
char_to_replace = e.keyCode;
char_position = text_input.selectionBeginIndex;
}
}
Hope that can help.
I have a song chopped up into 4 pieces: intro, A, B, ending. I have a program that plays the intro, then the A two times then B two times then back to A again and keeps repeating AABB until I press the ending button. If the ending button is pressed, it finishes the currently playing song piece, plays the ending and stops. It works like a charm except there is a very noticeable, irritating delay in the transition between the song pieces. How can I get rid of the delay and make it play the song pieces right after the previous piece ends? (I'm using actionscript 3.0 in flash cs6, the song pieces are in mp3 format, imported into the library).
Here's the program:
https://dl.dropboxusercontent.com/s/6f9w906x9uja683/Loop%20music%20test.swf
And here's the code I'm using:
import flash.events.MouseEvent;
stop();
//variables
var track_var: Number = 0;
var music_cnl:SoundChannel = new SoundChannel();
var intro_msc:Sound = new intro_mp3();
var a_msc:Sound = new A_mp3();
var b_msc:Sound = new B_mp3();
var outro_msc:Sound = new ending_mp3();
//stopping the movieclips
a_mc.stop();
b_mc.stop();
ending_btn.stop();
//intro
music_cnl = intro_msc.play();
track_var= 1;
intro_mc.gotoAndStop(2);
music_cnl.addEventListener(Event.SOUND_COMPLETE, playnext_fn);
trace("debug:track value set to",track_var);
//track selector
function playnext_fn(event:Event){
trace("debug:playnext_fn called");
switch(track_var){
case 0:
//stop
music_cnl.stop();
intro_mc.gotoAndStop(1);
a_mc.gotoAndStop(1);
b_mc.gotoAndStop(1);
ending_btn.gotoAndStop(1);
trace("debug:track value set to",track_var);
ending_btn.removeEventListener(MouseEvent.MOUSE_UP, ending_fn);
break;
case 1:
//first A track
track_var=2;
intro_mc.gotoAndStop(1);
b_mc.gotoAndStop(1);
a_mc.gotoAndStop(2);
music_cnl= a_msc.play();
music_cnl.addEventListener(Event.SOUND_COMPLETE, playnext_fn)
trace("debug:track value set to",track_var);
break;
case 2:
//second A track
track_var=3;
music_cnl= a_msc.play();
music_cnl.addEventListener(Event.SOUND_COMPLETE, playnext_fn);
trace("debug:track value set to",track_var);
break;
case 3:
//first B track
track_var = 4
a_mc.gotoAndStop(1);
b_mc.gotoAndStop(2);
music_cnl= b_msc.play();
music_cnl.addEventListener(Event.SOUND_COMPLETE, playnext_fn);
trace("debug:track value set to",track_var);
break;
case 4:
//second B track
track_var=1;
music_cnl= b_msc.play();
music_cnl.addEventListener(Event.SOUND_COMPLETE, playnext_fn);
trace("debug:track value set to",track_var);
break;
case 5:
//ending
a_mc.gotoAndStop(1);
b_mc.gotoAndStop(1);
intro_mc.gotoAndStop(1);
ending_btn.gotoAndStop(2);
music_cnl=outro_msc.play();
track_var=0;
music_cnl.addEventListener(Event.SOUND_COMPLETE, playnext_fn);
trace("debug:track value set to",track_var);
break;
}
}
//ending button
ending_btn.addEventListener(MouseEvent.MOUSE_UP, ending_fn);
function ending_fn(Event:MouseEvent){
track_var=5;
ending_btn.gotoAndStop(2)
trace("debug: ending button pressed|","track value set to",track_var);
}
I don't see a problem with your code (other than the redundant and incorrect syntax regarding
MAX_BUFFERSIZE, but I think this just looks like remnants of trying to resolve the problem!)
What I think is more likely to be causing the problem is your source audio - if it's imported as an MP3, or with a bitrate of 48 this can cause problems with synching.
You can't get rid of the delay using traditional Sound object whether from library, loaded externally, mp3, wav, any bite rate. The Flash Sound API suffers from a bad design and latency is inevitable when playing sounds.
However in your case you might be abe to get away with it by using the SampleEventData and mix together all your tracks. The initial latency can go up to 1000ms but in your case that might be irrelevant since the sync between sound is more important:
http://help.adobe.com/en_US/as3/dev/WSE523B839-C626-4983-B9C0-07CF1A087ED7.html
When playing Sounds in Flash in ANY WAY the latency (the time it takes for the sound to start playing) can go from 60ms to 400ms depending on the system. The recommended industry standard is 30ms making Flash an obsolete technology for any Sound related applications.
I'm trying to test some strings but trace outputs nothing. I was using Flash CS6, then I installed Flash CC then everything was fine. But then I created another loop now the same thing. Here's my code:
for (tempNum = lastCharNum; tempNum <= 7; tempNum++) {
trace(tempNum); // this outputs nothing
if (arr[tempNum] != String.fromCharCode(9)) {
firstCharNum = tempNum;
trace(firstCharNum); // this outputs nothing
}
}
I tried:
for (tempNum = lastCharNum; tempNum <= 7; tempNum++) {
txt.text = String(tempNum); // this doesn't make change to the textfield
if (arr[tempNum] != String.fromCharCode(9)) {
firstCharNum = tempNum;
txt.text = String(firstCharNum); // this doesn't make change to the textfield
}
}
Same thing, nothing happens.
Make sure "Omit Trace Statements" is not checked in the the publish settings. If its not checked, try File>Publish and Control>Test Movie>In Flash Professional to see if you get different results.
Ok I took some time and figured out to my surprise.
and unfortunately I couldn't just use standard arrow keys. Im making a simulator of a label maker and it has to work to the letter, arrow keys, and everthing.
var boop = textSelect.text.length;
var snoop = boop;
bbbutton.addEventListener(MouseEvent.CLICK, backBtns);
function backBtns(event:MouseEvent):void
{
snoop -= 1;
stage.focus = textSelect;
textSelect.setSelection( snoop,snoop);
}
You can accomplish this by using a textField's caretIndex property.
Assuming textSelect is a TextInput component, if it's a textField, just remove the .textField property from the lines below.
//this gets the current caret position, and subtracts one (if not already at 0)
var pos:int = textSelect.textField.caretIndex > 0 ? textSelect.textField.caretIndex - 1 : 0;
//this sets the selection to adjusted caret postion
textSelect.setSelection(pos,pos);
I'm new to starling and this may sound like a noob question but here goes nothing.
Imagine the following scenario (in Flash):
A movieclip named test
Test has 80 frames
Test has 4 labels at 20 frames each
When I script test in my project. I make it loop from label 0-1 (frames 1-19). Then I tell it to loop on label 2 on a certain event.
This way, I do not add or remove a movieclip or instantiate things just one.
Now, if I think about implementing it in starling. I'm thinking make 4 movieclips in flash. Export them as sprite sheets and then make four movieclips in the script. Add whichever moviclip needs to play in the juggler and similarly removechild it at that time.
This way, I'm adding the overhead cost of 'addchild' and 'removechild' everytime I want to switch between those animations. Is that a more cost effective way?
I presume you want to export a single clip, but control multipe(4 animations) rather than a single one. If this is the case, I wrote a few JSFL scripts a couple of years ago (when CS6 wasn't around to export spritesheets) which exported the main timeline of an .fla document as an image sequence, but used the frame labels in the filenames. This made it easy to integrate with TexturePacker. You can see video of it here.
Here's a JSFL snippet which will export a frame sequence with names generated based frame labels which should make it easy to manage in TexturePacker:
var d = (FLfile.getPlatform() == 'macos') ? '/' : '\\'; //delimiter
var doc = fl.getDocumentDOM(); //document
var tl = doc.getTimeline();tl.setSelectedLayers(0,true); //timeline
var cl = tl.layers[0]; //current layer
var numFrames = cl.frameCount;
var className = prompt("Name for your sequence", toClassName(doc.name.substr(0,doc.name.length-4)));
className = className.split('.')[0];//just in case the user adds .as
className = toClassName(className);//remove non alphabet chars
var docPath = doc.pathURI.substr(0,doc.pathURI.length - doc.name.length);
var exportPath = docPath+className+'_export'+d;
if(!FLfile.exists(exportPath)) FLfile.createFolder(exportPath);
fl.outputPanel.clear();
for(i = 0 ; i < numFrames; i++) {
if(cl.frames[i].name != ''){//if frame is labelled
tl.setSelectedFrames(i,i,true);
doc.exportPNG(exportPath+cl.frames[i].name+lpad(''+i,4)+'.png',true,true);
}
}
fl.trace("export complete!");
function lpad(number, length){
var result = '' + number;
while (result.length < length) result = '0' + result;
return result;
}
function toClassName(input){
return input.replace(/[^a-zA-Z]/g, "");
}
Also, I suggest having a look at generator tools like Dynamic-Texture-Atlas-Generator, Fruitfly, etc.