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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:26:04+00:00 2026-06-04T08:26:04+00:00

I’m having trouble with Objective C. I’m trying to call a block after I’ve

  • 0

I’m having trouble with Objective C.
I’m trying to call a block after I’ve moved a sprite

The short version of what i’m trying to achieve is that i want to move all of the enemies in an array, and when each one finishes moving i want to check whether it has collided.

The simplified code below shows what i’m trying to do.

My Actor class is defined like this

// -----------------------------
// Actor.h

#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "ScreenObject.h"

typedef void (^actorMoveComplete)(void);

@interface Actor : ScreenObject
{
   // actorMoveComplete onMoveComplete;
}

@property (nonatomic) BOOL isMoving;
@property (readwrite, copy) actorMoveComplete onMoveComplete;
@property (nonatomic, retain) CCSprite* sprite;

-(void) moveTo:(CGPoint)targetPosition Speed:(float) speed withCallback:(actorMoveComplete)moveComplete;

@end

// -----------------------------
//  Actor.m
#import "Actor.h"

@implementation Actor

@synthesize onMoveComplete;
@synthesize sprite;

-(void) moveTo:(CGPoint)targetPosition Speed:(float) speed withCallback:(actorMoveComplete)moveComplete;
{
    onMoveComplete = moveComplete;

    id actionMove = [CCMoveTo actionWithDuration:speed position:targetPosition];
    id actionMoveDone = [CCCallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished:)];
    [super.sprite runAction:[CCSequence actions:actionMove, actionMoveDone, nil]];
}

-(void) spriteMoveFinished:(id) sender
{
    isMoving = NO;
    if (onMoveComplete != nil)
        onMoveComplete();
}
@end

As you can see i’m trying to store the block in an onMoveComplete parameter (i’ve also tried it in a private variable), and then call it once the sprite move has completed.

In my calling class i’m iterating through a bunch of actors (enemies) and i want to call this anonymous code block for each actor once the move has completed:

{
   [self checkForCollision:enemy];
}

My calling class looks like this.

//------------------------------
//
//  GameLayer.h

#import "cocos2d.h"
#import "Actor.h"

@interface GameLayer : CCLayerColor
{
}

@property (nonatomic, copy) NSMutableArray *enemies;

- (void) updateEnemies;
- (void) checkForCollision:(Actor*)actor;
- (BOOL) isMapGridClear:(CGPoint)mapGrid excludeActor:(Actor*)actor;

@end


//------------------------------
//  GameLayer.m

#import "GameLayer.h"


@implementation GameLayer

@synthesize enemies;

- (void) updateEnemies
{

    for (Actor *enemy in enemies)
    {
        //  
        CGPoint newPosition = [self getNewPosition:enemy]; /* method to figure out new position */

        [enemy moveDelta:ccp(dX, dY) Speed:enemySpeed withCallback:^{
            [self checkForCollision:enemy];
        }];
    }
}


- (void) checkForCollision:(Actor*)actor
{
    if (![self isMapGridClear:actor.gridPosition excludeActor:actor])
    {
        actor.isCrashed=YES;
        [actor loadSprite:self spriteImage:@"crashed.png"];
    }
}


- (BOOL) isMapGridClear:(CGPoint)mapGrid excludeActor:(Actor*)actor
{
    /* Actual method figures out if there was a collision    */
    return YES;
}

@end

Unfortunately when I call the onMoveComplete block, i keep getting an EXC_BAD_ACCESS error
Interestingly, if I try to call the block inside the moveTo method, it works (but of course i want this to trigger AFTER the movement has completed).

Can anyone help me with what i’m doing wrong. Am I even using the correct mechanism?

(Apologies for the poorly formatted, incomplete code segments)

  • 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-06-04T08:26:06+00:00Added an answer on June 4, 2026 at 8:26 am

    You correctly declared your property as copy, but you are setting your instance variable directly to the address of the block without using the generated accessors. That means the block won’t get copied and gets destroyed before it is called.

    Assign your block using self.onMoveCompleted = moveCompleted and you will be fine.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm trying to create an if statement in PHP that prevents a single post
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
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
Basically, what I'm trying to create is a page of div tags, each has

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.