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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:43:02+00:00 2026-05-30T14:43:02+00:00

i make a class called Tile which is a square, and the passed block

  • 0

i make a class called “Tile” which is a square, and the passed block will get called when touched.

-(id) initWithRect: (CGRect) r color: (ccColor4B) c block: (void (^) (void)) blk {
    if ((self = [super init])) {
        rect = r;
        color = c;
        block = blk;
        tile = [CCLayerColor layerWithColor:color width:rect.size.width height:rect.size.height];
        tile.position = ccp(rect.origin.x, rect.origin.y);
        [self addChild: tile];
        self.isTouchEnabled = YES;
    }
    return self;
}

//rect is the square, i use CCLayerColor to represent the square.

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    CGPoint touchLocation = [Helper locationFromTouch: touch];
    if (CGRectContainsPoint(rect, touchLocation)) {
        block();
        [tile setColor:ccGRAY];
        return YES;

    }
    else {
        return NO;
    }
}

//when touched, just call the block.

then i make a couple of Tiles as follows:

Tile* aTile = [Tile tileWithMidPos:ccp(512, 500) width:300 height:200 color:ccc4(250, 250, 250, 250) block:^{
            [Helper playEffectButtonClicked];
        }];

but all the tiles actually execute the block that is passed by the last tile.
what is the problem here? (every tile is an object, so they should call their own block)

  • 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-30T14:43:04+00:00Added an answer on May 30, 2026 at 2:43 pm

    Blocks are allocated on the stack.

    In this case the Tile class should make a copy of the blk argument:

    -(id) initWithRect: (CGRect) r color: (ccColor4B) c block: (void (^) (void)) blk {
        if ((self = [super init])) {
            rect = r;
            color = c;
            block = [blk copy];
            tile = [CCLayerColor layerWithColor:color width:rect.size.width height:rect.size.height];
            tile.position = ccp(rect.origin.x, rect.origin.y);
            [self addChild: tile];
            self.isTouchEnabled = YES;
        }
        return self;
    }
    
    - (void)dealloc {
        [block release];
        [super dealloc];
    }
    

    If you are using ARC, you won’t need to worry about memory management (copy and release) if they are passed to methods with block parameters. If you pass stack allocated blocks to objects with id parameters, you must still copy:

    [myArray addObject:[^{ // some block } copy]];
    

    Mike Ash has an article well worth reading regarding blocks and ARC.

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

Sidebar

Related Questions

well i need to make a class called TList which have a private attribute
I've got a class called 'MusicNote' which inherits from 'PictureBox'. I'm trying to make
I have a formpanel that is extended to make a class which is called
I have a custom made class called Graph in which I use adjacency lists.
How do I make a delegate? I have a class called CustomSign. The class
I have a class called Comments for articles which implements a base class of
I have an Abstract class called Function which has 2 inheriting classes (actually 6,
I want to make a class called Series (containing episodes and information about this
Say I have a class called PermissionManager which should only exist once for my
I'm trying to make a class called LoopingInt. It stores two integers, one being

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.