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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:11:10+00:00 2026-06-05T08:11:10+00:00

I have an NSMutableArray (named coins) that isn’t following a randomPoint method. When I

  • 0

I have an NSMutableArray (named coins) that isn’t following a randomPoint method. When I go to iOS Simulator, the coins don’t show up (as in there are no images) and they stay in the center of the screen instead of a randomly assigned point. This is what should make a new coin:

if ([coins count] == 0){

    CGSize windowSize = [CCDirector sharedDirector].winSize;
    CGPoint randomPointOnScreen = ccp((float)(arc4random() % 100) / 100 * windowSize.width, (float)(arc4random() % 100) / 100 * windowSize.height);

   randomCoinType = arc4random() % kNumberOfCoinTypes + 1; // I have this because my files are named like "coin.1.png" and such

kNumberOfCoinTypes is defined as 10 to give me a number 1-10.
This is the corresponding void that should make the coins:

-(void)createCoinsAt:(CGPoint)position withCointype:(int)type{

NSString *imageFile;

switch (type) {


    case 1:

        imageFile = @"coin.1.png";
        break;

    case 2:
        imageFile = @"coin.2.png";
        break;

    case 9:
    case 3:
        imageFile = @"coin.3.png";
        break;

    case 7:
    case 8:
    default:
    case 4:
        imageFile = @"coin.4.png";
        break;

    case 5:
        imageFile = @"coin.5.png";
        break;

    case 6:
        imageFile = @"coin.6.png";
        break;

      }

Coins *c = [Coins spriteWithFile:imageFile];
//Coins *c = [Coins spriteWithFile:[NSString stringWithFormat:@"coin.%d.png", arc4random() % 5 + 1 ]];

c.type = type;
c.position = position;
c.velocity = ccp(0,0);
[coins addObject:c];
[self addChild:c z:2];

}

I have multiple cases following one command because I don’t know how to make one number appear more often than another.

  • 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-05T08:11:12+00:00Added an answer on June 5, 2026 at 8:11 am

    Everything is fine with the code you’ve shown. Your problems are probably in your Coin class.

    Things to check in your Coin class:

    • Does setting velocity to {0,0} inadvertently change position?
    • What CCSprite methods have you overridden? Make sure to call the super of those methods.

    Other things to try:

    • Can you make a coin.1.png visibly appear at a random point using only the CCSprite class? If not…
      • Make sure the physical coin.1.png file is correctly added to your project.
      • Check the parent objects: are they flagged as invisible? are they added to the scene?

    UPDATE: Avoiding subclassing CCSprite

    @interface Coin : NSObject 
    
    @property (nonatomic, retain) CCSprite *sprite;
    @property (nonatomic, assign) CGPoint velocity; 
    @property (nonatomic, assign) int type;
    @end
    

    —

    -(void)createCoinAt:(CGPoint)position withCointype:(int)type{
    
        NSString *imageFile;
        ...
        //imageFile switch statement here
        ...
        Coin *c = [[Coin alloc] init];
    
        c.sprite = [CCSprite spriteWithFile:imageFile];
        c.sprite.position = position;
        c.type = type;
        c.velocity = ccp(0,0);
        [coins addObject:c];
        [parentNode addChild:c.sprite z:2]; //make sure to add to correct parent CCNode
    }
    

    —
    Once a Coin is given a velocity:

    //duration assumes velocity is pixels-per-second
    CCAction *moveSprite = [CCRepeatForever actionWithAction:[CCMoveBy actionWithDuration:1.0 position:c.velocity]];
    [c.sprite runAction:moveSprite]; 
    

    Once a Coin disappears/dies/whatever:

    [c.sprite stopAllActions];
    [c.sprite removeFromParentAndCleanup:YES];
    c.sprite = nil;
    [coins removeObject:c];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I Create a class named DataClass there i have this method +(NSMutableArray*) returnList :(NSString
In my iOS app, I am using a NSMutableArray, named imageMArray. I have set
I have a NSMutableArray: NSMutableArray *temp = //get list from somewhere. Now there is
I have an NSMutableArray that is populated with objects of strings. For simplicity sake
I have a NSMutableArray that I need to search for a string and return
I have a NSMutableArray (called listOfSites) that is created and used in classA.m. I
I have a NSMutableArray and a NSArray . Both consist of elements that are
Early warning - code sample a little long... I have a singleton NSMutableArray that
I have a NSMutableArray named as records and a NSArray . Now I am
I have NSMutableArray named *arrWords containing list of 50 words. e.g. Bond/Bind/King/Sing/Ring etc. I

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.