I have a website that has multiple languages, currently the language bar is displayed along the top of the site with a button for each language. What I would prefer is one button, click it once, the button image changes to a union jack flag, and english is called, press it again, and the button image changes to french, and the french language is called, etc etc..
Is this one button, multiple calls possible? can anyone give me a good place to start looking in to this?
Yes, it is possible.
Your html would look something like this:
<button id='changeLang' onclick='changeLang()'></button>
(Remember to set this to your default to begin with)
and your javascript:
var counter = 0;
function changeLang(){
var buttonDetails = document.getElementById('changeLang');
buttonDetails.innerHTML = "";
switch(counter){
case 0:
buttonDetails.innerHTML = "<img src='unionJack.png' />English";
counter +=1;
break;
case 1:
buttonDetails.innerHTML = "<img src='frenchFlag.png' />French";
counter +=1;
break;
case 2:
//continue like above
default:
counter = 0;
changeLang();
}
}
If you ever need another language, just add it to the switch.
Alternatively, change the case code to:
case 0:
buttonDetails.src = "unionJack.png";
counter +=1;
break;
And remove the line that says buttonDetails.innerHTML = ""; if all you want is an image.
To call a function to change the language you would simply add a function call to one of your switch cases like so:
case 0:
buttonDetails.innerHTML = "<img src='unionJack.png' />English";
counter +=1;
foo(bar); //just add your function call
break;
Related
My code works, except it is requiring and extra "Space" at the end to be placed in order for the button to activate? Any ideas? I obviously don't have the space at the end of the user names or passwords in the code. This happens on another frame as well where I have the user type in a web address, I have the conditional set as == "md.website.com" but it is requiring "md.website.com " (extra space at the end) in order for the button to activate.
This code is expecting "AB1234 " and "newuser " instead of "AB1234" "newuser" like I need and I am telling it... I'm sorry, I'm new to AS3 and learning ALL I can, this site rocks for all the help I've already gotten!
username_txt.addEventListener(TextEvent.TEXT_INPUT,paramChanged3);
password_txt.addEventListener(TextEvent.TEXT_INPUT,paramChanged3);
next_btn.enabled = false;
next_btn.alpha = .5;
function paramChanged3(event:TextEvent):void
{
if (username_txt.text == "AB1234" && password_txt.text == "newuser" )
{
trace("go")
next_btn2.enabled = true;
next_btn2.alpha = 1;
next_btn2.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndPlay_20)
}
else
{
next_btn2.enabled = false;
next_btn2.alpha = .5;
}
}
function fl_ClickToGoToAndPlay_20(event:MouseEvent):void
{
gotoAndPlay(20);
}
The problem is that the TextEvent.TEXT_INPUT fires before the text field is actually updated. Try using Event.CHANGE instead (or using two TextEvent.TEXT_INPUT callbacks and appending the input character with event.text within each).
I don't know why AS3 is requiring the extra space, but I removed the exact conditional, and just did a minimum character count. Of course the trainee can then type in anything as long as it matches the minimum requirement, but again, the actual usernames and passwords don't matter, its all simulation anyways, here is the code with the character count....
username_txt.addEventListener(TextEvent.TEXT_INPUT,paramChanged3);
password_txt.addEventListener(TextEvent.TEXT_INPUT,paramChanged3);
next_btn.enabled = false;
next_btn.alpha = .5;
function paramChanged3(event:TextEvent):void
{
if (username_txt.text != "" && username_txt.length >=5 &&
password_txt.text != "" && password_txt.length >=6)
{
trace("go")
next_btn2.enabled = true;
next_btn2.alpha = 1;
next_btn2.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndPlay_20)
}
else
{
next_btn2.enabled = false;
next_btn2.alpha = .5;
}
}
function fl_ClickToGoToAndPlay_20(event:MouseEvent):void
{
gotoAndPlay(20);
}
I'm working on a game, in my game the user acquires weapons (cupcakes) throughout the levels. I have a button that the player clicks to change to the next cupcake (weapons acquired are stored in an array).
Here's my problem: if the player acquires 2 yellow cupcakes, 1 red cupcake, and 2 blue cupcakes, how do I navigate through the array without showing the same cupcake twice?
My button looks as if it's not changing weapons, when in code it is, it's going to the next element in the array but it's the same weapon [yellow cupcake, yellow cupcake, red cupcake, blue cupcake, blue cupcake].
public function CupcakeChangeButton(e: MouseEvent) {
cakeButtonCounter++;
//Make sure the cakeButtonCounter never exceeds the amount of
//elements in array
if (cakeButtonCounter >= CupcakesArray.length - 1) {
cakeButtonCounter = 0;
}
//NOTE: may need function called "cupcake count", this function should
//be called at the beginning of THIS function and in the constructor function
//Should count the amount of cupcakes and maybe set wasSeen Elements to No
/*
The switch statment makes its decisions based on the type of cupcake
is the current elment. The current element is represented by
"cakeButtonCounter" (CupcakesArray[cakeButtonCounter])
The if statements decides if the cupcake has been seen already.
If it hasnt been seen, it sets the button's text box to show how many of
those kind of cupcakes are left.
After the amount of cupcakes of that type is shown in the text box,
the "unshift" method is used to place "yes" in the first element, of it's
own Array type, WITHIN THE WAS SEEN ARRAY.......confusing!!!!!!
Example:
wasSeen.PinkCupcake.unshift("yes") = wasSeen.PinkCupcake[0] == "yes"
This is done so that we can keep track of what cupcakes has been seen
already
When the game is initialized the was seen elements are set to "no". So when
it's set to "yes", after being seen, The initial value,"no", is placed in the
next element. It's no longer needed, so we use the "splice" method to delete it
and HOPEFULLY, remove it from memory
The "else" block of code takes place if the cupcake has already been seen.
It increments the cakeButtonCounter so that the button will display the next
cupcake in the array, that has NOT been seen
After all decisions are made,(which cupcakes are next and which cupakes have
been seen)
Update the button's face by displaying the next cupcake that hasnt been seen
(button goes to and stop and next element)
NOTE: The ACTUAL case will be the dynamic var cupcake type (the NAME not the actual
cupcake)
*/
switch (CupcakesArray[cakeButtonCounter]) {
case "PinkCupcake":
if (wasSeen.PinkCupcake[0] == "no") {
CupcakeNavigationBox.countBox.text = "x" + PinkCupcakeCount;
wasSeen.PinkCupcake[0] == "yes";
trace("element change? " + wasSeen.PinkCupcake[0]);
} else {
cakeButtonCounter++;
trace("if yes...its starting that way " + wasSeen.PinkCupcake[0]);
}
break;
case "YellowCupcake":
if (wasSeen.YellowCupcake[0] == "no") {
CupcakeNavigationBox.countBox.text = "x" + YellowCupcakeCount;
wasSeen.YellowCupcake[0] == "yes";
} else {
cakeButtonCounter++;
}
break;
case "GreenCupcake":
if (wasSeen.GreenCupcake[0] == "no") {
CupcakeNavigationBox.countBox.text = "x" + GreenCupcakeCount;
wasSeen.GreenCupcake[0] == "yes";
} else {
cakeButtonCounter++;
}
break;
case "PurpleCupcake":
if (wasSeen.PurpleCupcake[0] == "no") {
CupcakeNavigationBox.countBox.text = "x" + PurpleCupcakeCount;
wasSeen.PurpleCupcake[0] == "yes";
} else {
cakeButtonCounter++;
}
break;
}
CupcakeNavigationBox.buttonFace.gotoAndStop(CupcakesArray[cakeButtonCounter]);
changeCupcake();
}
You should instead store available types of weapons in the array from where you choose the weapon type. Also, that array should contain ints, because apparently your cupcakes are spent on fire/action, thus you will have the ammo storage array at your disposal. If you need unlocked array, by all means make another array. (Make sure there's a default weapon, though, or else cake selection function can go into an infinite loop)
var cupcakesAmmo:Array=[0,0,0,0,0,0,0]; // as many as you have types of cupcakes
var cupcakesTypes:Array=[RedCupcake,YellowCupcake,PinkCupcake,GreenCupcake,BlueCupcake,PurpleCupcake,BlackCupcake];
// let's say we have seven cupcakes
var currentCupcake:int; // the selected weapon
function cupcakeChangeButton(e: MouseEvent) {
// first check if we actually have anything to change to
var weHave:int=0; // how many different cupcakes we have
var i:int;
for (i=0;i<cupcakesAmmo.length;i++) if (cupcakesAmmo[i]>0) weHave++;
if (weHave<2) return; // hehe, we either have no cupcakes or just one type of em
// otherwise let's change
do { // we have to do this at least once
currentCupcake++;
if (currentCupcake==cupcakesAmmo.length) currentCupcake=0; // change type
} while (cupcakesAmmo[currentCupcake]==0); // change until we find the type with nonzero ammo
// okay, type selected, let's get proper display
CupcakeNavigationBox.countBox.text = "x" + cupcakesAmmo[currentCupcake];
// see, no switch is needed! Just get the data off your array :)
// TODO change the displayed cupcake too (I don't see where you do this)
}
I don't know how to explain this good, I'll do my best :p
I have this code:
public function clickeado(MouseEvent):void{
if(getChildByName("placa") == null){
addChild(info);
}
trace(MouseEvent.target.name)
switch(MouseEvent.target.name){
case "_783":
info.circuito_tf.text = _783_circuito;
info.localidad_tf.text = _783_localidad;
info.responsable_tf.text = _783_responsable;
break;
I want that the text of "info.circuito_tf.text" to be the value of the variable called "_783_circuito". It's ok. Now I have 17 more cases, so I decided to do something like this:
switch(MouseEvent.target.name){
case "_783":
info.circuito_tf.text = MouseEvent.target.name + "_circuito";
info.localidad_tf.text = MouseEvent.target.name + "_localidad";
info.responsable_tf.text = MouseEvent.target.name + "_responsable";
break;
I wish I explained well, thanks!
ps: the value of info.circuito_tf.text in the second case it's "783circuito" instead of the value of the variable
You can address a variable through a string via this syntax:
info.circuito_tf.text = this[MouseEvent.target.name + "_circuito"];
Watch out for undefined values, though.
Yes, with this syntax you don't need the switch statement at all, just make sure your movie clips all have valid names and correct variables exist and are assigned values.
I'm making a dots and boxes game in flash using actionscript 3.
Currently I'm completely stuck as I want the player to click two buttons before a line is displayed. I planned to do something like this using the if statement
if button1 and button2 clicked
line1 visible = true
I've also tried adding eventlisteners with one function
function showLine(e:Event):void {
blue0001.visible = true
}
dot00.addEventListener(MouseEvent.CLICK, showLine);
But as far as I know this can only be used when you want to click one button. Is there anyway to have two eventlisteners satisfyed before the function is carried out?
Also how would I (if i can) use the if statements to carry out this?
You would probably do something like this, pseudo-code:
Assume all of the dots are in a dots array.
for (var i: Number = 0; i < dots.length; i++) {
dots.addEventListener(MouseEvent.CLICK, dotClicked, false, 0, true);
}
dotSelected = null;
function dotClicked(evt:MouseEvent):void {
if (dotSelected && isNeighbor(evt.target, dotSelected)) {
showLineConnecting(evt.target, dotSelected)
dotSelected = null;
} else if (!dotSelected) {
highlightDot(evt.target);
dotSelected = evt.target;
} else {
showError("You must click an adjacent dot");
}
}
At the request of the OP, this is what's going on.
for (var i: Number = 0; i < dots.length; i++) {
dots.addEventListener(MouseEvent.CLICK, dotClicked, false, 0, true);
}
Add an event listener to every dot. Here I am assuming you already have an array of dots defined. Each instance in the Array would be a MovieClip (likely), Sprite, or another DisplayObject.
dotSelected = null;
We will use a variable to keep track of any currently selected dot. Since no dot will be selected when the game starts, we set it to null.
function dotClicked(evt:MouseEvent):void {
if (dotSelected && isNeighbor(evt.target, dotSelected)) {
showLineConnecting(evt.target, dotSelected)
dotSelected = null;
} else if (!dotSelected) {
highlightDot(evt.target);
dotSelected = evt.target;
} else {
showError("You must click an adjacent dot");
}
}
This is the function that will get called when any dot is clicked. For explanation's sake, let's take the first click of the game. dotSelected is null, so the first if is false. The second if though is true, because (!dotSelected) is true. So, we run some function I called highlightDot with the dot as the argument. That function could look something like this:
function hightlightDot(dot:Dot):void {
dot.gotoAndStop("selected");
}
Now a second click is made. Now the first part of the first if, dotSelected, is true. The second part is now evaluated. Again I put in a made up function isNeighbor. The isNeighbor function takes two arguments, the dot that was just clicked and the dot that has already been clicked. This function needs to make sure the two dots are adjacent. This could be something like...
function isNeighbor(dot1:Dot, dot2:Dot):void {
return ((dot1.xGrid == dot2.xGrid && Math.abs(dot1.yGrid - dot2.yGrid) == 1) || (Math.abs(dot1.xGrid - dot2.xGrid) == 1) && dot1.yGrid == dot2.yGrid));
}
The above function assumes that the instances of Dot have some properties xGrid and yGrid which defines where in the playing board they sit. If they are in the same row and 1 column apart they are neighbors. If they are in the same column and 1 row apart they are neighbors.
The last thing that happens in a function showLineConnecting is called. That function will again take the two adjacent dots as arguments. It will then draw a line between them in whatever way you choose to do that. Finally, dotSelected is set back to null, allowing another set of dots to be selected.
One thing I just realized, it would probably be helpful to have an additional property that gets triggered on a Dot when it is connected to all of its neighbors so it could no longer be selected.
You will also need logic to handle knowing that a box has been created. For that you would likely just iterate the possibilities given the line that was just drawn. For each line drawn there are only two possible boxes that have been created. So check them both. Watch out for edges.
I'm in the process of designing a Flash application, and it requires either two or three pieces of data from the user. The third data element is only relevant if one of the other elements has changed from the default value.
In order to prevent the user from inputting information that would skew the calculations based on the input, how can I gray out the text input box if the default conditional is unchanged? I could embed a conditional to ignore the input from that particular box, but I know that some users will fill in all data fields regardless of on-screen instructions.
Just use the Event.CHANGE listener + handler to check the condition/state of elements. This event is fired every single time the user alters a component. For example, heres a text input doing this:
var tf:TextInput = new TextInput();
var someOtherObject:Button = new Button();
tf.x = 100;
tf.y = 100;
tf.width = 150;
stage.addChild(tf);
stage.addChild(someOtherObject);
tf.addEventListener(Event.CHANGE, onFormChanged);
private function onFormChanged(e:Event):void
{
switch(e.currentTarget){
case tf:
//Do your conditional checks here.
if(tf.text.toString().length < 10){
someOtherObject.enabled = false;
someOtherObject.text = "";
someOtherObject.maxChars = 0;
}else{
someOtherObject.maxChars = 100;
}
break;
case someOtherObject:
break;
}
}
So basically, as the user types into tf, I'm making sure they have entered at least 10 characters. If they were to meet the condition, and then erase a character, the event would be fired again, and the condition would not be met, so someOtherObject is disabled.