how does the "for each in" loop work? - actionscript-3

for some reason this code does not work as intended, I've cut out most of my program but, all I think I need to tell you is that I have a tree array, and I am trying to make collisions with said trees so I thought that this would work:
function collisions(loopEvent:Event):void
{
for each (var a:tree in TreeArray)
{
if (brettMc.right1.hitTestObject(a.stump))
{
rightcoll = false;
}
else
{
rightcoll = true;
}
if (brettMc.left1.hitTestObject(a.stump))
{
leftcoll = false;
}
else
{
leftcoll = true;
}
if (brettMc.up1.hitTestObject(a.stump))
{
upcoll = false;
}
else
{
upcoll = true;
}
if (brettMc.down1.hitTestObject(a.stump))
{
downcoll = false;
}
else
{
downcoll = true;
}
}
}
I am pretty sure that the problem is just that the for each loop is messed up.

I don't see any issue with loop itself
EXCEPT
Is TreeArray actual name of variable? Or it's a Type?
If that's actual variable, can you trace the length of it before loop?

Related

Parameter passing for multiple text fields that are gathered in an array?

So, I have a question. I'm currently working an an education program that teaches basics on cell organelles. I have lines pointing to each organelle, in which I want to have the user be able to input what each organelle name is (like a diagram). When the user has completed the work properly, I will display the next button.
However, in order to give the user the ability to proceed, I need a way of tracking whether or not a student's answers are correct. I am using parameter passing for this.
I would like to be able to return either true/false. If all answers are returned true, the user may advance. If even one answer is wrong, a message displays.
How do I use parameter passing to determine if a users answers are right/wrong? The textfields are in an array as well...
Here is the code.
Thanks!
-Zero;
import flash.events.MouseEvent;
var organelleInput:Array=[F18nucleolusInput_txt, F18nucleusInput_txt, F18erInput_txt, F18golgiInput_txt, F18vacuoleInput_txt, F18chloroplastInput_txt, F18lysosomeInput_txt, F18mitochondriaInput_txt];
F18next_btn.visible=false;
F18next_btn.addEventListener(MouseEvent.CLICK, F18goToFrameNineteen);
F18back_btn.addEventListener(MouseEvent.CLICK, F18goToFrameSix);
checkMyWork_btn.addEventListener(MouseEvent.CLICK, checkAnswers);
function checkAnswers(event:MouseEvent):void
{
var answer:String;
var correctAnswers:Boolean;
var incorrectAnswers:Boolean;
answer = organelleInput[i];
correctAnswers=checkCorrectAnswers(answer);
for(var j:int=0; j<organelleInput.length; j++)
{
organelleInput[j].restrict="a-z";
if(correctAnswers==true)
{
F18output_txt.text="Well done!";
F18next_btn.visible=true;
checkMyWork_btn.visible==false;
}
if(correctAnswers==false)
{
F18output_txt.text="One of them seems to be wrong. Try again.";
}
}
}
function F18goToFrameNineteen(event:MouseEvent):void{
gotoAndStop(19);
}
function F18goToFrameSix(event:MouseEvent):void{
gotoAndStop(6);
}
function checkCorrectAnswers(s:String):Boolean
{
if(F18nucleolusInput_txt.text=="nucleolus"){
return true;
}
return false;
if(F18nucleusInput_txt.text=="nucleus"){
return true;
}
return false;
if((F18erInput_txt.text=="endoplasmic reticulum")||(F18erInput_txt.text=="er")){
return true;
}
return false;
if((F18golgiInput_txt.text=="golgi body")||(F18golgiInput_txt.text=="golgi apparatus")){
return true;
}
return false;
if((F18mitochondriaInput_txt.text=="mitochondria")||(F18mitochondriaInput_txt.text=="mitochondrion")){
return true;
}
return false;
if((F18lysosomeInput_txt.text=="lysosome")||(F18lysosomeInput_txt.text=="lysosomes")){
return true;
}
return false;
if(F18vacuoleInput_txt.text=="vacuole"){
return true;
}
return false;
if((F18chloroplastInput_txt.text=="chloroplast")||(F18chloroplastInput_txt.text=="chloroplasts")){
return true;
}
return false;
}
You can check Array of TextFields vs Array of Arrays:
var Correct:Array = [
["nucleolus"],
["nucleus"],
["golgi body", "golgi apparatus"],
["mitochondria", "mitochondrion"]
];
var Answers:Array = [T1, T2, T3, T4];
// Returns true if all answers are correct.
function allCorrect():Boolean
{
for (var i:int = 0; i < Answers.length; i++)
if (!oneCorrect(Answers[i], Correct[i]))
return false;
return true;
}
// Returns true if answer is on the Array of correct answers.
function oneCorrect(source:TextField, target:Array):Boolean
{
return target.indexOf(source.text.toLowerCase()) > -1;
}

