Convert code sniplet gotoandstop to if else coondition - actionscript-3

i just want to ask how can i make a code without needing the code sniplet gotoandstop when clicking button, i want to make if else statement where if he click this called "answerb.btn" he will go to the destined frame.
Well im thinking this code
If (answer.btn = onPress) {
gotoAndStop(2);
}
But somehow ita wrong code i dont know what code to use for pressing button. Pls enlighten me ty

You should have an event listener. Here is an example of the way you could to this:
button.addEventListener(MouseEvent.CLICK, onClick);
private function onClick(e:MouseEvent):void
{
//your logic here
this.gotoAndStop(2);
};

Here is an example from one of my projects which required text to be entered into blocks before proceeding, but it used if statements with buttons presses so you could modify it to suit your needs as you wish!
stop();
var names:String;
var id:String;
var supervisor:String;
namein.addEventListener(TextEvent.TEXT_INPUT,paramChanged);
idin.addEventListener(TextEvent.TEXT_INPUT,paramChanged);
supervisorin.addEventListener(TextEvent.TEXT_INPUT,paramChanged);
beginbutton.enabled = false;
function paramChanged(event:TextEvent):void
{
if (namein.text != "" && idin.text != "" && supervisorin.text != "" &&
namein.length >=5 &&
idin.length >=5 &&
supervisorin.length >=5)//add your other fields here
{
beginbutton.enabled = true;
beginbutton.addEventListener(MouseEvent.CLICK,
fl_ClickToGoToAndPlayFromFrame);
}
else
{
beginbutton.enabled = false;
//If something changes that means we now fail the test you will want to disable the button again
}
}
function fl_ClickToGoToAndPlayFromFrame(event:MouseEvent):void
{
names = namein.text;
id = idin.text;
supervisor = supervisorin.text;
gotoAndPlay(15);
}

Related

Actionscript: SWF file does not record data to txt, while the compiler does

I am trying to create a psychological experiment in ActionScript. The performance of the participants is to be stored in a separate .csv file. I have written this code (apparently, instead of "This is the text to write" there is going to be a data array, but the problem appears with this code on equal parts)
import flash.net.FileReference;
import flash.events.Event;
var hint:String = "Please give a name for the file";
var labelling:String;
experiment_label.addEventListener(KeyboardEvent.KEY_UP, enter_pressed)
function enter_pressed (event:KeyboardEvent):void{
if (event.keyCode == 13) {
labelling = experiment_label.text;
addEventListener(Event.ENTER_FRAME,saveFile);
var ss:String = "this is text to write";
var fileRef:FileReference;
fileRef = new FileReference();
function saveFile(event:Event):void
{
fileRef.save(ss, labelling+".csv");
removeEventListener(Event.ENTER_FRAME, saveFile);
}
}
}
The problem I am facing is as follows: when I run it in from under Flash, it operates perfectly, and the save window does pop up. However, if I run an .swf file separately, it just ignores saving command.
Could you kindly suggest, what can I do? Or maybe I should use a different approach to saving altogether?
You current code will give you an Error #2176 (use a try ... catch to catch it) because FileReference.save()
is not called in response to a user action, such as a mouse event or keypress event.
To avoid that, you have to remove the Event.ENTER_FRAME event listener, even you don't need it :
function on_KeyUp(event:KeyboardEvent):void
{
if (event.keyCode == 13)
{
save_File();
}
}
function save_File():void
{
try
{
var fileRef:FileReference = new FileReference();
fileRef.save('some text here', 'my_file.csv');
}
catch (e:Error)
{
trace(e.toString());
}
}
Hope that can help.

Flash as3 show specific movie clip on click

I just started using flash so this might be a simple thing so please be patient.
I'm doing a project were I have a map and you can click the places on the map and the info appears. I could manage to link every button_btn to every info_mc but that would take forever, so I tried to make a function that would read the buttons name and redirect to the info_mc itself. However since the name is being converted to string when I change from name_mc to name_info it is not redirecting. Here is the code.
winterfell_info.visible = false;
map.winterfell_btn.doubleclickEnabled = true;
map.winterfell_btn.mouseChildren = doubleClickEnabled;
map.winterfell_btn.addEventListener(MouseEvent.DOUBLE_CLICK, show_clicked);
function show_clicked(e:MouseEvent):void{
var get_name = e.currentTarget.name;
var open_info_string = get_name.slice(0,-3) + "info";
//I've tried some things here but nothing is working for me.
var open_info = stage.getChildByName(open_info_string);
open_info.visible = true;
}
Any help would be very much appreciated, thank you.
And if you wondering yes its a map for Game of Thrones.
Remove quotes around "open_info_string", as a first measure, and change the capital S in that string to normal one "s", because Actionscript is case sensitive.
var open_info = stage.getChildByName(open_info_string);
Then, if it wouldn't work at once, insert a trace(open_info_string); before you try to reach the movie clip, to find out whether it was cropped correctly.
i've done something similar.. i don't have time to adapt the code, but if you look at what I did here I believe it'll help you. I'm making an array with the instance names of objects on my video player and then created a function which adds the appropriate event listener using a switch case statement.. easiest way to do this in mass, in my opinion.
var buttons:Array = [clicky,pauseBtn,playBtn];
for (var i in buttons)
{
buttons[i].buttonMode = buttons[i].useHandCursor = true;
addEventListner(buttons[i]);
}
function addEventListner(movieClip)
{
movieClip.addEventListener(MouseEvent.CLICK, function(e){
e.stopPropagation();
switch (movieClip.name)
{
case "playBtn" :
youTubeHolder.videoPlayer.playVideo();
videoController.playBtn.visible = false;
videoController.pauseBtn.visible = true;
break;
case "pauseBtn" :
youTubeHolder.videoPlayer.pauseVideo();
videoController.playBtn.visible = true;
videoController.pauseBtn.visible = false;
break;
case "stopBtn" :
youTubeHolder.videoPlayer.seekTo(0);
youTubeHolder.videoPlayer.stopVideo();
break;
default :
trace(movieClip.name+"is not a valid button");
}
});
}

