No known class method for selector 'generatorWithWorld:' - ios

I'm trying to create a platform game on xcode and this my first ever project and I've came across this error and it says "No known class method for selector 'generatorWithWorld:'" and i'm also getting the error "No visible #interface for 'HSWorldGenerator' declares the selector 'populate'"
This is the code for MyScene.h:
//
// MyScene.h
// The Adventure of Colin The Sloth
//
// Copyright (c) 2014 SunnersCorp. All rights reserved.
//
**#import <SpriteKit/SpriteKit.h>
#import "HSWorldGenerator.h"**
#interface MyScene : SKScene
#end
MyScence.m:
//
// MyScene.m
// The Adventure of Colin The Sloth
//
// Created by Harpreet Sunner on 24/07/2014.
// Copyright (c) 2014 SunnersCorp. All rights reserved.
//
#import "MyScene.h"
#import "HSSloth.h"
#interface MyScene()
#property BOOL isStarted;
#property BOOL isGameOver;
#end
#implementation MyScene
{
HSSloth *Sloth;
SKNode *world;
HSWorldGenerator *generator;
}
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
/* Setup your scene here */
self.anchorPoint = CGPointMake (0.5, 0.5);
self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];
world = [SKNode node];
[self addChild:world];
generator = [HSWorldGenerator generatorWithWorld:world]; **THIS IS WHERE I GET THE FIRST ERROR**
[self addChild:generator];
[generator populate]; **THIS IS WHERE I GET THE SECOND ERROR**
Sloth = [HSSloth Sloth];
[world addChild:Sloth];
}
return self;
}
- (void)start
{
self.isStarted = YES;
[Sloth start];
}
-(void)clear
{
NSLog(#"clear method called");
}
- (void)gameOver
{
NSLog(#"gameOver method called");
}
- (void)didSimulatePhysics
{
[self centerOrNode:Sloth];
}
- (void)centerOrNode:(SKNode *)node
{
CGPoint postionInScene = [self convertPoint:node.position fromNode:node.parent];
world.position = CGPointMake(world.position.x - postionInScene.x, world.position.y);
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (!self.isStarted)
[self start];
else if (self.isGameOver)
[self clear];
else
[Sloth jump];
}
-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
}
#end
HSWorldGenerator.h:
//
// HSWorldGenerator.h
// The Adventure of Colin The Sloth
//
// Created by Harpreet Sunner on 24/07/2014.
// Copyright (c) 2014 SunnersCorp. All rights reserved.
//
#import <SpriteKit/SpriteKit.h>
#interface HSWorldGenerator : SKNode
+ (id)generatorWithWorld:(SKNode *)world;
- (void)populate;
- (void)generate;
#end
HSWorldGenerator.m:
//
// HSWorldGenerator.m
// The Adventure of Colin The Sloth
//
// Created by Harpreet Sunner on 24/07/2014.
// Copyright (c) 2014 SunnersCorp. All rights reserved.
//
#import "HSWorldGenerator.h"
#interface HSWorldGenerator ()
#property double currentGroundX;
#property double currentObstacleX;
#property SKNode *world;
#end
#implementation HSWorldGenerator
+ (id)generatorWithWorld:(SKNode *)world
{
HSWorldGenerator *generator = [HSWorldGenerator node];
generator.currentGroundX = 0;
generator.currentObstacleX = 400;
generator.world = world;
return generator;
}
- (void)populate
{
for (int i = 0; 1 <3; i++)
[self generate];
}
- (void)generate
{
SKSpriteNode *ground = [SKSpriteNode spriteNodeWithColor:[UIColor greenColor] size:CGSizeMake(self.scene.frame.size.width, 100)];
ground.position = CGPointMake(self.currentGroundX, -self.scene.frame.size.height/2 + ground.frame.size.height/2);
ground.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:ground.size];
ground.physicsBody.dynamic = NO;
[self.world addChild:ground];
self.currentGroundX += ground.frame.size.width;
SKSpriteNode *obstacle = [SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(40, 70)];
obstacle.position = CGPointMake(self.currentObstacleX, ground.position.y + ground.frame.size.height/2 + obstacle.frame.size.height/2);
obstacle.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:obstacle.size];
obstacle.physicsBody.dynamic = NO;
[self.world addChild:obstacle];
self.currentObstacleX += 250;
}
#end
Could anyone please tell me what ive done wrong and how I can fix this please, I will appreciate it so much.
Thank You!