Actionscript display text on screen once an action is performed

So I need to display a text once an action is performed but even though I tried to do so by using dynamic text and labels, I didn't manage to finish my programming due to errors:
var group:RadioButtonGroup= new RadioButtonGroup ("Question1");
var group2:RadioButtonGroup= new RadioButtonGroup ("Question2");
var group3:RadioButtonGroup= new RadioButtonGroup ("Question3");
var group4:RadioButtonGroup= new RadioButtonGroup ("Question4");
var group5:RadioButtonGroup= new RadioButtonGroup ("Question5");
var counterT:int;
var counterF:int;
submit.buttonMode=true;
counterT=0;
counterF=0;
t1.group = group;
f1.group = group;
t2.group=group2;
f2.group=group2;
t3.group=group3;
f3.group=group3;
t4.group=group4;
f4.group=group4;
t5.group=group5;
f5.group=group5;
submit.label="Submit";
submit.addEventListener(MouseEvent.CLICK,submitanswer);
function submitanswer (event:MouseEvent): void {
if (group.selection == t1) {
counterT==counterT+1
}
else
if (group.selection==f1) {
counterF==counterF+1;
}
}
if (group2.selection ==t2) {
counterT==counterT+1
}
else
if (group2.selection==f2) {
counterF==counterF+1
}
if (group3.selection ==t3) {
counterT==counterT+1
}
else
if (group3.selection==f3) {
counterF==counterF+1
}
if (group4.selection ==t4) {
counterT==counterT+1
}
else
if (group4.selection==f4) {
counterF==counterF+1
}
if (group5.selection ==t5) {
counterT==counterT+1
}
else
if (group5.selection==f5) {
counterF==counterF+1
}
The first thing I see is that you are using the == to set a value. You need to use = when setting values. So like this:
if (x == y) {
counter = counter + 1;
}
or you can just use counter++ like this
if (x == y) {
counter++;
}

How to drag and drop multiple objects to specific targets?

