main layer with another layer not showing variables cocos2d - cocos2d-iphone

I'm having a strange problem... I have a custom class with sprite and booleans properties
In my main GameLayer, I have CCLayerColor *my1layer and in my .m section I have this code:
my1layer = [CCLayerColor layerWithColor:ccc4(144,238,144, 255)];
[self addChild: my1layer z:5 tag:100];
[my1layer setContentSize:CGSizeMake(200, 1280)];`
//default start position
my1layer =ccp(s.width/2-420, s.height-s.height+180.);
MylmageItem *postImage1 = [[MylmageItem alloc] initWithLayer:self];
[postImage1 release];
postImage1.postImageNum=CCRANDOM_0_1()*12 +1;
postImage1.postImage = [CCSprite spriteWithSpriteFrameName:(NSString*)[self assignPostImage:postImage1.postImageNum ]];
postImage1.postImage.position =ccp(my1layer.contentSize.width/2, my1layer.contentSize.height-my1layer.contentSize.height+80);
[my1layer addChild:myImage1.slotImage z:6 tag:101];’
code above does work and shows the images on layer, however when I try to access the property postImage1.postImageNum, it shows 0 always...
I do this: CCLayer *myLayer1 = (CCLayer*)[self getChildByTag:100]; later to retrieve my object...
MyImageItem *tmpImage1 = (MymageItem*)[myLayer1 getChildByTag:101]; from here I always get 0 when I do CCLOG(#"postImage1.postImageNum %d",tmpImage1.postImageNum);
How do I get data from my1layer?

Related

Set the physicsNode.debugDraw = TRUE crash

I am studying the SpriteBuilder to make the example Peeved Penguins,but on the Joint step I set the _physicsNode.debugDraw = TRUE,it cash when I run my game,I don't know how to fix it.
If this is in obj-c, then you enable the debugDraw when you define the physicsWorld properties like this:
physicsWorld = [CCPhysicsNode node];
physicsWorld.gravity = ccp(0,0);
physicsWorld.debugDraw = NO;
physicsWorld.collisionDelegate = self;
[self addChild:physicsWorld];
This is assuming that you have created a physics world

CCNode tag - Shows all tags are same

I am new to cocos2d game development.I have subclassed a CCNode,and i am looping to add this subclass to the game layer setting the tag for this node.In the m file of this subclass i am using the touches delegate to detect the element.But the console shows that for any object of this node created tag's are the same.
You must initialise tag for each node. Use uniq integer number as tag. Better use enum.
typedef enum
{
kEnemy_Dog = 1000,
kEnemy_Cat,
kEnemy_Rat,
kEnemy_Bat
}EnemyType;
typedef enum
{
kHeroType_Lion = 2000,
kHeroType_Tiger,
kHeroType_Dino,
kEnemy_Eagle
}HeroType;
CCSprite *dog = [CCSprite spriteWithFile:TEX_DOG];
dog.tag = kEnemy_Dog;
[self addChild:dog z:2 tag: kEnemy_Dog];
CCSprite *lion = [CCSprite spriteWithFile:TEX_DOG];
lion.tag = kHeroType_Lion;
[self addChild:dog z:2 tag: kHeroType_Lion];
//to check
if(inSprite.tag == kHeroType_Lion)

No visible #interface for 'class' declares the selector 'selector'

I'm following "Learn Cocos2D" and in chapter 4 I'm met with the following directive:
And in the GameLayer init method, add the call to the initSpiders method discussed next, right after scheduleUpdate:
-(id) init {
if ((self=[super init])) {
...
[self scheduleUpdate];
[self initSpiders];
}
return self;
}
I get and ARC error message: no visible #interface for 'GameLayer' declares the selector 'initSpiders'
I get the same message at the line: self resetSpiders
what am i missing? everything builds and runs great up to that point.
This issue derives from the fact that the initSpiders and resetSpiders are not declared in your class interface and are defined in the .m file after the point where they are used.
If they are not missing altogether, you can fix this in either of 2 ways:
move the definition of the initSpiders and resetSpiders methods above your init method and the errors will disappear;
add a declaration for both methods in the #interface of the class.
(If you do both, it will also work)
Check your code to see if the implementation for those methods is available.
Your error appears to be that you haven't followed the next bit of the book too. Completing the next section should allow you to compile your code without warnings like this.
A more complete extract of that section of the book is:
And in the GameScene init method add the call to the initSpiders method discussed next, right after scheduleUpdate:
-(id) init {
if ((self = [super init]))
{
… 96 CHAPTER 4: Your First Game
[self scheduleUpdate];
[self initSpiders];
}
return self;
}
After that a fair bit of code is added to the GameScene class, beginning with the initSpiders method in Listing 4–8, which is creating the spider sprites.
Listing 4–8. For Easier Access, Spider Sprites Are Initialized and Added to a CCArray
-(void) initSpiders
{
CGSize screenSize = [[CCDirector sharedDirector] winSize];
// using a temporary spider sprite is the easiest way to get the image's size
CCSprite* tempSpider = [CCSprite spriteWithFile:#"spider.png"];
float imageWidth = [tempSpider texture].contentSize.width;
// Use as many spiders as can fit next to each other over the whole screen width.
int numSpiders = screenSize.width / imageWidth;
// Initialize the spiders array using alloc.
spiders = [[CCArray alloc] initWithCapacity:numSpiders];
for (int i = 0; i < numSpiders; i++)
{
CCSprite* spider = [CCSprite spriteWithFile:#"spider.png"];
[self addChild:spider z:0 tag:2];
// Also add the spider to the spiders array.
[spiders addObject:spider];
}
// call the method to reposition all spiders
[self resetSpiders];
}

Cocos2d - Orbiting/spinning one sprite around another

I've got to sprites, essentially a nucleus (parent) and an electron (child). I'm trying to find a way to have the electron orbit the nucleus. I've found a few posts here and there on moving anchor points, but that is apparently related to the texture of the sprite, not the sprite itself.
This is my current init method for my parent sprite:
self = [super init];
if (self) {
CCSprite *orbitAnchor = [CCSprite new];
[self addChild:orbitAnchor];
orbitAnchor.position = ccp(32,32);
CCSprite *orbiter = [CCSprite spriteWithFile:#"line.png" rect:CGRectMake(0, 0, 8, 8)];
[orbitAnchor addChild:orbiter];
orbiter.position = ccp(40,40);
CCAction *orbitAction = [CCRepeatForever actionWithAction:[CCRotateTo actionWithDuration:1 angle:720]];
[orbitAnchor runAction:orbitAction];
[self initAnimations];
}
return self;
Those numbers are all totally arbitrary though - I just stuck them in there and got what looked best. I'm sure there's a more programatic way to do what I want.
Basically, I'm looking for a way to set the child's axis point at the center of the parent, and then have it rotate around that point. It seems like a relatively simple thing to do using CCRotate and such, but I think I'm missing what to search for in order to move the anchor point of the child. Any suggestions/other posts?
Thanks
You have [CCSprite new] which is an unusual use, probably not supported. Unless the orbit anchor node should display a texture, you can just use a CCNode as anchor.
Technically you're doing everything correct from what I can see. You might want to try without the rotate actions and instead change the direction manually in a scheduled update method.
CCNode *orbitAnchor = [CCNode node];
[self addChild:orbitAnchor z:0 tag:1];
orbitAnchor.position = ccp(32,32);
CCSprite *orbiter = [CCSprite spriteWithFile:#"line.png" rect:CGRectMake(0, 0, 8, 8)];
[orbitAnchor addChild:orbiter z:0 tag:2];
orbiter.position = ccp(40,40);
[self scheduleUpdate];
Update method:
-(void) update:(ccTime)delta
{
CCNode* orbitAnchor = [self getChildByTag:1];
orbitAnchor.direction += 5;
}
From the image filename it looks like you're trying to draw a line from the orbitAnchor to the orbiter. You can do that with ccDrawLine:
-(void) draw
{
CCNode* orbitAnchor = [self getChildByTag:1];
CCNode* orbiter = [self getChildByTag:2];
ccDrawLine(orbitAnchor.position, orbiter.position);
}
Anchor points. Automatically, objects are placed based on the center of an object right? with anchor points you can move that around, so if you rotate the object it will rotate around the anchorpoint. http://www.qcmat.com/understanding-anchorpoint-in-cocos2d/

cocos2d scene retaining issue

There is a scene in my application which has only two labels and a menu item. When I load this scene using replaceScene method it stays for 3-4 seconds and then gets disappeared or released. I want to keep it until cancel button is pressed. How can I do it? code is:
#implementation MyLayer
+ (id)myScene {
CCScene *aScene = [CCScene node];
MYLayer *myLayer = [MyLayer node];
[aScene addChild:myLayer];
return aScene;
}
- (id) init {
if (self = [super init]) {
//labels and menu here
}
return self;
}
And I am calling it from another scene like this:
[[CCDirector sharedDirector] replaceScene: [MyLayer myScene]];
Maybe the problem is that it's your first scene. Then you should use runWithScene method of CCDirector.
did you try replacing that scene with a "empty" init function to see if it still releases itself? It might be because of the amount of textures you are putting into memory
I did have sort of similar problems before because the images used in the new scene is too big and got auto purged by my app delegate, thus returning me an empty scene sometimes