Check where you #import HSWorldGenerator.h into your MyScene.h file.
The error is telling you that it doesn't know where to find the class method.
Looks like where you try to import the header in MyScene.h you have extra '**'s that might be commenting out that line.

Related

add two custom sprites into a scene,why do these sprites' methods will effect each other,anyone can find the mistake in my code? thanks

this is custom sprite class named BackGround
#import "BackGround.h"
// -----------------------------------------------------------------
id move02;
double roadX;
#implementation BackGround
+ (instancetype)initWithPicture: (NSString *) pic
{
return [[self alloc] init:pic];
}
-(id) init: (NSString *) pic
{
if(self = [super init])
{
CCSpriteFrameCache* spriteFrameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
CCSpriteFrame * bgSpriteFrame = [spriteFrameCache spriteFrameByName:pic];
self = [BackGround spriteWithSpriteFrame:bgSpriteFrame];
self.anchorPoint = ccp(0, 0);
roadX = -(self.contentSize.width-1)*2;
self.position = ccp((-roadX/2), 0);
id move01 = [CCActionMoveBy actionWithDuration:10.0f position:ccp(roadX,0.0)];
move02 = [CCActionRepeatForever actionWithAction:move01];
[self runAction:move02];
}
return self;
}
-(void)bgWhenRun
{
[self stopAllActions];
id move = [CCActionSpeed actionWithAction:move02 speed:2];
[self runAction:move];
}
-(void)bgWhenWalk
{
[self stopAllActions];
[self runAction:move02];
}
// -----------------------------------------------------------------
#end
this is scene class code
#import "_256Deathes.h"
#import "IntroScene.h"
#import "BackGround.h"
#import "cocos2d.h"
#import "Person.h"
// -----------------------------------------------------------------
Person * personA;
#implementation _256Deathes
{
}
- (instancetype)init
{
if ((self = [super init]))
{
NSAssert(self, #"Whoops");
self.userInteractionEnabled = YES;
CCSpriteFrameCache* spriteFrameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
[spriteFrameCache addSpriteFramesWithFile:#"256Deathes.plist"];
BackGround * bgSprite01 = [BackGround initWithPicture:#"earthA.png"];
bgSprite01.position = ccp(0, 0);
[self addChild:bgSprite01 z:0 name:#"bgSpriteA"];
BackGround * bgSprite02 = [BackGround initWithPicture:#"earthA.png"];
[self addChild:bgSprite02 z:1 name:#"bgSpriteB"];
}
return self;
}
- (void)onEnter
{
// always call super onEnter first
[super onEnter];
[self schedule:#selector(updateSprite) interval:0.02];
}
-(void)touchBegan:(CCTouch *)touch withEvent:(CCTouchEvent *)event
{
BackGround *spriteA = (BackGround *)[self getChildByName:#"bgSpriteA" recursively:NO];
BackGround *spriteB = (BackGround *)[self getChildByName:#"bgSpriteB" recursively:NO];
[spriteA bgWhenRun];
[spriteB bgWhenRun];
}
-(void)touchEnded:(CCTouch *)touch withEvent:(CCTouchEvent *)event
{
BackGround *sprite;
for(sprite in [self children])
{
if([sprite.name containsString:#"bgSprite"])
{
[sprite bgWhenWalk];
}
}
}
-(void) updateSprite
{
[self updateBackGround01];
}
-(void) updateBackGround01
{
BackGround *sprite;
for(sprite in [self children])
{
if([sprite.name containsString:#"bgSprite"])
{
double nextX = sprite.contentSize.width-3;
if(sprite.position.x <= (-nextX))
{
sprite.position = ccp(nextX, 0);
}
}
}
}
// -----------------------------------------------------------------
#end
when i touch begin or touch end, spriteA will stop moving, after i tried some times, i found [self stopAllActions] in methods named bgWhenRun and bgWhenWalk can make spriteA and spriteB effect each other.
anyone can find out the mistakes in the code then tell me?i have tried many times,now i really have no idea. thank you!
These two sprites effect each other because both are using same instance of variables id move02 and double roadX as being global ones. Declare them within scope of BackGround class i.e. as member variables of that class.

Draw rectangle in spritekit

I'm trying to draw an empty rectangle with a SKShapeNode but does appear on the screen. If i set a color to the fill property the rectangle appears. What could be the issue?
//MyScene.h
#import <SpriteKit/SpriteKit.h>
#interface MyScene : SKScene
#end
//MyScene.m
#import "MyScene.h"
#implementation MyScene
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
SKShapeNode *shapeNode = [SKShapeNode shapeNodeWithRectOfSize:CGSizeMake(self.frame.size.width/2, self.frame.size.height/2)];
shapeNode.strokeColor = [SKColor redColor];
shapeNode.lineWidth = 3;
[self addChild:shapeNode];
}
return self;
}
#end

How to make 2 objects collide with each other instead of passing through each other in Spritekit

I am using spritekit to create a simple running game. I have a stickman and the ground, and I want the stickman to collide with the ground when he falls to it. However, he simply passes through the ground and out of the frame. Any ideas?
//
// GameplayScene.h
// CodeRun
//
// Created by Sebastian Cain on 8/16/14.
// Copyright (c) 2014 ASX Software. All rights reserved.
//
#import <SpriteKit/SpriteKit.h>
#interface GameplayScene : SKScene
typedef NS_OPTIONS(uint32_t , ASXCollisionCategory){
ASXCollisionCategoryGround = 1 << 0,
ASXCollisionCategoryStickman = 1 << 1
};
#end
//
// Ground.m
// CodeRun
//
// Created by Sebastian Cain on 8/16/14.
// Copyright (c) 2014 ASX Software. All rights reserved.
//
#import "Ground.h"
#import "GameplayScene.h"
#implementation Ground
+ (instancetype)ground:(CGPoint)position{
Ground *groundNode = [Ground spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(960, 50)];
groundNode.anchorPoint = CGPointMake(0, 0);
groundNode.position = position;
groundNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(960, 50)];
groundNode.physicsBody.affectedByGravity = YES;
groundNode.physicsBody.dynamic = NO;
groundNode.physicsBody.mass = 1000;
groundNode.physicsBody.categoryBitMask = ASXCollisionCategoryGround;
groundNode.physicsBody.collisionBitMask = ASXCollisionCategoryStickman;
return groundNode;
}
#end
#import "Stickman.h"
#import "GameplayScene.h"
#implementation Stickman
+ (instancetype)stickman: (CGPoint)position{
Stickman *stickmanNode = [Stickman spriteNodeWithImageNamed:#"stick - Frame 1"];
stickmanNode.anchorPoint = CGPointMake(0.5, 0);
stickmanNode.position = position;
stickmanNode.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:25.0 center:CGPointMake(CGRectGetMidX(stickmanNode.frame), CGRectGetMidY(stickmanNode.frame))];
stickmanNode.physicsBody.mass = 50;
stickmanNode.physicsBody.affectedByGravity = YES;
stickmanNode.physicsBody.categoryBitMask = ASXCollisionCategoryStickman;
stickmanNode.physicsBody.collisionBitMask = ASXCollisionCategoryGround;
NSArray *textures = #[
[SKTexture textureWithImageNamed:#"stick - Frame 1"],
[SKTexture textureWithImageNamed:#"stick - Frame 2"],
[SKTexture textureWithImageNamed:#"stick - Frame 3"],
[SKTexture textureWithImageNamed:#"stick - Frame 4"],
[SKTexture textureWithImageNamed:#"stick - Frame 5"],
[SKTexture textureWithImageNamed:#"stick - Frame 6"],
[SKTexture textureWithImageNamed:#"stick - Frame 7"],
[SKTexture textureWithImageNamed:#"stick - Frame 8"],
[SKTexture textureWithImageNamed:#"stick - Frame 9"],
[SKTexture textureWithImageNamed:#"stick - Frame 10"]];
SKAction *stickmanAnimation = [SKAction animateWithTextures:textures timePerFrame:0.05];
SKAction *stickmanRepeat = [SKAction repeatActionForever:stickmanAnimation];
[stickmanNode runAction:stickmanRepeat];
return stickmanNode;
}
#end
//
// GameplayScene.m
// CodeRun
//
// Created by Sebastian Cain on 8/16/14.
// Copyright (c) 2014 ASX Software. All rights reserved.
//
#import "GameplayScene.h"
#import "Stickman.h"
#import "Ground.h"
#implementation GameplayScene
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
/* Setup your scene here */
SKSpriteNode *backgroundNode = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:self.frame.size];
backgroundNode.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
[self addChild:backgroundNode];
Stickman *stickmanInGameplay = [Stickman stickman:CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))];
[self addChild:stickmanInGameplay];
Ground *groundinGameplay = [Ground ground:CGPointMake(0, 0)];
[self addChild:groundinGameplay];
}
return self;
}
#end
Are you adding Ground node to (0, 0) position with (960, 50) size? In this case the player will be spawned inside that physics body!
To implement the ground, get rid of Ground node and put the following into GameplayScene init method:
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
self.physicsBody.categoryBitMask = ASXCollisionCategoryGround;
That's it.

-[_NSCFConstantString texture] error in Cocos2D

I am trying to create sprite animations for a game where when a user clicks on a button, it will spawn 1 unit/enemy, which will move across the screen in a running animation. At the moment, when I run the game, it will play and once I attempt to spawn a unit, the game crashes and gives me a -[__NSCFConstantString texture]: unrecognized selector sent to instance 0x11a15c error.
This is the header file for the Unit itself:
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "mainGameLayer.h"
#class MainGameLayer, Waypoint, Tower;
#interface Unit : CCSprite {
CGPoint myPosition;
int maxHP;
int currentHP;
float walkingSpeed;
Waypoint *destinationWaypoint;
BOOL active;
float centerToBottom;
float centerToSides;
}
#property (nonatomic, assign) MainGameLayer *theGame;
#property (nonatomic, assign) CCSprite *mySprite;
#property (nonatomic, strong) CCAction *walkAction;
+(id) nodeWithTheGame: (MainGameLayer *)_game;
-(id) initWithTheGame: (MainGameLayer *)_game;
-(void) doActivate;
-(void) getRemoved;
#end
Below is the implementation file. The 'self = super initWithSpriteFrame' line is currently throwing a warning saying "Incompatible pointer types sending NSString * to parameter of type 'CCSpriteFrame *'.
Also, the "CCSprite *frame = [[CCSpriterameCache....." line is throwing another warning saying 'Flag 0 results in undefined behaviour with p conversion specifier.
#import "Unit.h"
#import "Tower.h"
#import "Waypoint.h"
#define HEALTH_BAR_WIDTH 20
#define HEALTH_BAR_ORIGIN -10
#implementation Unit
#synthesize mySprite, theGame;
+(id) nodeWithTheGame:(MainGameLayer *)_game
{
return [[self alloc] initWithTheGame:_game];
}
-(id) initWithTheGame:(MainGameLayer *)_game
{
if ((self = [super initWithSpriteFrame:#"hero_walk_00.png"])) {
theGame = _game;
maxHP = 40;
currentHP = maxHP;
active = FALSE;
walkingSpeed = 0.5;
centerToBottom = 39.0;
centerToSides = 29.0;
CCArray *walkFrames = [CCArray arrayWithCapacity: 8];
for (int i = 0; i < 8; i++) {
CCSprite *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:#"hero_walk_%02.png", i]];
[walkFrames addObject: frame];
}
CCAnimation *walkAnimation = [CCAnimation animationWithSpriteFrames:[walkFrames getNSArray] delay:1.0/12.0];
self.walkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnimation]];
Waypoint *waypoint = (Waypoint *)[theGame.waypoints objectAtIndex:([theGame.waypoints count]-1)];
destinationWaypoint = waypoint.nextWaypoint;
CGPoint pos = waypoint.myPosition;
myPosition = pos;
[mySprite setPosition: pos];
[theGame addChild: self];
[self scheduleUpdate];
}
return self;
}
-(void) doActivate
{
active = TRUE;
}
-(void) update:(ccTime)dt
{
if (!active) {
return;
}
if ([theGame circle:myPosition withRadius:1 collisionWithCircle:destinationWaypoint.myPosition collisionCircleRadius:1]) {
if (destinationWaypoint.nextWaypoint) {
destinationWaypoint = destinationWaypoint.nextWaypoint;
} else {
[theGame getHpDamage];
[self getRemoved];
}
}
CGPoint targetPoint = destinationWaypoint.myPosition;
float movementSpeed = walkingSpeed;
CGPoint normalized = ccpNormalize(ccp(targetPoint.x - myPosition.x, targetPoint.y - myPosition.y));
mySprite.rotation = CC_RADIANS_TO_DEGREES(atan2(normalized.y, -normalized.x));
myPosition = ccp(myPosition.x + normalized.x * movementSpeed, myPosition.y + normalized.y * movementSpeed);
[mySprite setPosition: myPosition];
}
-(void) getRemoved
{
[self.parent removeChild:self cleanup:YES];
[theGame.units removeObject: self];
// Notify the game that we killed an enemy so we can check if we can send another wave
[theGame enemyGotKilled];
}
-(void) draw
{
ccDrawSolidRect(ccp(myPosition.x + HEALTH_BAR_ORIGIN, myPosition.y + 16), ccp(myPosition.x + HEALTH_BAR_ORIGIN + HEALTH_BAR_WIDTH, myPosition.y + 14), ccc4f(1.0, 0, 0, 1.0));
ccDrawSolidRect(ccp(myPosition.x + HEALTH_BAR_ORIGIN, myPosition.y + 16), ccp(myPosition.x + HEALTH_BAR_ORIGIN + (float)(currentHP * HEALTH_BAR_WIDTH)/maxHP, myPosition.y + 14), ccc4f(0, 1.0, 0, 1.0));
}
#end
Here is the header file of the main game layer:
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#interface MainGameLayer : CCLayer {
CCSpriteBatchNode *_actors;
}
+ (CCScene *) scene;
- (BOOL) circle:(CGPoint)circlePoint withRadius:(float)radius collisionWithCircle:(CGPoint)circlePointTwo collisionCircleRadius:(float)radiusTwo;
void ccFillPoly (CGPoint *poli, int points, BOOL closePolygon);
- (void) enemyGotKilled;
- (void) getHpDamage;
#property (nonatomic, strong) NSMutableArray *towers;
#property (nonatomic, strong) NSMutableArray *waypoints;
#property (nonatomic, strong) NSMutableArray *units;
#end
And the implementation file:
#import "MainGameLayer.h"
#import "Tower.h"
#import "Waypoint.h"
#import "Unit.h"
#implementation MainGameLayer
#synthesize towers;
#synthesize waypoints;
#synthesize units;
+ (CCScene *) scene
{
CCScene *scene = [CCScene node];
MainGameLayer *layer = [MainGameLayer node];
[scene addChild: layer];
return scene;
}
- (id) init
{
if ((self = [super init])) {
// Initialize
self.isTouchEnabled = TRUE;
CGSize winSize = [CCDirector sharedDirector].winSize;
// Setting the background (Map)
CCSprite *background = [CCSprite spriteWithFile:#"layout.png"];
[self addChild: background];
[background setPosition: ccp(winSize.width/2, winSize.height/2)];
[self addWaypoints];
// In Game Buttons / Menu
CCMenuItem *sampleButton = [CCMenuItemImage itemWithNormalImage:#"sample.jpg" selectedImage:#"sample.jpg" target:self selector:#selector(samplePurchased:)];
CCMenu *PurchaseUI = [CCMenu menuWithItems:sampleButton, nil];
[PurchaseUI setScale:0.5];
[PurchaseUI setPosition:ccp(63, 51)];
[PurchaseUI alignItemsHorizontally];
PurchaseUI.isTouchEnabled = TRUE;
[self addChild: PurchaseUI];
// Set up the sprite sheets (Currently in testing)
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"pd_sprites.plist"];
_actors = [CCSpriteBatchNode batchNodeWithFile:#"pd_sprites.pvr.ccz"];
[_actors.texture setAliasTexParameters];
[self addChild: _actors];
}
return self;
}
-(BOOL) canBuyTower
{
return YES;
}
-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches) {
CGPoint location = [touch locationInView: [touch view]];
location = [[CCDirector sharedDirector] convertToGL: location];
CCLOG(#"X: %f Y: %f", location.x, location.y);
if ([self canBuyTower]) {
// Spend the gold later
Tower *tower = [Tower nodeWithTheGame:self location: location];
[towers addObject: tower];
}
}
}
-(void) addWaypoints
{
waypoints = [[NSMutableArray alloc] init];
Waypoint * waypoint1 = [Waypoint nodeWithTheGame:self location:ccp(-25,360)];
[waypoints addObject:waypoint1];
Waypoint * waypoint2 = [Waypoint nodeWithTheGame:self location:ccp(73,360)];
[waypoints addObject:waypoint2];
waypoint2.nextWaypoint =waypoint1;
Waypoint * waypoint3 = [Waypoint nodeWithTheGame:self location:ccp(467,360)];
[waypoints addObject:waypoint3];
waypoint3.nextWaypoint =waypoint2;
Waypoint * waypoint4 = [Waypoint nodeWithTheGame:self location:ccp(905,360)];
[waypoints addObject:waypoint4];
waypoint4.nextWaypoint =waypoint3;
Waypoint * waypoint5 = [Waypoint nodeWithTheGame:self location:ccp(1050,360)];
[waypoints addObject:waypoint5];
waypoint5.nextWaypoint =waypoint4;
}
-(BOOL) circle:(CGPoint)circlePoint withRadius:(float)radius collisionWithCircle:(CGPoint)circlePointTwo collisionCircleRadius:(float)radiusTwo
{
float xdif = circlePoint.x - circlePointTwo.x;
float ydif = circlePoint.y - circlePointTwo.y;
float distance = sqrt(xdif*xdif + ydif*ydif);
if (distance <= radius + radiusTwo) {
return TRUE;
}
return FALSE;
}
-(void) samplePurchased: (id)sender
{
Unit *tempUnit = [Unit nodeWithTheGame: self];
[units addObject: tempUnit];
[tempUnit doActivate];
}
#end
I'm basically just working off some of the tutorials on the Ray Wenderlich website; I haven't run into any of these errors before and haven't been able to find much help on Google. Any help here is much appreciated! Thank you in advance!
EDIT: After making the changes I am now getting an invisible sprite move across the screen. The sprite's health bar will appear and move across the screen but not the actual sprite / animation itself. Any reasons for this?
[super initWithSpriteFrame:#"hero_walk_00.png"]
expects a CCSpriteFrame* as parameter, not a NSString*. This is why you get a compiler warning for this line - don't ignore compiler warnings!
The fix is simple, use the correct initializer:
[super initWithSpriteFrameName:#"hero_walk_00.png"]
And this returns a CCSpriteFrame* not a CCSprite*:
CCSprite *frame = [[CCSpriteFrameCache sharedSpriteFrameCache]
spriteFrameByName:[NSString stringWithFormat:#"hero_walk_%02.png", i]];
Hence the other warning. The fix:
CCSpriteFrame *frame = ...

Not accessing function in Objective-C

I'm currently trying to access a function on another file.(another layer, to be more precise).
Both layers are on a scene.
Third layer is trying to get a function from first layer...
Here's how I'm doing this:
Here's my scene in scene.h
#import "firstLayer.h"
#import "secondLayer.h"
#import "thirdLayer.h"
#interface myScene : CCScene
{
// custom instance vars here...
}
#end
Here's how I cast my scene in scene.m
-(id)init {
self = [super init];
if(self != nil){
firstLayer *firstLayerz = [firstLayer node];
[firstLayerz setTag:111];
[self addChild:firstLayerz z:0];
secondLayer *secondLayerz = [secondLayer node];
[secondLayer setTag:112];
[self addChild:secondLayer z:2];
thirdLayer *thirdLayerz = [thirdLayer node];
[thirdLayerz setTag:113];
[self addChild:thirdLayerz z:4];
Here's how I cast the function in thirdLayer.m
#import "scene.h"
#implementation thirdLayer.m
-(id)init {
self = [super init];
if(self != nil){
firstLayer* firstLayerz = (firstLayer*)[self.parent getChildByTag:111];
[firstLayerz functionNeeded];
}
Here's functionNeeded in firstLayer.m (right below init(
-(void)functionNeeded {
NSLog(#"inside fnnction needed");
}
Of course the log ain't showing...
I do the proper cast in firstLayer.h
#interface firstLayer : CCLayer {
}
-(void)functionNeeded;
#end
In your firstLayer's init method write
self.tag = 111;
Now in your ThirdLayer where you want to call method of first layer write :
CCScene *current = [[CCDirector sharedDirector] runningScene];
if (current) {
id layer = [current getChildByTag:111];
if (layer) {
[layer functionNeeded];
}
}

Resources