Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / Flash / Flash Actionscript / October 2005



Tip: Looking for answers? Try searching our database.

class/object question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ofeet - 31 Oct 2005 21:39 GMT
Here's the situation:

I'm trying to do a card game that was done in c++. In the c++ version, the
Deck object held multiple Card objects. How do I do this in flash?

Here's my Deck.as so far.

class Deck {
    var cards:Array;
    function Deck() {
       
    }
    function addCard(Card) {
        cards.push(Card);
    }
}

Whenever I try to get info about the card by using trace(Deck.cards[0]) I just
get 'undefined' but when I do Card.attribute I get info...

please help!
David Stiller - 31 Oct 2005 22:02 GMT
ofeet,

> Here's my Deck.as so far.
>
[quoted text clipped - 6 lines]
>  }
> }

   So, you have a Deck class and have defined a Deck property Deck.cards
(of Array datatype).  So far, so good.  You have defined a Deck.addCard()
method.  Fine.

> Whenever I try to get info about the card by using trace(Deck.cards[0])
> I just get 'undefined' but when I do Card.attribute I get info...

   Well, where have you defined Card?  You haven't shown us that code.  You
might have a Deck class and a Card class.  Each is going to do what it's
going to do.  Write your card Class, then import that into the Deck class so
Deck knows what the heck you're talking about.  While you're at it, you
should use the AS2 strong typing notation and purposefully choose to make
your members private or public, depending on your needs.

   I also notice, you haven't actually created an array.  You've only
established the property that will contain your array.  You might, for
example, create your array in the constructor.

import Card;
class Deck {
   // Properties
   private var cards:Array;
   // Constructor
   function Deck() {
       cards = new Array();
   };
   // Methods
   private function addCard(c:Card):Void {
       cards.push(c);
   }
}

David
stiller (at) quip (dot) net
"Luck is the residue of good design."
ofeet - 31 Oct 2005 22:05 GMT
Found the problem.

it appears as though

var cards:Array;

needed to be

var cards:Array = new Array();
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.