Bird,GameWorld和GameScene是我项目中的三个自定义类。
我在Gameworld类中有一个Bird类型的对象(作为属性)。我在GameScene类中有类型Bird和Gameworld的对象。现在在Gamescene课上,当我这样做时:
_bird = [_gameWorld bird];
警告错误:
将非Objective-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
Bird.h
#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”,错误消失,它正在工作。进口是偶然进行的,不需要。但我不知道为什么它会导致错误。
答案 0 :(得分:2)
导入语句必须是线性的,但是你有一个循环。 GameScene.m
- &gt; GameScene.h
- &gt; Bird.h
- &gt; GameWorld.h
- &gt; Bird.h
。
因此,当GameWorld.h
有#import "Bird.h"
时,编译器发现Bird.h
已导入并且未导入它。问题是@interface Bird
尚未定义,因此GameWorld
没有Bird
的正确界面。
答案 1 :(得分:1)
正如你最后提到的,那是唯一的问题。
那发生了什么?
当您将Gameworld
导入Bird
接口文件时,您的Gameworld
本身就有一个名为bird
的变量Bird
。然后,您继续在后续行中定义Bird
类。在此之前,编译器不知道Bird
对象是什么,默认情况下它假定为int
。
如果您想在Gameworld
类中声明Bird
对象,那么您只需要使用前向声明,即
@class Gameworld;
这告诉编译器确实存在名为Gameworld
的 Class 。