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

  • Home
  • SEARCH
  • 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 8219923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:20:47+00:00 2026-06-07T13:20:47+00:00

I’m new to Objective-C, I feel like I’m probably just making a dumb mistake,

  • 0

I’m new to Objective-C, I feel like I’m probably just making a dumb mistake, but I have tried googling this with no luck (maybe I’m just not searching the right things)

Essentially, I tried to write my own object class, and then make several instances of it, but the data seems locked together; Changing the data for any one reference changes it for all of them.

here’s where I make the objects and use them.

@implementation Drawing
//leaving many functions out as they are not part of the problem

Ball * balls[10];
int numPoints;

//this gets called first
- (id) initWithCoder: (NSCoder *)aDecoder
{
    //leaving out loading of images.....

    numPoints=10;
    for(int i=0; i<10; i++){
        balls[i]=[[Ball alloc] init];
        printf("bmem: %p\n",%balls[i]);
        float tpx=arc4random()%1024;
        float tpy=arc4random()%768;
        printf("randX: %f\n",tpx);
        printf("randY: %f\n",tpy);
        CGPoint tempPt = CGPointMake(tpx,tpy);
        printf("mem: %p\n",%tempPt);
        [balls[i] setLocation:tempPt];
    }
    //code to start a timer on the drawRect function....
}

//called regularly, every 30 seconds
- (void) drawRect:(CGRect)rect
{
    CGContextRef c=UIGraphicsGetCurrentContext();
    CGContextClearRect(c, rect);

    for(int i=0; i<numPoints; i++)
    {
        int ptX=[balls[i] getX];
        int ptY=[balls[i] getY];
        printf("index: %d\n",i);
        printf("x: %d\n",ptX);
        printf("y: %d\n",ptY);
        CGContextDrawImage(c, CGRectMake((int)ptX-WIDTH/2, (int)ptY-WIDTH/2, WIDTH, HEIGHT), image);
    }
}

This program outputs a series of numbers which I thought would be useful.
– “bmem”, or the point in memory where the ball is, increments by regular intervals, as expected.
– “randX” and “randY” are completely random, as they should be.
– “mem”, or the point in memory with the CGPoint, doesn’t change

and this is the ball object:

@implementation Ball
int x;
int y;

-(void)setLocation:(CGPoint)loc{
    x=loc.x;
    y=loc.y;
}

-(int)getX{
    return x;
}

-(int)getY{
    return y;
}

@end

At first I just though I had static attributes in the Ball class, but on googling I found out that objective-c does not have static attributes. I’ve blindly tried about a dozen different things with no success. I really just need this to work.

  • 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-07T13:20:49+00:00Added an answer on June 7, 2026 at 1:20 pm

    You are using the global variables:

    Ball * balls[10];
    int numPoints;
    

    When you probably want instance variables:

    @interface Balls : NSObject
    {
        Ball * balls[10];
        int numPoints;
    }
    ...
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I have some data like this: 1 2 3 4 5 9 2 6
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
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 have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have two tables with like below codes: Table: Accounts id | username |

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.