Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7605805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:15:33+00:00 2026-05-31T00:15:33+00:00

I’m working on a species ID app and would like to populate a layer

  • 0

I’m working on a species ID app and would like to populate a layer with sprites based on which animal you select on the main layer. I’ve made each animal a menu item, and can get my info layer to appear when pressing the button, but how can I set it up so the layer shows the right data depending on which animal you select? The info layer is not a full screen layer, but rather an overlaying layer that only fills about 75% of the screen, which is why I’m going with a layer rather than a scene. I know I can create a new layer for each animal (approx 50) and code it so each button calls its own layer, but I think populating based on which button is pressed would make for cleaner code. If flamingoButton is pressed, sprite is filled with flamingo.png and label is populated with flamingo information. How do I get my info layer to listen to the buttons on the main layer?

MainLayer.m code:

-(id) init
{
    if( (self=[super init])) 
    {        
        CCMenuItemImage *flamingoButton = [CCMenuItemImage itemFromNormalImage:@"Explore-sign.png" selectedImage:@"Explore-sign.png" target:self selector:@selector(showSecondLayer:)];
        flamingoButton.position = CGPointMake(0, 60);
        flamingoButton.tag = 101;
        CCMenu *menu = [CCMenu menuWithItems:flamingoButton, nil];
        [self addChild:menu];
    }
    return self;
}

-(void) showSecondLayer: (id) sender
{    
    CCMenuItemImage *item = (CCMenuItemImage *) sender;
    int itemID = item.tag;

    secondLayer = [SecondLayer node];
    secondLayer.position = CGPointMake(0, 700);
    [self addChild:secondLayer];
    CCMoveTo *moveLayer = [CCMoveTo actionWithDuration:1.0 position:CGPointMake(0, 0)];
    [secondLayer runAction:moveLayer];
}

SecondLayer.m (the info layer)

