Hi guys please can anyone help out this error: "Accessing unknown' lives' getter method" I am getting on the 2nd line(CCSprite line) of the code below.
GamePlayLayer.h is:
#import "cocos2d.h"
#import "Box2D.h"
#import "GLES-Render.h"
#import <Foundation/Foundation.h>
#class UILayer;
#class Insect;
#interface GamePlayLayer : CCLayer {
b2World * world;
GLESDebugDraw * debugDraw;
CCSpriteBatchNode * sceneSpriteBatchNode;
Insect * insect;
b2Body *body;
SceneUILayer * uiLayer;
double startTime;
bool gameOver;
bool gameWon;
NSMutableArray *lives;
}
- (id)initWithUILayer:(UILayer *)sceneUILayer;
#end
GamePlayLayer.m
UILayer * sr = (UILayer *)[self.parent getChildByTag:10];
CCSprite * live = [sr.lives objectAtIndex:self.lives];
[live setVisible:NO];
Allright I have to add the following information:
The UILayer.h (The UILayer is the HUD layer)file is :
#interface UILayer : CCLayer {
NSMutableArray *lives;
}
#property (nonatomic,retain) NSMutableArray *lives;
The UILayer.m is:
#import "UILayer.h"
#import "GameManager.h"
#implementation UILayer
#synthesize lives;
- (id)init {
if ((self = [super init])) {
lives = [[NSMutableArray arrayWithCapacity:3]retain];
for(int i=0;i<3;i++)
{
CCSprite * life = [CCSprite spriteWithFile:#"life_Label.png"];
[life setPosition:ccp(winSize.width/18+ 32*i,290)];
[self addChild:life];
[lives addObject:life];
}
}
The way that the compiler sees your method -setVisible is a setter method for a property visible. I'm guessing that isn't what you intended. Try renaming your method to something like [live visible:NO]; or anything other than -set.
are you defining a property for lives?
e.g. in your .h:
#interface blah
// ...
#property (assign) NSInteger lives;
#end
then in your .m:
#implementation blah
#synthesize lives = _lives;
//your code
#end
You could:
have no #property int lives; in header file
have no #synthesize lives; in implementation file.
have lives as something else than int (or convertible to int).
But it's just a poor guessing with the snippet you have provided.
Related
My GameScene.m file :
#import "GameScene.h"
// Needed to obtain the Navigation Controller
#import "AppDelegate.h"
#pragma mark - GameScene
// GameScene implementation
#implementation GameScene
// on "init" you need to initialize your instance
-(id) init
{
self = [super init];
if (self != nil)
{
[self addChild:[GameLayer node]];
}
return self;
}
- (void) dealloc
{
[super dealloc];
}
#end
#implementation GameLayer
#synthesize hero;
-(id) init
{
if( (self=[super init] ))
{
hero = [[Hero alloc] initWithGame:self];
}
return self;
}
-(void) dealloc
{
[super dealloc];
}
#end
My GameScene.h file :
#import <GameKit/GameKit.h>
// When you import this file, you import all the cocos2d classes
#import "cocos2d.h"
// GameScene
#interface GameScene : CCScene
{
}
#end
#class Hero;
#interface GameLayer : CCLayer
{
Hero * _hero;
NSMutableArray * _bullets;
bool _playerFiring;
NSMutableArray * _enemies;
float _lastTimeEnemyLaunched;
float _enemyInterval;
int _score;
int _lives;
int _bombs;
int _level;
}
#property (nonatomic,retain) Hero * hero;
#property (nonatomic,readwrite) bool playerFiring;
#property (nonatomic,readwrite) float lastTimeEnemyLaunched;
#property (nonatomic,readwrite) float enemyInterval;
#property (nonatomic,retain) NSMutableArray * enemies;
#property (nonatomic,retain) NSMutableArray * bullets;
#property (assign,readwrite) int score;
#property (assign,readwrite) int lives;
#property (assign,readwrite) int bombs;
#property (assign,readwrite) int level;
#end
My Hero.h file :
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "GameScene.h"
#class GameLayer;
#interface Hero : CCNode
{
CCSprite * mySprite;
GameLayer * theGame;
float _lastTimeFired;
float _fireInterval;
float _firingSpeed;
float _movementSpeed;
}
#property (nonatomic,retain) CCSprite * mySprite;
#property (nonatomic,retain) GameLayer * theGame;
#property (nonatomic,readwrite) float lastTimeFired;
#property (nonatomic,readwrite) float fireInterval;
#property (nonatomic,readwrite) float firingSpeed;
#property (nonatomic,readwrite) float movementSpeed;
#end
And my Hero.m file :
#import "Hero.h"
#implementation Hero
#synthesize theGame,mySprite;
-(id) initWithGame:(GameLayer *)game
{
self = [super init];
if(self != nil)
{
// ask director for the window size
CGSize size = [[CCDirector sharedDirector] winSize];
self.theGame = game;
mySprite = [CCSprite spriteWithFile:#"hero.png"];
[theGame addChild:mySprite z:2];
[mySprite setPosition:ccp(size.width/2,50)];
self.lastTimeFired = 0;
self.fireInterval = 3;
self.firingSpeed = 10;
self.movementSpeed = 5;
}
return self;
}
-(void) dealloc
{
[super dealloc];
}
#end
And here's my problem : I get two warnings - 1. "instance method -initWithGame not found (return type default to 'id')" and 2. "Receiver 'Hero' is a forward class and corresponding #interface may not exist"
I tried to add "-(id) initWithGame:(GameLayer *)game" line to Hero.h interface, but it won't work. I tried to add that line but with + instead of -, but nothing.
I end up without my Hero displayed on a screen. Does anyone knows how to solve this problem (I use newest version of Xcode)?
In GameScene.m, you should
#import "Hero.h"
This explains why you get the "forward class" warning: since you did not import the header, the only thing known to the compiler in the GameScene compilation unit is the forward declaration.
Once you do that, if you also declare initWithGame in Hero.h, then you won't get any warning.
Been programming an iOS/objective-c/cocos2d app. Things have been going fine up until this snag... I should note that it's Objective-C++, ie. .mm files, compiling for the iOS 5.0 simulator.
Basically, I'm only trying to subclass an interface. The error is produced on the #interface line: "Cannot find interface declaration for GameObject"
#import <Foundation/Foundation.h>
#import "CCSprite.h"
#import "cocos2d.h"
#import "Box2D.h"
#import "GameObject.h"
#interface AvalancheBlock : GameObject
{
}
#end
Here's the code for GameObject.h:
#import <UIKit/UIKit.h>
#import "cocos2d.h"
#import "Box2D.h"
#import "b2Body.h"
#import "OhSnowGame.h"
#interface GameObject : CCNode {
#public
b2Body *body;
b2BodyDef *bodyDef;
b2FixtureDef *fixtureDef;
b2PolygonShape *polygonShape;
b2CircleShape *circleShape;
CCSprite *sprite;
int typeTag;
bool markedForDestruction;
}
#property (readwrite, assign) b2Body *body;
#property (nonatomic, assign) b2BodyDef *bodyDef;
#property (nonatomic, assign) b2FixtureDef *fixtureDef;
#property (nonatomic, assign) b2PolygonShape *polygonShape;
#property (nonatomic, assign) b2CircleShape *circleShape;
#property (nonatomic, retain) CCSprite *sprite;
#property (readwrite, assign) int typeTag;
#property (readwrite, assign) bool markedForDestruction;
#property (readonly) int type;
-(id) init;
-(void) initBox2D;
-(int) type;
#end
And GameObject.mm:
#import "GameObject.h"
#implementation GameObject
#synthesize body, bodyDef, fixtureDef, polygonShape, circleShape, sprite, type, typeTag, markedForDestruction;
-(id) init {
if( (self=[super init]) ) {
[self initBox2D];
markedForDestruction = NO;
}
return self;
}
-(void) initBox2D {
bodyDef = new b2BodyDef();
fixtureDef = new b2FixtureDef();
//Initial fixture settings
fixtureDef->density = 1.0f;
fixtureDef->friction = 0.5f;
fixtureDef->restitution = 0.3f;
bodyDef->userData = self;
}
-(int) type {
return 0;
}
#end
If the correct file is being included... then why the error?
Thanks!
SOLVED:
Figured it out... there WAS a circular reference, but I still needed the reference. The solution was to remove the #import "OhSnowGame.h" from GameObject.h, and instead add the directive:
#class OhSnowGame
Learn something new every day.
SECOND UPDATE:
New issue, but very much related.
I have OhSnowGame.mm which includes a "AvalancheGrid.h".
AvalancheGrid.h includes AvalancheParams.h.
In OhSnowGame, I'm referencing both AvalancheGrid and AvalancheParams.
The compiler is throwing a linker error:
"Duplicate symbol AvalancheParams in OhSnowGame.o and AvalancheGrid.o"
But really it doesn't seem like there's any circular reference to me.
If I replace the #import "AvalancheGrid.h" in OhSnowGame.mm with just
#class AvalancheGrid; (and also #class AvalancheParams.h;)
then it doesn't let me reference the properties of those objects like I need to, ie:
avParams.severity = 3; //nope
avParams->severity = 3; //nope
[avParams setSeverity:3]; //nope (with #property severity exposed and synthesized)
I don't understand. How can I avoid the linker error and still access those properties?
I have a subclass of UIImageView and would like to pass self as a parameter to the delegate. I get a error "Expected ')' before MyImageView". I need to pass the object to the delegate so the delegate can read certain properties from the object.
#import <UIKit/UIKit.h>
#protocol MyImageViewDelegate <NSObject>
-(void) buttonPressedForView:(MyImageView *) imageView; //line with ERROR...
#end
#interface MyImageView : UIImageView {
id <MyImageViewDelegate> delegate;
BOOL _imageMode;
}
#property (nonatomic,assign) id <MyImageViewDelegate> delegate;
#property (nonatomic,readonly) BOOL imageMode;
I think you need a forward declaration of MyImageView before your protocol declaration.
#class MyImageView;
#protocol MyImageViewDelegate <NSObject>
-(void) buttonPressedForView:(MyImageView *) imageView;
#end
Maybe you should define the protocol after defining the MyImageView interface?? it seems that the method its not recognizing MyImageView class type, or just define the protocol in a new .h and import your current .h.
Try adding this:
#import <UIKit/UIKit.h>
#class MyImageView; <<ADD THIS LINE
#protocol MyImageViewDelegate <NSObject>
-(void) buttonPressedForView:(MyImageView *) imageView; //line with ERROR...
#end
#interface MyImageView : UIImageView {
id <MyImageViewDelegate> delegate;
BOOL _imageMode;
}
#property (nonatomic,assign) id <MyImageViewDelegate> delegate;
#property (nonatomic,readonly) BOOL imageMode;
A quick question if I may, can anyone explain what I am missing below, I was assuming the 3rd one with the would work?
#interface ...
// These work
#property(assign) SomeClass *someDelegate;
#property(assign) id someDelegate;
// This gives warning
#property(assign) id <SomeClassDelegate> someDelegate;
.
#implementation ...
#synthesize someDelegate;
[self setSomeDelegate:[[SomeClass alloc] init]];
[someDelegate setDelegate:self];
.
warning: method '-setDelegate:' not found (return type defaults to 'id')
EDIT_001:
// SomeClass.h
#import <Foundation/Foundation.h>
#class SomeClass;
#protocol SomeClassDelegate <NSObject>
#optional
-(void)didHappen:(SomeClass *)someClass;
#required
-(void)willUse:(SomeClass *)someClass withThis:(BOOL)flag;
#end
#interface SomeClass : NSObject {
id <SomeClassDelegate> delegate;
}
#property(assign) id <SomeClassDelegate> delegate;
-(void)otherActions;
#end
cheers Gary.
Go protocols!
#protocol MyDelegateProtocol
- (NSNumber*) someFunction:(NSArray*) anArray;
#end
#interface MyClass : NSObject {
id<MyDelegateProtocol> delegate;
}
#property id<MyDelegateProtocol> delegate
#end
Then in your #implementation:
#synthesize delegate;
As far as I know, the Cocoa way :-)
Cheers
Nik
Unless it exists and you didn't show it, you don't have a setDelegate method. You have a setSomeDelegate method, though.
I simply want to change a variable of an object from another class. I can compile without a problem, but my variable always is set to 'null'.
I used the following code:
Object.h:
#interface Object : NSObject {
//...
NSString *color;
//...
}
#property(nonatomic, retain) NSString* color;
+ (id)Object;
- (void)setColor:(NSString*)col;
- (NSString*)getColor;
#end
Object.m:
+(id)Object{
return [[[Object alloc] init] autorelease];
}
- (void)setColor:(NSString*)col {
self.color = col;
}
- (NSString*)getColor {
return self.color;
}
MyViewController.h
#import "Object.h"
#interface ClassesTestViewController : UIViewController {
Object *myObject;
UILabel *label1;
}
#property UILabel *label1;
#property (assign) Object *myObject;
#end
MyViewController.m:
#import "Object.h"
#implementation MyViewController
#synthesize myObject;
- (void)viewDidLoad {
[myObject setColor:#"red"];
NSLog(#"Color = %#", [myObject getColor]);
[super viewDidLoad];
}
The NSLog message is always Color = (null)
I tried many different ways to solve this problem, but no success.
Any help would be appreciated.
Thanks for the help so far.
I modified the code as follow, but it still doesn't work as it should.
MyViewController.h:
#import <UIKit/UIKit.h>
#import "Object.h"
#interface MyViewController : UIViewController {
Object *myObject;
}
#property (nonatomic, retain) Object *myObject;
#end
MyViewController.m:
#import "MyViewController.h"
#import "Object.h"
#implementation MyViewController
#synthesize myObject;
- (void)viewDidLoad {
Object *myObject = [Object new];
myObject = 0;
[myObject setColor:#"red"];
NSLog(#"color = %#", myObject.color);
[super viewDidLoad];
}
If I do it like this, NSLog returns color = null (and I think myObject is only visible in viewDidLoad). How can declare myObject and make it visible in MyViewController?
I stripped down my Object class to
Object.h:
#interface Object : NSObject {
NSString *color;
}
#property(nonatomic, retain) NSString *color;
#end
Object.m:
#import "Object.h"
#implementation Object
#synthesize color;
#end
I wasn't able to define an object myObject in ViewDidLoad so that I can access its properties from the whole ViewController class? What did I miss?
Side question: Why do I have to set myObject to 0?
You're declaring a property, then explicitly declaring the accessors in Object.h. You only need to do one or the other - they mean the same thing (well, almost - you'll have color instead of getColor)
To implement the property in Object.m you should use #synthesize color. The explicit implementations, again, are then redundant (unless they do anything extra).
The explicit setColor implementation in Object.m is calling the property - which you are implementing explicitly, so I would have expected you to get an infinite recursion here.
MyViewController.m should probably synthesize label1, since you declare the property in the header (although it's not being used in your snippet).
[myObject getColor] is calling the color property, which you declared but did not synthesize. If you had explicitly implemented it as color it would have picked that up - but it won't match getColor (which is fortunately as that would have led to an infinite recursion again.
I don't see anywhere where you create your myObject instance. If you don't it will be nil and methods called on it (including property accesses) will return 0 or nil.
I suspect (6) is the cause of your issue, but the others need to be addressed too. Make sure you read up on property syntax.