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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:57:11+00:00 2026-06-11T22:57:11+00:00

i create a sprite object which sets a CCSprite as its visual //shapes.h @interface

  • 0

i create a sprite object which sets a CCSprite as its visual

//shapes.h
@interface Shapes : CCSprite 
{
    CCSprite *mySprite;
    GamePlayLayerShapes *theGame;
    NSInteger shapeID;
}

@property (nonatomic, retain) CCSprite *mySprite;
@property (nonatomic, retain) GamePlayLayerShapes *theGame;
@property (nonatomic, readwrite) NSInteger shapeID;

@end

then in shapes.m i initialize the object

//shapes.m
@implementation Shapes

@synthesize mySprite, theGame, active, shapeID;

-(id) initWithGame:(GamePlayLayerShapes *)game andFile:(NSString *)file andNumber:  (NSInteger)number andZ:(NSInteger)z
{
self = [super init];
if(self != nil) 
{
    self.theGame = game;

    if (number > 25)
    {
        mySprite = [CCSprite spriteWithFile:file];
    }
    else
    {
        mySprite = [CCSprite spriteWithSpriteFrameName:file];
    }
    [theGame addChild:mySprite z:z];
    [mySprite setPosition:ccp(500, 200)];
    self.shapeID = number;
}
return self;
}

then in gameplaylayershapes.h i set up two NSMutableArray (i didn’t add all irrelevant variables.

@interface GamePlayLayerShapes : CCLayer 
{
CCSpriteBatchNode *shapesSpriteBatchNode;
NSMutableArray *shapeArray;
NSMutableArray *targetShapeArray;
    Shapes *newShape;
}

then i set it all up in gameplaylayershapes.m

-(id) init
{
self = [super init];
if(self != nil)
{
    srandom(time(NULL));
    shapeArray = [[NSMutableArray alloc] init];
    targetShapeArray = [[NSMutableArray alloc] init];



    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"GameplayShapes.plist"];
    shapesSpriteBatchNode = [CCSpriteBatchNode batchNodeWithFile:@"GameplayShapes.png"];

    [self addChild:shapesSpriteBatchNode];

    [self createTargetShape];

    [self runGame];

    }
return self;
}

-(void)createTargetShape
{
   //fills 4 variables with random numbers between 1 and 25
    targetShape1 = (arc4random() % 25) +1;
    targetShape2 = (arc4random() % 25) +1;
    targetShape3 = (arc4random() % 25) +1;
    targetShape4 = (arc4random() % 25) +1;

for (int x=1; x<5; x++)
{
    switch (x)
    {
        case 1:
            shapeFileName = [NSString stringWithFormat:@"shape%i.png",targetShape1];
            shapeNumber = targetShape1;
            break;
        case 2:
            shapeFileName = [NSString stringWithFormat:@"shape%i.png",targetShape2];
            shapeNumber = targetShape2;
            break;
        case 3:
            shapeFileName = [NSString stringWithFormat:@"shape%i.png",targetShape3];
            shapeNumber = targetShape3;
            break;
        case 4:
            shapeFileName = [NSString stringWithFormat:@"shape%i.png",targetShape4];
            shapeNumber = targetShape4;
            break;
        default:
            break;
    }
    newShape = [[Shapes alloc] initWithGame:self andFile:shapeFileName andNumber:shapeNumber andZ:10];
    [targetShapeArray addObject:newShape];
    [newShape release];
}
}

now what I’m trying to do is change the shapeID and mySprite for each shape object in targetshapearray. i can get the shapeID to change, but i cant for the life of me get the mySprite image to change which would change the appearance of it on my game. i keep getting a EXC_BAD_ACCESS error. Any Help would be appreciated

-(void)createNewTargetShapes
{
//fills 4 variables with random numbers between 1 and 25
targetShape1 = (arc4random() % 25) +1;
targetShape2 = (arc4random() % 25) +1;
targetShape3 = (arc4random() % 25) +1;
targetShape4 = (arc4random() % 25) +1;

for (int x=0; x<4; x++)
{
    Shapes * n = (Shapes *) [targetShapeArray objectAtIndex:x];
    switch (x)
        {
            case 0:
                n.shapeID = targetShape1;
                shapeFileName = [NSString stringWithFormat:@"shape%i.png",targetShape1];
                n.mySprite = [CCSprite spriteWithFile:shapeFileName];
                break;
            case 1:
                n.shapeID = targetShape2;
                shapeFileName = [NSString stringWithFormat:@"shape%i.png",targetShape2];
                n.mySprite = [CCSprite spriteWithFile:shapeFileName];
                break;
            case 2:
                n.shapeID = targetShape3;
                shapeFileName = [NSString stringWithFormat:@"shape%i.png",targetShape3];
                n.mySprite = [CCSprite spriteWithFile:shapeFileName];
                break;
            case 3:
                n.shapeID = targetShape4;
                shapeFileName = [NSString stringWithFormat:@"shape%i.png",targetShape4];
                n.mySprite = [CCSprite spriteWithFile:shapeFileName];
                break;
            default:
                break;
        }
}
}
  • 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-11T22:57:13+00:00Added an answer on June 11, 2026 at 10:57 pm

    To change a CCSprites image you must change its texture. Here is an example block of code for how to do that:

    CCTexture2D* tex = [[CCTextureCache sharedTextureCache] addImage:@"spriteimagefile.png"];
    [sprite setTexture: tex];
    

    This will change the sprites image to “spriteimagefile.png”.

    Use the above code instead of

    n.mySprite = [CCSprite spriteWithFile:shapeFileName];
    

    I hope that’s what you’re looking for!

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

Sidebar

Related Questions

I am basically trying to create a display object transformation manager which will allow
So I have an Application Sandbox HTMLLoader object which I create in AIR and
how can i create collision between the objects added to physics body and sprite
I've been trying to create a sprite sheet loader for days now, and I've
I need to create the same sprite, with the same image, like 50 -
I am trying to create a very basic sprite image. First off i have
i added sprite to scene by this tutorials : http://www.raywenderlich.com/9743/how-to-create-a-simple-2d-iphone-game-with-opengl-es-2-0-and-glkit-part-1 but i have bad
I know how I can create animation with TextureRegions and apply to an object
I've used Nick Vellios' tutorial to create radial gravity with a Box2D object. I
I've made a game where all objects use one sprite sheet, which works fine

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.