-(id) init
{
    if( (self=[super init])) 
    {

        //Change this sprite image based on button from main layer. I don't have it coded in yet, but I understand the concept of putting a variable in the file string using %@ or %d

        CCSprite *infoCard = [CCSprite spriteWithFile:@"species1.png"];
        infoCard.anchorPoint = CGPointMake(0.5, 0);
        infoCard.position = CGPointMake(512, 0);
        [self addChild:infoCard];    
    }
    return self;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T00:15:34+00:00Added an answer on May 31, 2026 at 12:15 am

    Ok, this might work:

    //MainLayer:
    -(id) init
    {
        if( (self=[super init])) 
        {        
            CCMenuItem *flamingoButton = [CCMenuItemImage itemFromNormalImage:@"Explore-sign.png" 
                                                                selectedImage:@"Explore-sign.png" 
                                                                       target:self 
                                                                     selector:@selector(showSecondLayer:)];
            flamingoButton.position = ccp(0, 60);
            flamingoButton.tag = 1;
            CCMenu *menu = [CCMenu menuWithItems:flamingoButton, nil];
            [self addChild:menu];
        }
        return self;
    }
    
    -(void) showSecondLayer: (CCMenuItem*) sender
    {    
        secondLayer = [SecondLayer layerWithTag:[sender tag]];
        secondLayer.position = ccp(0, 700);
        [self addChild:secondLayer];
        CCMoveTo *moveLayer = [CCMoveTo actionWithDuration:1.0 position:ccp(0, 0)];
        [secondLayer runAction:moveLayer];
    }
    
    //Second Layer.h
    +(id)layerWithTag:(NSInteger)aTag;
    -(id) initWithTag:(NSInteger)aTag;
    
    //Second Layer.m:
    +(id)layerWithTag:(NSInteger)aTag {
        return [[[SecondLayer alloc] initWithTag:aTag] autorelease];
    }
    
    -(id) initWithTag:(NSInteger)aTag
    {
        if( (self=[super init])) 
        {
    
            //Change this sprite image based on button from main layer. I don't have it coded in yet, but I understand the concept of putting a variable in the file string using %@ or %d
    
            CCSprite *infoCard = [CCSprite spriteWithFile:[NSString stringWithFormat:@"species%d.png", aTag]];
            infoCard.anchorPoint = ccp(0.5, 0);
            infoCard.position = ccp(512, 0);
            [self addChild:infoCard];    
        }
        return self;
    }
    

    EDIT:


    Even though the previous solution works, it’s not intuitive, and I feel I am breaking some OOP concepts. Most importantly, it is only useable given that your info about the animal can be retrieved using a single int! .. Using it this way is a BIT better, it’s totally up to you to decide:

    Ehm, so, I would suggest you set up an Entity Class first:

    //AnimalResources.h
    #import "Blahblahblah"
    
    //Give it a good name, I was always bad at Science:
    @interface AnimalResources {
        //load all your properties:
        NSString* info;
        CCSprite* sprite;
        ...
    }
    
    //set the properties as needed:
    //Make sure you properly manage this!! It is retained!
    @property (nonatomic, retain) CCSprite* sprite;
    ...
    
    //method prototype (signature.. am not sure)
    //Now, we shall build on the fact that it will be easy for you to map an integer to the right resources:
    +(id)animalResourcesWithTag:(NSInteger)aTag;
    -(id)initAnimalResourcesWithTag:(NSInteger)aTag;
    
    //AnimalResources.m:'
    
    @synthesize sprite, ... ;
    
    +(id)animalResourcesWithTag:(NSInteger)aTag {
        [[[AnimalResources alloc] initAnimalResourcesWithTag:aTag] autorelease];
    }
    -(id)initAnimalResourcesWithTag:(NSInteger)aTag {
        if ((self = [super init])) {
            //use tag to retrieve the resources:
            //might use the stringFormat + %d approach, or have a dictionary/array plist, that maps an int to a dictionary of resource keys.
    
            //string way of doing things:
            self.sprite = [CCSprite spriteWithFile:[NSString stringWithFormat:@"species%d.png", aTag]];
            ...
    
            //Dictionary: dict/array is an NSDictionary/NSArray read from disk sometime. Don't read it here, since it 
            //will read the file from disk many times if you do --> BAD. I could explain a rough way to do that if you 
            //need help
            animalDict = [dict objectForKey:[NSString stringWithFormat:@"species%d.png", aTag]];
            //OR...
            animalDict = [array objectAtIndex:aTag];
            //better to have @"spriteNameKey" defined in a macro somewhere: #define kAnimalResourceKeySprite @"SpriteKey"
            self.sprite = [CCSprite spriteWithFile:[animalDict objectForKey:@"SpriteNameKey"]];
            ....
        }
        return self;
    }
    
    Phew! Then .. you guessed it!
    
        -(void) showSecondLayer: (CCMenuItem*) sender
        {    
            secondLayer = [SecondLayer layerWithAnimalResources:[AnimalResources animalResourcesWithTag:[sender tag]]];
            secondLayer.position = ccp(0, 700);
            [self addChild:secondLayer];
            CCMoveTo *moveLayer = [CCMoveTo actionWithDuration:1.0 position:ccp(0, 0)];
            [secondLayer runAction:moveLayer];
        }
    
        //Second Layer.h
        +(id)layerWithAnimalResources:(AnimalResources*)resource;
        -(id)initWithAnimalResources:(AnimalResources*)resource;
    
        //Second Layer.m:
        +(id)layerWithAnimalResources:(AnimalResources*)resource {
            return [[[SecondLayer alloc] initWithAnimalResources:aTag] autorelease];
        }
    
        -(id) initWithAnimalResources:(AnimalResources*)resource
        {
            if( (self=[super init])) 
            {
    
                //Change this sprite image based on button from main layer. I don't have it coded in yet, but I understand the concept of putting a variable in the file string using %@ or %d
    
                CCSprite *infoCard = [resource sprite];
                infoCard.anchorPoint = ccp(0.5, 0);
                infoCard.position = ccp(512, 0);
                [self addChild:infoCard];    
            }
            return self;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to run a str_replace or preg_replace which looks for certain words
I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.