Bird,GameWorld和GameScene是我项目中的三个自定义类。

我在Gameworld类中有一个类型为Bird(作为属性)的对象。我在GameScene类中具有Bird和Gameworld类型的对象。现在在Gamecene课上,当我这样做时:

_bird = [_gameWorld bird];


错误提示:


  非目标C指针类型'int *'的隐式转换为'Bird'
  *'不适用于ARC


为什么会这样呢?

编辑:

GameScene.h

#import <SpriteKit/SpriteKit.h>
#import "Bird.h"
#import "ScrollHandler.h"
#import "Pipe.h"
#import "GameWorld.h"

@interface GameScene : SKScene <SKPhysicsContactDelegate>{

    int _midPointY;
    float _gameHeight;
    NSString *_gameName;
    NSString *_getReady;
    Bird *_myBird;

    NSTimeInterval _dt;
    float bottomScrollerHeight;

    GameWorld *_myWorld;

}

@property (nonatomic) SKSpriteNode* backgroundImageNode;
@property (nonatomic) SKSpriteNode* greenBird;

@property (nonatomic) NSTimeInterval lastSpawnTimeInterval;
@property (nonatomic) NSTimeInterval lastUpdateTimeInterval;


@end


GameWorld.h

#import <Foundation/Foundation.h>
#import <SpriteKit/SpriteKit.h>
#import "Bird.h"
#import "ScrollHandler.h"

typedef NS_ENUM(NSInteger, GameState){
    MENU,
    READY,
    RUNNING,
    GAMEOVER,
    HIGHSCORE
};

@interface GameWorld : NSObject <ScrollHandlerDelegate>{

    float _runTime;
    GameState _currentState;

}

@property (nonatomic, strong) ScrollHandler *scroller;
@property (nonatomic, strong) Bird *bird;
@property (nonatomic, assign) int midPointY;
@property (nonatomic, assign) int score;
@property (nonatomic, strong) SKSpriteNode *bg;
@property (nonatomic, strong) NSArray *birds;
@property (nonatomic, strong) SKSpriteNode *birdNode;

@end


GameScene.m

#import "GameScene.h"


#define UPWARD_PILLER @"Upward_Green_Pipe"
#define Downward_PILLER @"Downward_Green_Pipe"

static const uint32_t pillerCategory            =  0x1 << 0;
static const uint32_t birdCategory        =  0x1 << 1;
static const uint32_t grassCategory             =  0x1 << 2;


static const float BG_VELOCITY = (TIME * 60);

static inline CGPoint CGPointAdd(const CGPoint a, const CGPoint b)
{
    return CGPointMake(a.x + b.x, a.y + b.y);
}

static inline CGPoint CGPointMultiplyScalar(const CGPoint a, const CGFloat b)
{
    return CGPointMake(a.x * b, a.y * b);
}



@implementation GameScene

-(id)initWithSize:(CGSize)size
{
    if (self = [super initWithSize:size])
    {

        self.anchorPoint = CGPointMake(0, 1);
        self.yScale = -1;

        //To detect collision detection
        self.physicsWorld.contactDelegate = self;
        _myWorld = [[GameWorld alloc] initWithMidPointY:_midPointY];
        [self initGameObjects];
        [self initAssets];
        [self setCoinAnimation];
    }
    return self;
}

-(void) initGameObjects {

    _myBird = [_myWorld bird]; //here is the problem

}

-(void) initAssets {

    //initialize other assets

}

-(void) setCoinAnimation {

    //initialize other assets

}

@end




#import "GameWorld.h"

@import UIKit;

@interface Bird : NSObject {

    CGPoint _position;
    CGPoint _velocity;
    CGPoint _acceleration;

    float _rotation;
    float _originalY;
    int _width;
    int _height;
    int _dieCount;
    bool _isAlive;

}

@end


注意:我刚刚发现删除导入的“ Gameworld.h”
 从“ Bird.h”中消失,该错误消失并且正在运行。导入是在那里意外完成的,因此不需要。但是我不为什么会导致错误。

最佳答案

就像您在最后提到的那样,那是唯一的问题。

那到底是怎么回事?

当您将Gameworld导入到Bird接口文件中时,您的Gameworld本身就有一个名为bird的变量,其类型为Bird。然后,您继续并在随后的几行中定义了Bird类。在此之前,编译器不会知道Bird对象是什么,并且默认情况下将其假定为int

如果您要如何在Gameworld类中声明Bird对象,则无需使用接口将其导入,而只需使用前向声明即可。

@class Gameworld;


这告诉编译器确实存在一个名称为Gameworld的类。

关于ios - ARC不允许将非客观C指针类型'int *'隐式转换为'Bird *',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29465707/

10-10 23:46