I am very new to AS3. Of course just ignore the reset button for now. I know my code is big and clunky but I don't mind that for now as it is easily understood by me.
I am trying to match a yellow ball with the yellow goal and green to green and so on.
However every time I try to drag a second object I just can't, the object is stuck, I hear that is the problem with AS3/Flash and the only simple way to solve it is to drag it off the screen which is something I don't want to do.
yellowBall.addEventListener(MouseEvent.MOUSE_DOWN, mouseDowny)
redBall.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownr)
blueBall.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownb)
greenBall.addEventListener(MouseEvent.MOUSE_DOWN, mouseDowng)
yellowBall.addEventListener(MouseEvent.MOUSE_UP, mouseUpy)
redBall.addEventListener(MouseEvent.MOUSE_UP, mouseUpr)
blueBall.addEventListener(MouseEvent.MOUSE_UP, mouseUpb)
greenBall.addEventListener(MouseEvent.MOUSE_UP, mouseUpg)
resetButton.addEventListener(MouseEvent.CLICK, reset);
addEventListener(Event.ENTER_FRAME,enterFrameHandlerr);
function mouseDowny(event:MouseEvent):void {
yellowBall.startDrag();
}
function mouseDownr(event:MouseEvent):void {
redBall.startDrag();
}
function mouseDownb(event:MouseEvent):void {
blueBall.startDrag();
}
function mouseDowng(event:MouseEvent):void {
greenBall.startDrag();
}
function mouseUpy(event:MouseEvent):void {
yellowBall.stopDrag();
}
function mouseUpr(event:MouseEvent):void {
redBall.stopDrag();
}
function mouseUpb(event:MouseEvent):void {
blueBall.stopDrag();
}
function mouseUpg(event:MouseEvent):void {
greenBall.stopDrag();
}
function enterFrameHandlerr(event:Event):void{
if(yellowBall.hitTestObject(yellowHome)){
yellowBall.stopDrag();
yellowBall.x = int(2000);
yellowBall.y = int(60);
}
if(redBall.hitTestObject(redHome)){
redBall.stopDrag();
redBall.x = int(425);
redBall.y = int(60);
}
if(blueBall.hitTestObject(blueHome)){
blueBall.stopDrag();
blueBall.x = int(425);
blueBall.y = int(275);
}
if(greenBall.hitTestObject(greenHome)){
greenBall.stopDrag();
greenBall.x = int(55);
greenBall.y = int(275);
}
}
function reset(event:MouseEvent):void
{
yellowBall.x = int(250);
yellowBall.y = int(250);
};

Actions Script 3.0 if else statements

need to do several things by it's click event. I'm a beginner to this, so is there any other way to write this code? by clicking this button, it goes to next frame and according to statement several buttons will be visible or not. I wrote the code this way and it says there are syntax error, but I couldn't find any. Hope you guys understand this and will help me. :) Thank you!
review_btn.addEventListener(MouseEvent.CLICK, review1)
function review1(event:MouseEvent):void{
if(rvw1 == "Correct"){
gotoAndStop(3627);
help1.visible = false
}
else{
gotoAndStop(3627);
help1.visible = true
}
}
review_btn.addEventListener(MouseEvent.CLICK, review2)
function review2(event:MouseEvent):void{
if(rvw2 == "Correct"){
gotoAndStop(3627);
help2.visible = false
}
else{
gotoAndStop(3627);
help2.visible = true
}
}
review_btn.addEventListener(MouseEvent.CLICK, review3)
function review3(event:MouseEvent):void{
if(rvw3 == "Correct"){
gotoAndStop(3627);
help3.visible = false
}
else{
gotoAndStop(3627);
help3.visible = true
}
}
review_btn.addEventListener(MouseEvent.CLICK, review4)
function review4(event:MouseEvent):void{
if(rvw4 == "Correct"){
gotoAndStop(3627);
help4.visible = false
}
else{
gotoAndStop(3627);
help4.visible = true
}
}
review_btn.addEventListener(MouseEvent.CLICK, review5)
function review5(event:MouseEvent):void{
if(rvw5 == "Correct"){
gotoAndStop(3627);
help5.visible = false
}
else{
gotoAndStop(3627);
help5.visible = true
}
}
I'll take an attempt at it. It looks like the only difference is that in each method you need to match up "helpX".visible with "rvwX" equals the string "Correct", where X is a number from 1-5. The gotoAndStop() frame is the same regardless. Also, that all five are meant to be off the same button. I'm going to take an assumption that the clips 'help' are movieclips defined on the stage else if they are from something else I would store them in an array for looping through instead of 'building' the name and finding the reference that way just for clarity.
function review(event:MouseEvent):void {
for(var counter:int = 1; counter < 6; counter++){
this["help" + counter].visible = (this["rvw" + counter] != "Correct");
}
this.gotoAndStop(3627);
}
review_btn.addEventListener(MouseEvent.CLICK, review);
I think you have to do a class with 2 fields: "help" and "rvw" (let me call it "Switcher"). Also it may contain a function of setting visibility (may, not must, this function can also be in your main class):
Switcher.as:
import flash.display.MovieClip;
public class Switcher {
private var help:MovieClip;
private var rvw:String;
public function setVisibility() {
help.visible = !(rvw == "Correct");
}
}
Then you have to make an array of Switcher's objects in your main class and to use only one "review" handler:
function review(event:MouseEvent):void {
for each(var sw:Switcher in switchersArray) {
sw.setVisibility();
}
this.gotoAndStop(3627);
}
The code from previous answer will work correctly, but IMHO, creating an Array (or Vector) of similar objects is better than doing lots of help1, help2, help3 etc variables.

