A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: [RESOLVED] simple flashcard script

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    2

    resolved [RESOLVED] simple flashcard script

    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];
    }
    }
    Attached Files Attached Files

  2. #2
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    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

    Code:
    forward.onClick = function() {
    trace (i==cards.length-1);
    if (i==cards.length-1){
    	i=0;
    	}else{
    	++i
    	}
    	txt2.text = cards[i][0];
    	
    }
    and that works fine.

  3. #3
    up to my .as in code Chris_Seahorn's Avatar
    Join Date
    Dec 2004
    Posts
    4,389
    those are tertiary operators, a shorhand way of writing "if" clauses....very popular and yes......used by Wilbert wherever appropriate

  4. #4
    Senior Member
    Join Date
    Dec 2002
    Location
    Netherlands
    Posts
    1,632
    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.

  5. #5
    Junior Member
    Join Date
    Nov 2009
    Posts
    2
    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.

  6. #6
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center