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

  • Home
  • SEARCH
  • 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 8818565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:08:14+00:00 2026-06-14T05:08:14+00:00

I am new to programming with Objective-C and Stackoverflow, and I need some help.

  • 0

I am new to programming with Objective-C and Stackoverflow, and I need some help.

I’m trying to get an object from a NSMutableArray and check one of its internal properties. I figured the best way to do this is by doing the following cast:

GenericRoom *room = (GenericRoom*)[myRooms objectAtIndex: currentIndex + side];

if (room.myType == EMPTY) {
    return YES;
}

The problem is that when I create a class of type GenericRoom, your constructor already defines myType as EMPTY. When I insert the instances in NSMutrableArray, I changed to myType TAVERN this way:

NSMutableArray *myRooms = [[NSMutableArray alloc] initWithCapacity:15];

for (int i = 0: i <15: i + +) {
    GenericRoom tmpRoom * = [[GenericRoom alloc] initWithType: TAVERN];
    myRooms insertObject: tmpRoom atIndex i];
}

But when I do the cast that quote up there, it simply creates a new instance of the object GenericRoom, instead of copying the object inside NSMutableArray, making its result is always YES, my failing vereficação.

Is there any better way to solve this problem?
Thank you all.

EDIT: The complete code

GenericRoom.h

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

enum Rooms {
    EMPTY, TAVERN, WARRIORGUILD, MAGEGUILD
}roomsType;

@interface GenericRoom : CCLayer {
    CCSprite *mySprite;
    enum Rooms myType;
}

@property (nonatomic, retain) CCSprite *mySprite;
@property enum Rooms myType;
@property int test;

- (id)initWithSprite: (NSString *)file;
- (id)initWithType: (enum Rooms)roomType;

@end

GenericRoom.m

#import "GenericRoom.h"
@implementation GenericRoom
@synthesize mySprite, myType, test;

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

    }
    return self;
}

-(id)initWithType: (enum Rooms) roomType {
    if (self = [super init]) {
        myType = roomType;

    }
    return self;
}

-(id)initWithSprite: (NSString *)file {

    if (self = [super init]) {
        mySprite = [CCSprite spriteWithFile:file];

        [self addChild: mySprite];
    }

    return self;
}
@end

RoomManager.h

#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "GenericRoom.h"
#import "RoomTavern.h"
#import "RoomEmpty.h"

enum Sides {
    LEFT, RIGHT, UP, DOWN
}theSides;

@interface RoomManager : CCLayer {
    CGSize size;
    NSMutableArray *myRooms;
}

- (void) CreateRoom: (enum Rooms)roomType;


@end

RoomManager.m

@implementation RoomManager

-(id)init {

    if (self = [super init]) {
        size = [[CCDirector sharedDirector] winSize];
        myRooms = [[NSMutableArray alloc] initWithCapacity:15];

        for (int i = 0; i < 15; i++) {
            GenericRoom *tmpRoom = [[GenericRoom alloc] initWithType:TAVERN];
            tmpRoom.test = 10;
            [myRooms insertObject:tmpRoom atIndex:i];
        }

        [self CreateRoom:TAVERN];
    }

    return self;
}

- (void) CreateRoom: (enum Rooms)roomType {
    switch (roomType) {
        case TAVERN:
        {
            //Create the Tavern Main Room
            RoomTavern *tmpRoom = [[RoomTavern alloc] initWithSprite:@"room-hd.png"];
            tmpRoom.mySprite.position = ccp(size.width/2,size.height/2);

            [self addChild:tmpRoom];

            [myRooms removeObjectAtIndex:8];
            [myRooms insertObject:tmpRoom atIndex:8];

            if ([self CheckAdjacentRooms:8 andSide:LEFT]) {
                RoomEmpty *tmpEmptyRoom = [[RoomEmpty alloc] initWithSprite:@"roomToBuild-hd.png"];
                tmpEmptyRoom.mySprite.position = ccp(tmpRoom.mySprite.position.x - tmpRoom.mySprite.contentSize.width, tmpRoom.mySprite.position.y);

                [self addChild:tmpEmptyRoom];

                [myRooms insertObject:tmpEmptyRoom atIndex:7];
            }

            break;
        }

        default:
            break;
    }
}

- (BOOL) CheckAdjacentRooms: (int)currentIndex andSide:(enum Sides)side {
    int leftRightSide = 0;
    if(side == LEFT)
        leftRightSide = -1;
    else if (side == RIGHT) 
        leftRightSide = 1;

    GenericRoom *roomTmp  = (GenericRoom *)[myRooms objectAtIndex:currentIndex + side];

    if (roomTmp.myType == EMPTY) {
        return YES;
    }
    else
        return NO;
}

@end
  • 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-06-14T05:08:16+00:00Added an answer on June 14, 2026 at 5:08 am

    To enhance @Dan F answer. Read this …

    http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/objectivec/Chapters/ocObjectsClasses.html#//apple_ref/doc/uid/TP30001163-CH11-SW7

    … what’s nil messaging. Your locally declared myRooms variable hides your myRooms property (= instance variable). In other words, myRooms is nil. And what happens …

    GenericRoom *room = (GenericRoom*)[myRooms objectAtIndex: currentIndex + side];
    

    Because of nil messaging rules, [myRooms objectAtIndex:...] returns nil, thus your room is set to nil.

    if (room.myType == EMPTY) {
        return YES;
    }
    

    And room.myType sends message myType to room object. And because room is nil, return value is 0 – enums does contain integer scalar values – this is the reason why it returns 0. And I assume that EMPTY is first element in your enum = has value 0. And 0 == 0 => returns YES even when your myRooms instance variable is nil.

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

Sidebar

Related Questions

I am new to Xcode and Objective-C programming and need some help. I am
I'm new to programming and stackoverflow and have decided to start by learning objective
First of all, I'm new to objective-c programming so I've probably made some mistakes
I'm very new to objective-c and programming. I had read in one of the
I'm new to Objective-C programming so sorry for the lame question. I'm trying to
I'm new to programming in general and currently trying to get a foot into
I am very new to programming and Objective-C and I am trying to work
I am new Objective C. I am trying to understand and workout problems from
I'm new to programming in Objective-C and have some C++ under my belt. As
Being relatively new to programming--think Programming in Objective-C by Kochan, Chapter 15--I'm wondering if

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.