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 6838243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:34:24+00:00 2026-05-26T23:34:24+00:00

After Python Objective-C syntax blows my mind! I’m trying to create a menu that

  • 0

After Python Objective-C syntax blows my mind!

I’m trying to create a menu that will 25 buttons. Clicking on that will start is appropriate level. The levels differ only by calling [CCTMXTiledMap tiledMapWithTMXFile: @ “lvl_1-25.tmx”];

To create the menu, I use:

    CCMenuItemSprite *lvl_1_button= [CCMenuItemSprite itemFromNormalSprite:[GameButton buttonWithText:@"lvl 1"] selectedSprite:NULL target:self selector:@selector(lvl1_start)];
    ...
    CCMenu *menu = [CCMenu menuWithItems: lvl_1_button, lvl_2_button, lvl_3_button,  nil];       

    [self addChild:menu];

The scene changes with:

-(void)lvl1_start

{
    [[CCDirector sharedDirector] replaceScene:[lvl1_start node]];

}

In this case the difference between levels is minimal. In one line at initialization.

-(id) init
{
    if( (self=[super init])) 
    {
        self.tileMap = [CCTMXTiledMap tiledMapWithTMXFile:@"lvl_1.tmx"];

The result is a ton of duplicate code. How can this be simplified?

After all, only need to change the scene and simply pass a single variable (the level number) in the initialization method.

  • 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-26T23:34:25+00:00Added an answer on May 26, 2026 at 11:34 pm

    I’ll try to make it as simple as possible possible.

    To start add a new init method to your level scene which takes as argument the tilemap’s name for example:

    // LevelScene.h
    - (id)initWithTilemapName:(NSString *)tilemap;
    
    // LevelScene.m
    - (id)initWithTilemapName:(NSString *)tilemap
    {
        if ((self = [super init]))
        {
            self.tileMap = [CCTMXTiledMap tiledMapWithTMXFile:tilemap];
            // ...
    

    Then to make the menu creation more dynamic, add your item in a run loop and associate them with a tag (to be able to differentiate them):

    CCMenu *menu = [CCMenu menuWithItems:nil];
    [self addChild:menu];
    
    for (int i = 1; i <= 25; i++)
    {
        CCMenuItemSprite *lvlItem = 
        [CCMenuItemSprite itemFromNormalSprite:[GameButton buttonWithText:[NSString stringWithFormat:@"lvl%d",i]] selectedSprite:NULL target:self selector:@selector(lvl_start:)];
        [lvlItem setTag:i];
    
        [menu addChild:lvlItem];
    }
    

    Add to finish in the selector, retrieve the menu item and create the scene with its corresponding tilemap.

    - (void)lvl_start:(CCMenuItemS *)item
    {
        LevelScene *yourScene = [[LevelScene alloc] initWithTilemapName:[NSString stringWithFormat:@"lvl%d.tmx",item.tag];
    
        [[CCDirector sharedDirector] replaceScene:yourScene];
    
        [yourScene release];
    }
    

    This is just an example to give you an idea, I have not tested it. But I hope it’ll help you. 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use python to set environment variables that will persist in Pythons
I'm trying to debug a deadlock in a multi-threaded Python application after it has
I'm using parallel-python and start a new job server in a function. after the
In python after imports, one can see the file, that has been loaded/where the
I need to make some installation action (create a table record) after python manage.py
I'm trying to setup ipython.el in emacs23. I've successfully installed it (after putting python-mode.el
I'm trying to learn python after spending the last 15 or so years working
I'm trying to override the printed output from an Exception subclass in Python after
I'm currently re-engaging with Python after a long absence and loving it. However, I
G'day, I'm wanting to go back to Python after not using it for a

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.