Simple Card game help
A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Simple Card game help

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    3

    Simple Card game help

    Hello people and I welcome myself to FK..

    I am not an experienced user of Flash. I have done some stuff, but I have only been learning it for 3 months on and off (mostly off).

    So I want to make a simple card game. The game contains only 10 cards in the deck, and not the classic ones (they are symbols). The game works as follows:

    -One card is shown to the player, facedown. (this is done for aesthetics only).
    -The user clicks the card, which then divides into 3 cards, equally spaced, facedown as well.
    -The user then randomly clicks a card out of three, which then flips to reveal itself.
    -If the card is card X (there is only one X in the "deck"), then give him one point.
    -If the card is not the X, give him one penalty point.
    -Either way, the card fades out after it is flipped.
    -The turn is finished when the player finds the X, or if there is only one card left (which means it is the X, which the player didn't find).
    -New turn, previous steps.
    -The game ends in two ways:
    1. If the player has 20 penalties.
    2. Or if the player has 10 points.

    Now, my main problem is that my brain is currently mush from the many other assignments. Although I've programmed before and I know the steps BEFORE the actual programming, I can't think of the way to do this right now. I'm also hindered by my limited AS3 knowledge. So I will break my problem into questions, hopefully me (when my brain starts working) or somebody else will answer them.

    -How do I make the deck (the programming aspect)?
    -How do I create the animation of the single card breaking into three? Programming or keyframe?
    -How do I randomly assign 3 cards out of the 10 in each turn?
    -How do I link the images of my cards to their programming equivalent? (eg the water_card to the water_mc)
    -Do I use a variable which clears before each turn to remember how many cards has the user clicked?
    -Each card's movieclip has to have Flip and Fadeout animations?

  2. #2
    :
    Join Date
    Dec 2002
    Posts
    3,511
    There are many ways to approach this problem. Maybe this will give you are start...

    Code:
    // Core is the document class.
    package {
            import flash.display.*;
            import flash.events.*;
            import flash.text.*;
    
            public class Core extends Sprite {
                    // library contains a movie clip named 'card' with linkage
    "Card"
                    // registration is set in the middle
                    // first frame is back of card, the next 10 frames are the
    fronts of each card
                    // with the "X" card front as the last frame
                    //
                    private var _deck:Deck;
                    private var _goodTxt:TextField;
                    private var _badTxt:TextField;
                    private var _cardCount:int;
    
                    public function Core() {
                            // movie clip on stage, with instance name of "good"
                            // it contains dynamic text field with instance name
    of "txt"
                            var mc:MovieClip = getChildByName("good") as
    MovieClip;
                            _goodTxt = mc.getChildByName("txt") as TextField;
                            // movie clip on stage, with instance name of "bad"
                            // it contains dynamic text field with instance name
    of "txt"
                            mc = getChildByName("bad") as MovieClip;
                            _badTxt = mc.getChildByName("txt") as TextField;
                            _goodTxt.text = String(0);
                            _badTxt.text = String(0);
                            doNew();
                    }
                    private function doNew($e:Event=null):void {
                            if (_deck) {
                                    removeChild(_deck);
                                    _deck = null;
                            }
                            trace("************ NEW ROUND *************");
                            _cardCount = 0;
                            _deck = new Deck();
                            _deck.x = 250;
                            _deck.y = 350;
                            addChild(_deck);
                            _deck.buttonMode = true;
                            _deck.addEventListener(MouseEvent.CLICK, doClick,
    false, 0, true);
                    }
                    private function doClick($e:Event):void {
                            _deck.buttonMode = false;
                            _deck.removeEventListener(MouseEvent.CLICK,
    doClick);
                            _deck.dealThree();
                            _deck.addEventListener(Deck.ROUND_OVER, doNew,
    false, 0, true);
                            _deck.addEventListener(Deck.PICK_GOOD, doGood,
    false, 0, true);
                            _deck.addEventListener(Deck.PICK_BAD, doBad, false,
    0, true);
                    }
                    private function doGood($e:Event):void {
                            _goodTxt.text = String(parseInt(_goodTxt.text) + 1);
                            if (parseInt(_goodTxt.text) == 10) {
                                    trace("************ WON *************");
                                    if (_deck) {
                                            removeChild(_deck);
                                            _deck = null;
                                    }
                            }
                    }
                    private function doBad($e:Event):void {
                            _badTxt.text = String(parseInt(_badTxt.text) + 1);
                            if (parseInt(_badTxt.text) == 20) {
                                    trace("************ LOST *************");
                                    if (_deck) {
                                            removeChild(_deck);
                                            _deck = null;
                                    }
                            } else if (++_cardCount == 9) {
                                    doNew();
                            }
                    }
    
            }
    }
    Code:
    package {
            import flash.display.*;
            import flash.events.*;
            import fl.transitions.*;
            import fl.transitions.easing.*;
    
            public class Card extends MovieClip {
                    public static const CARD_PICKED:String = "card_picked";
                    public static const CARD_LEFT:String = "L";
                    public static const CARD_MIDDLE:String = "M";
                    public static const CARD_RIGHT:String = "R";
                    private static var cards = new Array(10);
                    private static var clickable:Boolean = true;
                    private var _isX:Boolean = false;
                    private var _pos:String = null;
                    private var _tween:Tween;
                    private var _frame:int;
    
                    public function Card($x:Boolean, $num:int) {
                            _isX = $x;
                            _frame = $num + 2;
                            gotoAndStop(1);
                            cards[$num] = this;
                    }
                    public static function set canClick($val):void {
                            clickable = $val;
                            for each (var i:Card in cards) {
                                    if (i._pos != null) {
                                            i.buttonMode = clickable;
                                    }
                            }
                    }
                    public function get Pos():String {
                            return _pos;
                    }
                    public function get isX():Boolean {
                            return _isX;
                    }
                    public function set Pos($pos:String):void {
                            _pos = $pos;
                            this.parent.setChildIndex(this,
    this.parent.numChildren - 1);
                            switch (_pos) {
                                    case CARD_LEFT :
                                            new
    Tween(this,"x",Regular.easeInOut,this.x,-150,1,true);
                                            break;
                                    case CARD_MIDDLE :
                                            new
    Tween(this,"x",Regular.easeInOut,this.x,0,1,true);
                                            break;
                                    case CARD_RIGHT :
                                            new
    Tween(this,"x",Regular.easeInOut,this.x,150,1,true);
                                            break;
                            }
                            Card.canClick = false;
                            _tween = new
    Tween(this,"y",Regular.easeInOut,this.y,-200,1,true);
                            _tween.addEventListener(TweenEvent.MOTION_FINISH,
    doReady, false, 0, true);
                    }
                    private function doReady($e:Event):void {
                            Card.canClick = true;
                            addEventListener(MouseEvent.CLICK, doClick, false,
    0, true);
                    }
                    private function doClick($e:Event):void {
                            if (clickable) {
                                    Card.canClick = false;
                                    removeEventListener(MouseEvent.CLICK,
    doClick);
                                    this.parent.setChildIndex(this,
    this.parent.numChildren - 1);
                                    _tween = new
    Tween(this,"scaleX",Regular.easeIn,1,0,0.5,true);
    
    _tween.addEventListener(TweenEvent.MOTION_FINISH, doFlip, false, 0, true);
                            }
                    }
                    private function doFlip($e:Event):void {
                            gotoAndStop(_frame);
                            _tween = new
    Tween(this,"scaleX",Regular.easeOut,0,1,0.5,true);
                            _tween.addEventListener(TweenEvent.MOTION_FINISH,
    doneFlip, false, 0, true);
                    }
                    private function doneFlip($e:Event):void {
                            if (_isX) {
                                    _tween = new
    Tween(this,"scaleX",Strong.easeInOut,1,1.8,2,true);
                                    _tween = new
    Tween(this,"scaleY",Strong.easeInOut,1,1.8,2,true);
                            } else {
                                    this.parent.setChildIndex(this, 0);
                                    _tween = new
    Tween(this,"alpha",Regular.easeOut,1,0,1,true);
                            }
                            _tween.addEventListener(TweenEvent.MOTION_FINISH,
    doPicked, false, 0, true);
                    }
                    private function doPicked($e:Event):void {
                            dispatchEvent(new Event(Card.CARD_PICKED, true));
                    }
            }
    }
    Code:
    package {
            import flash.display.*;
            import flash.events.*;
    
            public class Deck extends Sprite {
                    public static const ROUND_OVER:String = "round_over";
                    public static const PICK_GOOD:String = "pick_good";
                    public static const PICK_BAD:String = "pick_bad";
                    private var _deck:Array;
                    private var _cards:Array;
                    private var _pos:String;
    
                    public function Deck() {
                            _deck = makeRandArray(10);
                            trace(_deck);
                            _cards = new Array();
                            for (var i:String in _deck) {
                                    _cards[i] = new Card((_deck[i] == "9" ?
    true:false),_deck[i]);
                                    addChild(_cards[i]);
                            }
                            addEventListener(Card.CARD_PICKED, doPicked, false,
    0, true);
                    }
                    public function dealThree():void {
                            dealOne(Card.CARD_LEFT);
                            dealOne(Card.CARD_MIDDLE);
                            dealOne(Card.CARD_RIGHT);
                    }
                    private function dealOne($pos:String):void {
                            var card:Card = _cards.shift();
                            card.Pos = $pos;
                    }
                    private function doPicked($e:Event):void {
                            var card:Card = $e.target as Card;
                            Card.canClick = true;
                            if (card.isX == true) {
                                    dispatchEvent(new Event(Deck.ROUND_OVER,
    true));
                                    dispatchEvent(new Event(Deck.PICK_GOOD,
    true));
                            } else {
                                    dispatchEvent(new Event(Deck.PICK_BAD,
    true));
                                    if (_cards.length > 0) {
                                            dealOne(card.Pos);
                                    }
                            }
                    }
                    private static function makeRandArray($len:int):Array {
                            var _array = new Array();
                            for (var i:int = 0; i < $len; i++) {
                                    _array.push(i);
                            }
                            for (i =  -  $len; i < $len; i++) {
    
    _array.unshift(int(_array.splice(Math.random() * $len >> 0,1)));
                            }
                            return _array;
                    }
            }
    }

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Excellent answer!

    While I do not understand a lot of the code, and mostly what things are assigned to which class, I will study the code and try to incorporate it into my game (I don't like code I don't understand, hence my dislike towards snippets)

    I decided the following things, although I do not know yet how will they affect the code.
    -There is only bad card (9 of them in a deck) and the X card.
    -Also, an X card is ALWAYS present in each turn. So, each turn contains 2 bad and one X.

    My main question before I start reading the code, apart from the Core class, all the others are linked with what?

    I'm sorry, it's just that it's an 101 course for Flash and we have done nothing more than AS linkage on the library (no class coding, although I know about classes from python, but it seems way more complex with AS)

  4. #4
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Scrap what I said before. I do not want always an X card present.

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

Poll by Flashkit.com