Help me with some Actionscript Cards

Need a little help with some coding that makes cards

Page 1 of 1

2 Replies - 1521 Views - Last Post: 30 May 2009 - 12:45 PM

#1 MrKarazy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-May 09

Help me with some Actionscript Cards

Posted 29 May 2009 - 07:32 PM

Hey guys, wondering if you can help me with some problems i've been having trying to make a deck of cards in Actionscript

I have to set it up into three files, Card, Deck, and CDtester.

Now, the only problems that im having are with my CDtester file
Getting errors like this "1136: Incorrect number of arguments. Expected 1."
this "1067: Implicit coercion of a value of type void to an unrelated type Card."
and this "1137: Incorrect number of arguments. Expected no more than 0."

If anyone could reply very soon, that would be great.

Thanks! :D


--------------------------------CARD CODE-------------------------------


package {

	public class Card {

		//PROPERTIES
		protected var suit:int;
		protected var rank:int;
		protected var facing:Boolean;
		const SUIT_NAMES:Array=new Array("Hearts","Diamonds","Clubs","Spades");
		const RANK_NAMES:Array = new Array("Two", "Three", "Four", "Five",
		   "Six", "Seven", "Eight", "Nine", "Ten",
		   "Jack", "Queen", "King", "Ace");

		//CONSTUCTOR
		public function Card(s:int,r:int,f:Boolean) {
			this.suit=s;
			this.rank=r;
			this.facing=f;
		}

		public function getSuitName(s:int):String {
			return SUIT_NAMES[s - 1];
		}
		public function getRankName(r:int):String {
			return RANK_NAMES[r - 1];
		}
		//behaviorS/FUNCTIONS

		//flip
		public function flip():void {
			if (this.facing==false) {
				this.facing==true;
			}

		}
		public function toString(r:int, s:int){
			//check if card is facing is true
			if (this.facing==false) {
				trace("Card is not flipped");
			} else {
				trace("Card is the "+r+" of "+s);
			}
		}

		public function isValid(s:int, r:int){
			if ((s>=0 && s>=4) && (r>=0 && r>=13)) {
				return true;
			} else {
				return false;
			}
		}

		//GETTERS
		public function getSuit():int {
			return this.suit;
		}
		public function getRank():int {
			return this.rank;
		}
		public function getFacing():Boolean {
			return this.facing;
		}
	}

}




----------------------------------------DECK CODE--------------------------------------

package {

	public class Deck {

		//PROPERTIES
		protected var cards:Array;
		protected var topspot:int;
		protected var e:Card;
		protected var facing = e.getFacing;
		protected var suit = e.getSuit;
		protected var rank = e.getRank;

		//CONSTRUCTOR
		public function Deck() {
			this.cards=new Array(52);
			this.topspot=cards[51];
			
		}

		//behaviorS/FUNCTIONS
		//print
		public function print():void {
			for (var i:int=0; i<52; i++) {
				trace(cards[i]);
			}
		}
		//shuffle
		public function shuffle(startIndex:int=0,endIndex:int=0):void {
			if (endIndex==0) {
				endIndex=cards.length-1;
			}
			for (var i:int=endIndex; i>startIndex; i--) {

				var randomNumber:int=Math.floor(Math.random()*endIndex)+startIndex;
				var tmp:* =cards[i];
				cards[i]=cards[randomNumber];
				cards[randomNumber]=tmp;
			}
		}
		//add top/bottom
		public function addCardToTop():void {
			var tmp:* =cards[51];
			cards[51]=cards[0];
			cards[0]=tmp;
		}

		public function addCardToBottom():void {
			var tmp2:* =cards[0];
			cards[0]=cards[51];
			cards[51]=tmp2;
		}

		//countcards
		/*public function countCards(c):int {
		
		}*/


		//draw top/bottom
		public function drawTopCard():void {
			trace(cards[51]);
			this.topspot=cards[50];
		}
		public function drawBottomCard():void {
			trace(cards[0]);
			this.topspot=cards[50];
		}
		//validated
		/*public function isValid(e:Card):Boolean {
		this.suit+=e.getSuit();
		this.rank+=e.getRank();
		
		}*/

		//flipAll
		public function flipAll(e:Card):void {
			/*for (var i:int = 0; i < 52; i++) {
			if (cards[i].facing=false) {
			cards[i].facing=true;
			}
			}*/
			this.facing+=e.getFacing();
			cards.forEach(this.facing=true);
		}

		public function equals(e:Card):void {
			this.rank+=e.getSuit();
			this.rank+=e.getRank();

		}

		//GETTERS
		public function getCards():Array{
			return this.cards;
		}
		public function getTopspot():int {
			return this.topspot;
		}

	}
}






