Flash CC HTML5 Canvas load from file/database - html

im new to stackoverflow, so i hope im doing everything right here ^^
So to my Question/Problem:
Im working with Flash CC and made a small HTML5 Canvas Animation. In this Animation there is a Textfield where I want to load text from a file/database. Is this possible? I assume that I have to use javascript/json for that bec Flash CC Canvas is working with that. But can I code it directly in Flash (in the Actions Panel) or do I have to open the js-output file and put my code there? And are there any examples of that case?
Second, and a more specific case of that above, I want to load a specific text into my textfield for example:
lets say I have a text in 3 languages (english, italian, french).
so I send a variable, where I state which language I want (var = eng;) and the correct text is loaded (from the file or database) to the textfield.
I hope everything is understandable (sry im not a native speaker)
Thanks in advance for any help/tips

You can code that interaction directly into the .fla file if you like.
Here is a simple example that you can play around with:
Open a new blank HTML5 Canvas Document in Flash
Create a text field and button and give them the instance names of text_box for the text box and btn for the button.
paste the following code onto frame 1 on the timeline:
//Variables
var count = 0;
//Make browser listen for a mouse click on the button
this.btn.addEventListener("click", fnCycleLang.bind(this));
function fnCycleLang()
{
if(count == 0){
this.text_box.text = "Hello World!"; //this actually changes the text in the text box
count ++;//this adds 1 to the count
}
else if(count == 1){
this.text_box.text = "Bonjour Le Monde!";
count ++;
}
else if(count == 2){
this.text_box.text = "Ciao Mondo!";
count = 0;
}
}
When you publish the .fla to HTML5 you should be able to click the button to cycle through the different languages. This way may be a but code intensive but it depends on how much text you are going to need to swap around. This would be fine for something like some menu items. Anything with a lot of text you would probably be better off not using flash to author it for you.

Related

What should i use to load audio in as3 instead of load()?