Call two function in one onchange = not working

I have been searching how to put more than one function in onchange and I found how something like this for example: onchange = "function1(); function2();".
My problem here is I have followed what does the example like, but only function1 is working, function2 is not working. If I make it otherwise to onchange = "function2(); function1();", only function2 is working, function1 is not working, the same.
Any ideas guys?
Thanks.
The functions, I used Ajax:
function1(test)
{
var kode = test.value;
if (!kode) return;
xmlhttp.open('get', '../template/get_name-opr.php?kode='+kode, true);
xmlhttp.onreadystatechange = function() {
if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200))
{
//alert(kode);
document.getElementById("code").innerHTML = xmlhttp.responseText;
}
return false;
}
xmlhttp.send(null);
}
function2(test)
{
var kode = test.value;
if (!kode) return;
xmlhttp**1**.open('get', '../template/get_name2-opr.php?kode='+kode, true);
xmlhttp**1**.onreadystatechange = function() {
if ((xmlhttp**1**.readyState == 4) && (xmlhttp**1**.status == 200))
{
//alert(kode);
document.getElementById("code2").innerHTML = xmlhttp**1**.responseText;
}
return false;
}
xmlhttp**1**.send(null);
}
To solve my problem, I created two xmlhttp different. (xmlhttp and xmlhttp1).
Go through the link I gave, it seems to be problem with the way you are managing the xmlhttprequest objects, manage their instances properly, in your case because you are using the same xmlhttprequest for two simultaneous AJAX requests, only one of them is getting served. Either wait for one of them to get served or create two instances of the xmlhttprequest.
The statement xmlhttp.readystate = function() {...} obviously replaces the readystate property of that xmlhttprequest object, so on your second function, that is being replaced( because you are using the xmlhttprequest for both of them ). This is why you are seeing the funny behaviour.
Call function2() at the end of function1().
onchange = "function1()"
function1(){
...
function1 body;
...
function2()
}
Wrap the two function calls in one and call that function!
function myFirstFunction() {
//body
}
function mySecondFunction() {
//body
}
//Call this guy.
function myWrappedFunction() {
myFirstFunction();
mySecondFunction();
}

manually set check box selected in as3

I have a CheckBox in as3 that i need to set as checked with code. Do I dispatch an event? If so, how do I create an event, and give it a target (target is read only). Or is there another way to do it? thanks.
my_checkbox.selected = true;
And of course:
my_checkbox.selected = false;
To handle the box being checked/unchecked:
my_checkbox.addEventListener(MouseEvent.CLICK, _selected);
function _selected(e:MouseEvent):void
{
var bool:Boolean = e.target.selected;
if(bool)
{
trace("was checked");
// more code
}
else
{
trace("was unchecked");
// more code
}
}

Actionscript 3 drop down menu link error

I'm having issues with the following menu. I've been adapting a script from kirupa into actionscript 3.
When I get to the last level of the menu it won't link properly. It always takes the last url of the bunch as the url for all links in that branch of the tree.
Can anyone help me get it to link properly? A zip with the fla and the xml can be found at the following link.
http://www.jdviz.com/projects/xmlmenu.zip
Thanks,
There's a problem with the closures at the end of the code. The current button is not identified properly.
if (node_xml.childNodes[i].nodeName != "resource") {
//cleared the code for clarity...
} else {
curr_item.arrow.visible = false;
curr_item.addEventListener(MouseEvent.MOUSE_DOWN, function(e:MouseEvent):void {
trace(curr_item.urlLink);
});
}
change the above to:
var currentButton:MenuItem_mc = new MenuItem_mc();
function mouseOverHandler(e:MouseEvent ):void
{
currentButton = e.currentTarget as MenuItem_mc;
currentButton.addEventListener( MouseEvent.CLICK , clickHandler );
}
function clickHandler(e:MouseEvent):void
{
var btn:MenuItem_mc = event.currentTarget as MenuItem_mc;
trace( btn.urlLink );
}
if (node_xml.childNodes[i].nodeName != "resource") {
//cleared the code for clarity...
} else {
curr_item.arrow.visible = false;
curr_item.addEventListener(MouseEvent.MOUSE_DOWN, mouseOverHandler );
}