--------------------------------------CDTESTER CODE---------------------------------

package {


	import flash.events.*;
	import flash.display.*;
	import flash.ui.*;

	public class CDtester extends MovieClip {
		var input="";
		var testDeck:Deck = new Deck();
		var testCard:Card=new Card(-1,-1,false);



		public function CDtester() {
			this.stage.addEventListener(KeyboardEvent.KEY_DOWN, inputKey);
			this.startMenu();
		}

		public function startMenu():void {

			trace("[=======================]");
			trace("Choose an option by hitting the corresponding number");
			trace("1- Make a Deck");
			trace("2- Shuffle");
			trace("3- Flip deck");
			trace("4- Print Deck");
			trace("5- Draw top card");
			trace("6- Draw bottom card");
			trace("7- Add card to top");
			trace("8- Add card to bottom");
			trace("9- Check if deck is valid");
			trace("0- Count remaining cards");
			trace("[=======================]");

		}

		public function inputKey(e:KeyboardEvent) {

			if (e.keyCode==13) {
				var inputnum=parseInt(input);
				switch (inputnum) {
					case 1 :
						trace("Making a new deck...");
						testDeck = new Deck();
						break;
					case 2 :
						trace("Shuffling deck...");
						testDeck.shuffle();
						break;
					case 3 :
						trace("Flipping cards...");
						testDeck.flipAll();
						break;
					case 4 :
						trace("");
						testDeck.print();
						break;
					case 5 :
						testCard=testDeck.drawTopCard();
						trace(testCard.toString());
						testCard.flip();
						trace("You drew"+testCard.toString());
						testCard.flip();
						break;
					case 6 :
						testCard=testDeck.drawBottomCard();
						trace(testCard.toString());
						testCard.flip();
						trace("You drew"+testCard.toString());
						testCard.flip();
						break;
					case 7 :
						trace("Adding "+testCard.toString() +" to the top...");
						testDeck.addCardToTop(testCard);
						break;
					case 8 :
						trace("Adding "+testCard.toString() +" to the bottom...");
						testDeck.addCardToBottom(testCard);
						break;
						/*case 9 :
						if (testDeck.isValid()) {
						trace("Deck is real.");
						} else {
						trace("Deck is fake!!");
						}
						break;*/
						/*case 0 :
						trace("There are "+testDeck.countCards()+ " cards left.");
						break;*/
					default :
						trace("Enter an option between 0-9.");
				}
				input="";
				this.startMenu();
			} else {
				input+=String.fromCharCode(e.charCode);


			}

		}

	}
}




Is This A Good Question/Topic? 0
  • +

Replies To: Help me with some Actionscript Cards

#2 SoLi  Icon User is offline

  • andydust.com

Reputation: 41
  • View blog
  • Posts: 1,438
  • Joined: 27-January 02

Re: Help me with some Actionscript Cards

Posted 30 May 2009 - 09:56 AM

one particular problem is that you have specified for the flipAll function in the Deck class to take an argument:

public function flipAll(e:Card):void



but when calling it from the CDTester you do not provide a card object:

	   case 3 :
						trace("Flipping cards...");
						testDeck.flipAll();
						break;




Similarly your functions addCardToBottom and addCardToTop, also in the Deck class, do not take any arguments:

public function addCardToTop():void
...
public function addCardToBottom():void



but when you call them from the CDTester class, you are providing a card object:

testDeck.addCardToTop(testCard);




Lastly, your datatype mismatch error is because the function drawTopCard in the Deck class is defined to return void:

   public function drawTopCard():void



However in the CDTester class, you attempt to assign it to a variable that has been declared as a Card object:

var testCard:Card=new Card(-1,-1,false);
...
testCard=testDeck.drawTopCard();



The same is true for the drawBottomCard function.

Hope this helps :)

This post has been edited by SoLi: 30 May 2009 - 09:58 AM

Was This Post Helpful? 1
  • +
  • -

#3 MrKarazy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-May 09

Re: Help me with some Actionscript Cards

Posted 30 May 2009 - 12:45 PM

I think i'm seeing what you mean.

Ill try to fix these and let you know how it goes.

Thanks! :^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1