I have a setup of 4x4 drum pads which are essentially buttons or movie clips with 3 states to show visual feedback (like the lighting up of the pads when they are pressed). I have tested this setup on an android tablet as well as a windows 7 touch-enabled laptop. Obviously the windows 7 laptop is more powerful and thus more responsive.
But not responsive enough.
I am wondering how I should be approaching the loading of audio files which are short drum-kit sample sounds in mp3 format stored in an assets folder next to the swf file.
Is there a better way, such as a way to cache the sounds so that I don't have to reload them each time the sound event is triggered?
Any help on this is greatly appreciated. (I just am not aware of how else, right now, to approach loading sounds other than calling a new sound each time the pad is pressed, thus not really caching the sound)
var kickc_03:Sound=new kickc_03_mp3(); // not sure whether to use Sound() or kickc_03 here..
// or
var kickc_03:kickc_03_mp3=new kickc_03_mp3();
// they will both work because kickc_03_mp3 subclasses Sound.
if i were you i would do something like:
// execute this once
var drumNum:int = 16;
var soundArray:Array = [null];
var C:Class;
init();
function init():void{
for(var i:int=1;i<=drumNum;i++){
C = Class(getDefinitionByName("kickc_"+formatF(i)+"_mp3");
soundArray.push(new C());
}
function formatF(n:int):String{
var s:String=n.toString();
while(s.length<2){
s="0"+s;
}
return s;
}
//////////////////////////////////////////////
now anytime you want to generate a kickc_somenum_mp3 sound, use:
soundArray[somenum].play();

Unable to access children in movie clip

Inside flash cs6 I have drawn a flash movieclip in which I set export settings as abc.Gameboard. Inside gameboard I have a bunch of pieces (symbol:Piece) which I export as abc.Piece - both base class set to MovieClip and with class files. The piece has frame labels like hit, over etc.. My problem is accessing the pieces in code so I can eg. gotoAndPlay("mine") - at the moment the event only fires once which is the last piece on the board.
I can set the frame action on this last piece but would like to figure out how to do same for each piece.
I add a gameboard to the stage like so
var gb:Gameboard = new Gameboard();
gb.name = "gb001";
contextView.addChild(gb);
Then
contextView.addEventListener(Event.ADDED, thingAdded);
private function thingAdded(event:Event):void
{
var type:String = event.target.toString();
switch(type)
{
// this runs only once - i want it to run for each piece that is inside the symbol
case "[object Piece]":
var p:MovieClip = event.target as Piece;
p.gotoAndPlay("mine");
break;
}
}
or if there's a better way that would be great.. this looks pretty clunky
Edit: Bit more info about how I'm trying to build the gameboard
Draw a collection of shapes in illustrator - mask it (Gameboard region). Import into Flash as Graphic. Convert graphic to several movie clip symbols (So JSFL can drill down and access masked pieces) - run JSFL script & create 00's of pieces. Then I set export settings on Piece and Gameboard and add Gameboard to the contextView.
I actually wrote an entire article about this once. The ADDED event should fire once for every DisplayObject that gets added. Are you sure that you're not using ADDED_TO_STAGE, which does not bubble? If you're using ADDED_TO_STAGE, then you need to set the useCapture flag to true to get it to fire for all children.
If you want to involve RobotLegs in the process, probably the better way is to simply create a "marker" Class for each specific button that you want to have behave in a different way, then register a mediator for each Class that will manage the behvior. Robotlegs already has the hooks built in to listen for ADDED_TO_STAGE and do this.
However, you could also consider using the Flash IDE for what it's for, which is putting stuff on stage. In that case, your GameBoard instance will be ready in the constructor of your main document Class for you to do whatever you want with it.
MPO is that logic that is outside Gameboard shouldn't know or care how it works internally, and honestly it probably shouldn't even be GameBoard's responsibility to handle simple stuff like button over states and things. That should be up to the button itself. If the buttons don't need to toggle or anything beyond what SimpleButton handles, you can just declare the button instances as Button in the library instead of MovieClip and get all that stuff for free instead of coding it yourself.
Part of being a good coder is in being able to figure out ways not to code everything.
in their Gameboard inside Piece? I want know exactly your Gameboard Structure.
If you're right. try this:
function thingAdded(e:Event):void
{
if(!e.target is Gameboard) return;
var mc:Gameboard = Gameboard(e.target);
var i:int = 0;
while(i<mc.numChildren)
{
if( mc.getChildAt(i) is Piece)
{
var piece:Piece = Piece(mc.getChildAt(i));
piece.gotoAndStop(2);
}
i++;
}
}
Here is my Sample code: Gameboard

HTML5 Canvas, Button instead of Keyboard

Found some nice Open Source code of a snake game, but wanting to make an iphone web version. So I can't use a physical keyboard, is it possible to use a button instead of it?
Have this code:
case 37:
if (direction != "right"){
moveLeft();
}
break;
Got some more questions but this one is the one 's keeping me where I am in the dev.
Sorry for my bad English, I am a dutch guy.
thanks
You don't need Canvas for this, just create a regular HTML button and put that code in its onclick event.

AS3/CS4 removed TextField reappearing after tween finishes

I load a swf via the Loader() class. As the swf is loaded I loop through all the children of root and remove any TextField with removeChild(). This works initially - the TextField is removed. But as a tween in the loaded swf finishes and loops, the removed TextField somehow reappears. The TextField is Dynamic text. If I remove some StaticText, this stays removed. What is going on here?
loopChildren(root);
function loopChildren(dispObj:*):void {
for (var i:int = 0; i< dispObj.numChildren; i++) {
var obj:DisplayObject = dispObj.getChildAt(i);
if (obj is DisplayObjectContainer) {
loopChildren(obj);
}
else {
if (obj is TextField) {
obj.parent.removeChild(obj);
i--;
}
}
}
}
If I call loopChildren with a timer (5 sec intervall) and trace(obj.name) for all children, I notice that the obj.name changes. The TextField is called instance11, instance32, instance54 and so on. It seems that a new instance is created and added continuously?
EDIT:
If I transfer the code directly to the swf and run it without using the wrapper it works as expected. I must be missing something that happens when its loaded through another movie?
EDIT2:
I should say that the TextField I'm trying to remove is in a separate layer on the same time line as the tween. If I put the tween in its own timeline, it seems to work as expected. But can't dynamic text and tween live together?
It sounds like your text field is defined on the stage (i.e. in the FLA, not instantiated by script), and the layer it's on probably has a keyframe that is played through at some point in your tween.
If that's correct, then what you're running into is a fairly low-level limitation of Flash regarding the competing natures of the timeline and the script environment. When you play through a tween, every timeline keyframe encodes some information for Flash about what sorts of object should be on the stage at that point, and where they should be, etc.
But when you also start referencing those same object with script, in general Flash has to guess whether you want the script or the information in the timeline to take precedence. In this case, probably what's happening is that after you remove the text field with script, one of the frames your tween plays through references the same field, and Flash doesn't know whether the removal was supposed to be permanent, or whether to honor the keyframe and recreate the field.
Thus, the short answer (too late!) to your question is, in general you should try to handle your stage elements with timeline tweens, or script, but not both. This is why your solution in edit #2 worked; because by moving the tween onto its own timeline, (I'm guessing) you no longer played across keyframes that encoded information about the text field in question. Another solution would probably be to remove the offending text field via the timeline (by playing to a keyframe where that text field no longer exists). Or, of course you could also control your textfield solely by script, creating it and destroying it, so it's never part of the timeline at all.
Why not just set the text field to empty?
loopChildren(root);
function loopChildren(dispObj:DisplayObjectContainer):void
{
for each(var i:int = 0; i < dispObj.numChildren; i++)
{
var obj:DisplayObject = dispObj.getChildAt(i);
if (obj is DisplayObjectContainer)
{
loopChildren(obj);
}
else if (obj is TextField)
{
TextField(obj).text = "";
}
}
}

Flash HTML image link AS3

I have a flash as3 file, and I'm importing an HTML file that contains this link:
<img src="https://www.paypal.com/en_GB/i/btn/btn_buynowCC_LG.gif" id="paypal_button">
and I cannot for the life of me figure it out, I've looked everywhere.
There were a couple of forums that suggested:
function onTicketLoad():void {
if(paypal != null && this.contains(paypal)) {
removeChild(paypal);
}
var imgLoader:DisplayObject = content_text.getImageReference("paypal_button");
//imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,onHtmlImageLoaded);
paypal.addChild(imgLoader.content);
}
function onHtmlImageLoaded(e:Event):void {
e.target.removeEventListener(Event.COMPLETE, onHtmlImageLoaded);
paypal.addChild(e.target.content);
paypal.buttonMode = true;
paypal.useHandCursor = true;
addChild(paypal);
}
(that example is fragmented as all hell, I just included it to illustrate what I'm trying to do)
Basically I think I need to load the image into the getImageReference method... but I'm so lost... any help would be appreciated!
I've found creating links on <img> tags in TextFields in Flash can be problematic.
I spotted the issue has been reported on the adobe bug tracker here:
http://bugs.adobe.com/jira/browse/FP-2146
Once as a workaround I created a separate swf file the exact same size as the image and embedded that instead (i.e. a 160x47 stage and a MovieClip/button with a clickable link using navigateToURL). Basically you can embed swf files within TextFields the same way as images.
It's not ideal but it did seem like a decent compromise considering the swf 'button' can now be used and laid out in more or less the same way as you would have used the <img> tag.
If you are going to use the < img > tag in an htmlText box, "you must set the text field to be multiline and to wrap text"
I tried it, and it works fine for me. It loaded the image and the link worked too. Here is my code:
import flash.text.TextField;
var labelText:String = '<img src="https://www.paypal.com/en_GB/i/btn/btn_buynowCC_LG.gif" id="paypal_button">';
var label:TextField = new TextField();
label.border = true;
label.multiline = true;
label.wordWrap = true;
label.width = 200;
label.height = 100;
addChild(label);
label.htmlText = labelText;
My example uses pure AS3, but it should be exactly the same as selecting "multiline" and "wordwrap" from your textfields properties panel.
AIR (Mobile) ignores IMG tags for security apparently... useless in my opinion.
Adding the fact the TextFlow is not recommend or supported for that either in Mobile, and many other fruit less attempts to get an IMG tag to render is beyond challenging so far. I'll wager nearly impossible with the given components (in AIR Mobile land of course, which is not well documented and conflictive with online sources around the issues, so it is unclear and confusing being fresh out of beta).