firebug saying not a function

<script type = "text/javascript">
var First_Array = new Array();
function reset_Form2() {document.extraInfo.reset();}
function showList1() {document.getElementById("favSports").style.visibility="visible";}
function showList2() {document.getElementById("favSubjects").style.visibility="visible";}
function hideProceed() {document.getElementById('proceed').style.visibility='hidden';}
function proceedToSecond ()
{
document.getElementById("div1").style.visibility="hidden";
document.getElementById("div2").style.visibility="visible";
document.getElementById("favSports").style.visibility="hidden";
document.getElementById("favSubjects").style.visibility="hidden";
}
function backToFirst () {
document.getElementById("div1").style.visibility="visible";
document.getElementById("div2").style.visibility="hidden";
document.getElementById("favSports").style.visibility="visible";
document.getElementById("favSubjects").style.visibility="visible";
}
function reset_Form(){
document.personalInfo.reset();
document.getElementById("favSports").style.visibility="hidden";
document.getElementById("favSubjects").style.visibility="hidden";
}
function isValidName(firstStr) {
var firstPat = /^([a-zA-Z]+)$/;
var matchArray = firstStr.match(firstPat);
if (matchArray == null) {
alert("That's a weird name, try again");
return false;
}
return true;
}
function isValidZip(zipStr) {
var zipPat =/[0-9]{5}/;
var matchArray = zipStr.match(zipPat);
if(matchArray == null) {
alert("Zip is not in valid format");
return false;
}
return true;
}
function isValidApt(aptStr) {
var aptPat = /[\d]/;
var matchArray = aptStr.match(aptPat);
if(matchArray == null) {
if (aptStr=="") {
return true;
}
alert("Apt is not proper format");
return false;
}
return true;
}
function isValidDate(dateStr) {
//requires 4 digit year:
var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
var matchArray = dateStr.match(datePat);
if (matchArray == null) {
alert("Date is not in a valid format.");
return false;
}
return true;
}
function checkRadioFirst() {
var rb = document.personalInfo.salutation;
for(var i=0;i<rb.length;i++) {
if(rb[i].checked) {
return true;
}
}
alert("Please specify a salutation");
return false;
}
function checkCheckFirst() {
var rb = document.personalInfo.operatingSystems;
for(var i=0;i<rb.length;i++) {
if(rb[i].checked) {
return true;
}
}
alert("Please specify an operating system") ;
return false;
}
function checkSelectFirst() {
if ( document.personalInfo.sports.selectedIndex == -1)
{
alert ( "Please select a sport" );
return false;
}
return true;
}
function checkRadioSecond() {
var rb = document.extraInfo.referral;
for(var i=0;i<rb.length;i++) {
if(rb[i].checked) {
return true;
}
}
alert("Please select form of referral");
return false;
}
function checkCheckSecond() {
var rb = document.extraInfo.officeSupplies;
for(var i=0;i<rb.length;i++) {
if(rb[i].checked) {
return true;
}
}
alert("Please select an office supply option");
return false;
}
function checkSelectSecond() {
if ( document.extraInfo.colorPick.selectedIndex == 0 ) {
alert ( "Please select a favorite color" );
return false;
}
return true;
}
function check_Form(){
var retvalue = isValidDate(document.personalInfo.date.value);
if(retvalue) {
retvalue = isValidZip(document.personalInfo.zipCode.value);
if(retvalue) {
retvalue = isValidName(document.personalInfo.nameFirst.value);
if(retvalue) {
retvalue = checkRadioFirst();
if(retvalue) {
retvalue = checkCheckFirst();
if(retvalue) {
retvalue = checkSelectFirst();
if(retvalue) {
retvalue = isValidApt(document.personalInfo.aptNum.value);
if(retvalue){
document.getElementById('proceed').style.visibility='visible';
var rb = document.personalInfo.salutation;
for(var i=0;i<rb.length;i++) {
if(rb[i].checked) {
var salForm = rb[i].value;
}
}
var SportsOptions = "";
for(var j=0;j<document.personalInfo.sports.length;j++){
if ( document.personalInfo.sports.options[j].selected){
SportsOptions += document.personalInfo.sports.options[j].value + " ";
}
}
var SubjectsOptions= "";
for(var k=0;k<document.personalInfo.subjects.length;k++){
if ( document.personalInfo.subjects.options[k].selected){
SubjectsOptions += document.personalInfo.subjects.options[k].value + " ";
}
}
var osBox = document.personalInfo.operatingSystems;
var OSOptions = "";
for(var y=0;y<osBox.length;y++) {
if(osBox[y].checked) {
OSOptions += osBox[y].value + " ";
}
}
First_Array[0] = salForm;
First_Array[1] = document.personalInfo.nameFirst.value;
First_Array[2] = document.personalInfo.nameMiddle.value;
First_Array[3] = document.personalInfo.nameLast.value;
First_Array[4] = document.personalInfo.address.value;
First_Array[5] = document.personalInfo.aptNum.value;
First_Array[6] = document.personalInfo.city.value;
for(var l=0; l<document.personalInfo.state.length; l++) {
if (document.personalInfo.state.options[l].selected) {
First_Array[7] = document.personalInfo.state[l].value;
}
}
First_Array[8] = document.personalInfo.zipCode.value;
First_Array[9] = document.personalInfo.date.value;
First_Array[10] = document.personalInfo.phone.value;
First_Array[11] = SportsOptions;
First_Array[12] = SubjectsOptions;
First_Array[13] = OSOptions;
alert("Everything looks good.");
document.getElementById('validityButton').style.visibility='hidden';
}
}
}
}
}
}
}
}
/*function formAction2() {
var retvalue;
retvalue = checkRadioSecond();
if(!retvalue) {
return retvalue;
}
retvalue = checkCheckSecond();
if(!retvalue) {
return retvalue;
}
return checkSelectSecond() ;
} */
</script>
This is just a sample of the code, there are alot more functions, but I thought the error might be related to surrounding code. I have absolutely no idea why, as I know all the surrounding functions execute, and First_Array is populated.
However when I click the Proceed to Second button, the onclick attribute does not execute because Firebug says proceedToSecond is not a function
button code:
<input type="button" id="proceed" name="proceedToSecond" onclick="proceedToSecond();" value="Proceed to second form">
I had the same problem, and it's because you have a form with the same name as your function. JavaScript doesn't seem to be able to distinguish between the two, so you get the "not a function" error.
Maybe there is an error in your Javascript before that snippet given by you. If so, the rest will not be parsed by Firefox and then your function will not be defined.
The problem was resolved by changing proceedToSecond() to doIt() in both the call and the function name. I have no idea why
Hmm... I always used javascript:function_name() instead of function_name() because a few times the javascript didn't run. The name tag for the html snippet might have to be changed to a slightly different name because javascript might be getting it mixed up. Can you show us the entire javascript file because there may be a mistake/syntax error somewhere at the bottom/top.
It works on my computer, Firefox 3.5.5, Firebug 1.4.3, when inserting your code into an empty html document (<html><head/><body> code </body></html>)
Maybe there is another bug somewhere in your DOM, or in some of your other functions?
Could you possibly paste the entire source here, or maybe on a pastebin site?