0

我是使用 Objective-C 和 Stackoverflow 编程的新手,我需要一些帮助。

我正在尝试从 NSMutableArray 获取对象并检查其内部属性之一。我认为最好的方法是执行以下演员:

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

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

问题是当我创建一个 GenericRoom 类型的类时,您的构造函数已经将 myType 定义为 EMPTY。当我在 NSMutrableArray 中插入实例时,我以这种方式更改为 myType TAVERN:

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

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

但是当我在那里进行引用时,它只是创建了对象 GenericRoom 的新实例,而不是在 NSMutableArray 中复制对象,使其结果始终为 YES,我失败的验证。

有没有更好的方法来解决这个问题?谢谢你们。

编辑:完整的代码

通用房间.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

通用房间.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

房间管理器.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

房间管理器.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
4

3 回答 3

0

如果您上面的代码是它在实际项目中的显示方式,那么问题是您正在重新声明您的myRooms对象。当您编写此行时:

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

您正在创建一个名为的新本地范围对象myRooms,当您退出当前范围(方法、块等)时该对象将丢失

解决此问题所需要做的就是简单地删除NSMutableArray *行首的 ,它会将新分配的数组分配给您的属性。像这样:

myRooms = [[NSMutableArray alloc] initWithCapacity:15];
于 2012-11-09T17:20:42.290 回答
0

加强@Dan F 的回答。读这个 ...

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

...什么是nil消息传递。您在本地声明myRooms的变量隐藏了您的myRooms属性(= 实例变量)。换句话说,myRoomsnil。会发生什么...

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

由于nil消息传递规则,[myRooms objectAtIndex:...]返回nil,因此您room的设置为nil.

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

并向对象room.myType发送消息。因为是,返回值是- 枚举确实包含整数标量值 - 这就是它返回的原因。我假设这是你的第一个元素 enum = has value 。即使您的实例变量是. == => 也会返回。myTyperoomroomnil00EMPTY000YESmyRoomsnil

于 2012-11-09T17:35:31.910 回答
0

您的初始化程序似乎有一些缺陷,这可能会导致您的问题。

// NOTE: `Rooms` instead of `enum Rooms`
- (id)initWithType:(Rooms)roomType 
{ 
   if (self = [super init])  
   {
      // NOTE: accessor is used, otherwise the value is cleared when out of scope
      self.myType = roomType; 
   }
   return self;
}
于 2012-11-09T18:12:18.543 回答