Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6151757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:48:49+00:00 2026-05-23T19:48:49+00:00

I’m trying to experiment with cocos2d by building a simple card game. I’m not

  • 0

I’m trying to experiment with cocos2d by building a simple card game. I’m not new to iOS development but I am very new to the cocos2d engine. I have 2 custom classes so far:

A card class:
.h

#import <Foundation/Foundation.h>
#import "cocos2d.h"

@interface Card : NSObject {
CCSprite *faceSprite;
NSString *suit;
NSUInteger value;
float priority;
}

@property (nonatomic, retain) CCSprite *faceSprite;
@property (nonatomic, readonly) NSString *suit;
@property (nonatomic, readonly) NSUInteger value;
@property (nonatomic, assign) float priority;

- (id)initWithSprite:(CCSprite *)paramSprite suit:(NSString *)paramSuit andValue:(NSUInteger)paramValue;

@end

.m

#import "Card.h"

@implementation Card
@synthesize faceSprite, suit, value, priority;

- (id)init
{
self = [self initWithSprite:nil suit:nil andValue:nil];
if (self) {
    // Initialization code here.
}

return self;
}

-(id)initWithSprite:(CCSprite *)paramSprite suit:(NSString *)paramSuit andValue:(NSUInteger)paramValue {
if ((self = [super init])) {
    faceSprite = paramSprite;
    suit = paramSuit;
    value = paramValue;
}

return  self;
}

@end

And a Deck class:
.h

#import <Foundation/Foundation.h>
#import "Card.h"

@interface Deck : NSObject {
NSMutableArray *cardsArray;
}
@property (nonatomic, retain) NSMutableArray *cardsArray;

-(void)shuffle;
-(Card *)drawTopCard;

@end

.m

- (id)init
{
if ((self = [super init])) {
    cardsArray = [[NSMutableArray alloc] init];
    for (NSInteger suit = 1; suit < 5; suit++) {
        NSString *suitString;
        switch (suit) {
            case 1:
                suitString = @"h";
                break;
            case 2:
                suitString = @"d";
                break;
            case 3:
                suitString = @"s";
                break;
            case 4:
                suitString = @"c";
                break;
        }
        for (NSInteger i = 3; i < 14; i++) {
            CCSprite *cardImage = [CCSprite spriteWithFile:[NSString stringWithFormat:@"%d%@.gif",i, suitString]];
            Card *card = [[Card alloc] initWithSprite:cardImage suit:suitString andValue:i];
            [cardsArray addObject:card];
        }

    }
}

return self;
}

-(void)shuffle {
int timesToShuffle = 1;
while (++timesToShuffle < 4) {
    for (int i = 0; i < [cardsArray count]; i++) {
        int cardToSwap = arc4random() % [cardsArray count];
        [cardsArray exchangeObjectAtIndex:i withObjectAtIndex:cardToSwap];
    }
}

}

-(Card *)drawTopCard {
Card *cardDrawn = [[cardsArray objectAtIndex:0] retain];
//[cardsArray removeObjectAtIndex:0];
return cardDrawn;
}

@end

When I try to do the following in my main scene, I get a EXC_BAD_ACCESS error inside CCScheduler.m in the update:(ccTime) method

-(id) init
{
if( (self=[super init])) {

    deck = [[Deck alloc] init];
    [deck shuffle];
    [self schedule:@selector(drawAndShowCard) interval:3.0];

}
return self;
}

-(void)drawAndShowCard {
// ask director the the window size
CGSize size = [[CCDirector sharedDirector] winSize];

// position the label on the center of the screen
Card *card = [deck drawTopCard];
CCSprite *cardSprite = (CCSprite *)card.faceSprite;
cardSprite.position =  ccp( size.width /2 , size.height/2 );

[self addChild:cardSprite];
}

strange thing is if I move the deck alloc init and the shuffle inside the drawCardAndShow method, it no longer crashes, however this is not the correct place for this for OBVIOUS reasons as I don’t want a whole new deck every time I draw a card…

any help?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:48:50+00:00Added an answer on May 23, 2026 at 7:48 pm

    I see one serious problem but you have multiple issues with your code.

    When you have strings as properties you should declare them as copy, in your case you have it as assign:

    @property (nonatomic, readonly) NSString *suit;
    

    If you want to declare the property readonly, you can once again declare the property inside your .m to make it writeable internally:

    @interface Card() 
    @property (nonatomic, copy)
    NSString *suit; 
    @end
    

    You should use the self. notation for properties to make your intent clear to others when you are accessing the ivar and when accessing the property.

    You got a mem leak here:

    Card *card = [[Card alloc] initWithSprite:cardImage suit:suitString andValue:i];
                [cardsArray addObject:card];
    -->missing [card release]
    

    This here is unnecessary

    @interface Deck : NSObject {
    NSMutableArray *cardsArray;
    }
    @property (nonatomic, retain) NSMutableArray *cardsArray;
    

    You can omit the ivar, just make sure you do not alloc/init without autorelease.

    If you insist on having an ivar rename at least to _cardsArray and then

    @synthesize cardsArray=_cardsArray
    

    so that you can distinguish between the two. In one case you need to explicit to retain, in the other you use the setter/getter.

    hth

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
Seemingly simple, but I cannot find anything relevant on the web. What is the
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.