Hi, I am trying to create a simple flashcard script using arrays. I have gotten pretty far, but I can't get the function right to forward to the next question in the flash card array. The function to go back works fine. I originally wrote it in javascript, and transfered it into as3. Here is my code:
Code:
import km.core.*;
import km.components.*;
//flashcards array
var cards = [
['card1', 'answer1'],
['card2', 'answer2'],
['card3', 'answer3']
];
var i = 0;
txt2.text = cards[i][0];
var back:LabelButton = new LabelButton;
var forward:LabelButton = new LabelButton;
var flip1:LabelButton = new LabelButton;
var flip2:LabelButton = new LabelButton;
ScriptedSkin.applyTo(back,forward,flip1,flip2);
back.setSize(90,24);
back.move(114,30);
back.label.text = '<< Back';
forward.setSize(90,24);
forward.move(424,30);
forward.label.text = 'Next >>';
flip1.setSize(90,24);
flip1.move(275,30);
flip1.label.text = 'Show';
flip2.setSize(90,24);
flip2.move(275,30);
flip2.label.text = 'Hide';
addChild(back);
addChild(forward);
addChild(flip1);
back.onClick = function() {
//this works fine navigating the array
i = (i == 0) ? cards.length-1 : i-1;
txt2.text = cards[i][0];
//clear answer area
txt1.text = "";
}
forward.onClick = function() {
//for some reason, this doesn't work
i = (i == cards.length-1) ? 0 : i+1;
txt2.text = cards[i][0];
}
flip1.onClick = function() {
txt1.text = cards[i][1];
}
}
I don't really get the syntax you are using, I reccognize it as shorthand and I see guys like Wilbert use it. So rather than fix it per say I rewrote it as
Yes, I use it sometimes
There's nothing wrong with the code in this case. It's a little KM problem.
If you change your variable declaration from
var i = 0;
into
var i:int = 0;
The error is gone.
I would advise you to use the debug version of the Flash Player. In this case for example it shows an error that might help Bob fix the problem.
Ah the var i:int=0 was the only thing missing! Thank you! I probably shouldn't be using shorthand like that and should be writing it all the way out, but I figured if it worked in javascript it would work in flash, and it does.
The shorthand is a good thing, I just didn't follow it.
Chris and Wilbert are much more experienced coders than I am. Frankly till this week I hadn't had a day off from my real job in about 20 days, I do this stuff on the side from time to time and get a little rusty between.